From a48bcfd5e425d05ab3d2d0fbcc0a5c13c70019c4 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Wed, 5 Dec 2012 15:37:12 +0400 Subject: [PATCH] Fix memory enlarger dialog for new Mac launcher --- .../diagnostic/OutOfMemoryDialog.java | 4 +- .../com/intellij/diagnostic/VMOptions.java | 113 +++++++------- .../intellij/diagnostic/VMOptionsTest.java | 146 +----------------- 3 files changed, 64 insertions(+), 199 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/diagnostic/OutOfMemoryDialog.java b/platform/platform-impl/src/com/intellij/diagnostic/OutOfMemoryDialog.java index 3723db3a4670..9b28593f92a4 100644 --- a/platform/platform-impl/src/com/intellij/diagnostic/OutOfMemoryDialog.java +++ b/platform/platform-impl/src/com/intellij/diagnostic/OutOfMemoryDialog.java @@ -23,6 +23,7 @@ import com.intellij.ui.components.JBLabel; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; +import java.io.File; public class OutOfMemoryDialog extends DialogWrapper { public enum MemoryKind { @@ -60,7 +61,8 @@ public class OutOfMemoryDialog extends DialogWrapper { memoryKind == MemoryKind.HEAP ? VMOptions.XMX_OPTION_NAME : memoryKind == MemoryKind.PERM_GEN ? VMOptions.PERM_GEN_OPTION_NAME : VMOptions.CODE_CACHE_OPTION_NAME, ApplicationNamesInfo.getInstance().getProductName())); - final String path = VMOptions.getSettingsFilePath(); + File file = VMOptions.getWriteFile(); + final String path = file != null ? file.getPath() : null; if (path != null) { mySettingsFileHintLabel.setText(DiagnosticBundle.message("diagnostic.out.of.memory.willBeSavedTo", path)); } diff --git a/platform/platform-impl/src/com/intellij/diagnostic/VMOptions.java b/platform/platform-impl/src/com/intellij/diagnostic/VMOptions.java index 03774ed51f10..16b2750eece1 100644 --- a/platform/platform-impl/src/com/intellij/diagnostic/VMOptions.java +++ b/platform/platform-impl/src/com/intellij/diagnostic/VMOptions.java @@ -47,18 +47,12 @@ public class VMOptions { @NonNls private static final Pattern XMX_PATTERN = Pattern.compile(XMX_OPTION + MEM_SIZE_EXPR); @NonNls private static final Pattern PERM_GEN_PATTERN = Pattern.compile(PERM_GEN_OPTION + MEM_SIZE_EXPR); @NonNls private static final Pattern CODE_CACHE_PATTERN = Pattern.compile(CODE_CACHE_OPTION + MEM_SIZE_EXPR); - @NonNls private static final Pattern MAC_OS_VM_OPTIONS_PATTERN = - Pattern.compile("(" + MAC_ARCH_VM_OPTIONS + "(?:(?:\\s*)(?:(?:\\s*))*))(.*)()"); - - @NonNls private static final String INFO_PLIST = "/Contents/Info.plist"; private static String ourTestPath; - private static boolean ourTestMacOs; @TestOnly - static void setTestFile(String path, boolean isMacOs) { + static void setTestFile(String path) { ourTestPath = path; - ourTestMacOs = isMacOs; } @TestOnly @@ -92,15 +86,11 @@ public class VMOptions { @Nullable public static String read() { - File file = getFile(); + File file = getReadFile(); if (file == null) return null; try { - String content = FileUtil.loadFile(file); - if (isMacOs()) { - content = extractMacOsVMOptions(content); - } - return content; + return FileUtil.loadFile(file); } catch (IOException e) { LOG.info(e); @@ -137,32 +127,19 @@ public class VMOptions { } private static void writeOption(String option, int value, Pattern pattern) { - File file = getFile(); + File file = getWriteFile(); if (file == null) return; try { String optionValue = option + value + "m"; - String content = FileUtil.loadFile(file); - String vmOptions; + String content = read(); - if (isMacOs()) { - vmOptions = extractMacOsVMOptions(content); - if (vmOptions == null) return; - } - else { - vmOptions = content; + content = replace(pattern, content, optionValue, "", "", content + " " + optionValue); + + if (file.exists()) { + FileUtil.setReadOnlyAttribute(file.getPath(), false); } - vmOptions = replace(pattern, vmOptions, optionValue, "", "", vmOptions + " " + optionValue); - - if (isMacOs()) { - content = replace(MAC_OS_VM_OPTIONS_PATTERN, content, vmOptions, "$1", "$3", content); - } - else { - content = vmOptions; - } - - FileUtil.setReadOnlyAttribute(file.getPath(), false); FileUtil.writeToFile(file, content.getBytes()); } catch (IOException e) { @@ -170,13 +147,6 @@ public class VMOptions { } } - @Nullable - private static String extractMacOsVMOptions(String text) { - Matcher m = MAC_OS_VM_OPTIONS_PATTERN.matcher(text); - if (!m.find()) return null; - return m.group(2); - } - private static String replace(Pattern pattern, String text, String replacement, @@ -194,35 +164,76 @@ public class VMOptions { } @Nullable - private static File getFile() { - final String path = ourTestPath != null ? ourTestPath : getSettingsFilePath(); - return path != null ? new File(path) : null; + private static File getReadFile() { + if (ourTestPath != null) { + return new File(ourTestPath); + } + + File custom = getCustomFile(true); + if (custom != null) return custom; + + return getDefaultFile(); } - @NonNls @Nullable - public static String getSettingsFilePath() { - final File f = new File(doGetSettingsFilePath()).getAbsoluteFile(); + public static File getWriteFile() { + if (ourTestPath != null) { + return new File(ourTestPath); + } + + File custom = getCustomFile(false); + if (custom != null) return custom; + + return getDefaultFile(); + } + + @Nullable + public static File getDefaultFile() { + final File f = new File(doGetSettingsFilePath(false)).getAbsoluteFile(); if (!f.exists()) return null; try { - return f.getCanonicalPath(); + return f.getCanonicalFile(); } catch (IOException e) { LOG.debug(e); - return f.getPath(); + return f; + } + } + + @Nullable + public static File getCustomFile(boolean ifExists) { + if (!SystemInfo.isMac) return null; + + final File f = new File(doGetSettingsFilePath(true)).getAbsoluteFile(); + if (!f.exists()) { + if (ifExists) return null; + return f; + } + + try { + return f.getCanonicalFile(); + } + catch (IOException e) { + LOG.debug(e); + return f; } } @NotNull - private static String doGetSettingsFilePath() { + private static String doGetSettingsFilePath(boolean customLocation) { final String vmOptionsFile = System.getProperty("jb.vmOptionsFile"); if (!StringUtil.isEmptyOrSpaces(vmOptionsFile)) { return vmOptionsFile; } if (SystemInfo.isMac) { - return PathManager.getHomePath() + INFO_PLIST; + if (customLocation) { + return PathManager.getConfigPath() + "/idea.vmoptions"; + } + else { + return PathManager.getBinPath() + "/idea.vmoptions"; + } } final String productName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase(); @@ -230,8 +241,4 @@ public class VMOptions { final String osSuffix = SystemInfo.isWindows ? ".exe" : ""; return PathManager.getBinPath() + File.separatorChar + productName + platformSuffix + osSuffix + ".vmoptions"; } - - private static boolean isMacOs() { - return ourTestPath != null ? ourTestMacOs : SystemInfo.isMac; - } } diff --git a/platform/platform-tests/testSrc/com/intellij/diagnostic/VMOptionsTest.java b/platform/platform-tests/testSrc/com/intellij/diagnostic/VMOptionsTest.java index e74e1c2c8e52..9f1493bfe616 100644 --- a/platform/platform-tests/testSrc/com/intellij/diagnostic/VMOptionsTest.java +++ b/platform/platform-tests/testSrc/com/intellij/diagnostic/VMOptionsTest.java @@ -33,7 +33,7 @@ public class VMOptionsTest extends UsefulTestCase { myFile = FileUtil.createTempFile("vmoptions.", ".txt"); writeFile("-Xmx512m\n" + "-XX:MaxPermSize=128m"); - VMOptions.setTestFile(myFile.getPath(), false); + VMOptions.setTestFile(myFile.getPath()); } @Override @@ -167,150 +167,6 @@ public class VMOptionsTest extends UsefulTestCase { readFile()); } - public void testReadAndWriteUnderMacOs() throws IOException { - VMOptions.setTestFile(myFile.getPath(), true); - - writeFile("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -someOption -Xmx512m -XX:MaxPermSize=128m -otherOption\n" + - " \n" + - " \n" + - ""); - - assertEquals(512, VMOptions.readXmx()); - assertEquals(128, VMOptions.readMaxPermGen()); - - VMOptions.writeXmx(1024); - VMOptions.writeMaxPermGen(256); - - assertEquals("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -someOption -Xmx1024m -XX:MaxPermSize=256m -otherOption\n" + - " \n" + - " \n" + - "", - readFile()); - } - - public void testReadAndWriteNewUnderMacOs() throws IOException { - VMOptions.setTestFile(myFile.getPath(), true); - - writeFile("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -someOption\n" + - " \n" + - " \n" + - ""); - - assertEquals(-1, VMOptions.readXmx()); - assertEquals(-1, VMOptions.readMaxPermGen()); - - VMOptions.writeXmx(1024); - VMOptions.writeMaxPermGen(256); - - assertEquals("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -someOption -Xmx1024m -XX:MaxPermSize=256m\n" + - " \n" + - " \n" + - "", - readFile()); - } - - public void testReadAndWriteUnderMacOsIfThereAreCommentsBetweenTags() throws IOException { - VMOptions.setTestFile(myFile.getPath(), true); - - writeFile("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " \n" + - " \n" + - " -someOption\n" + - " \n" + - " \n" + - ""); - - assertEquals(-1, VMOptions.readXmx()); - assertEquals(-1, VMOptions.readMaxPermGen()); - - VMOptions.writeXmx(1024); - VMOptions.writeMaxPermGen(256); - - assertEquals("\n" + - " \n" + - " \n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " \n" + - " \n" + - " -someOption -Xmx1024m -XX:MaxPermSize=256m\n" + - " \n" + - " \n" + - "", - readFile()); - } - - public void testReadAndWriteIntoExactTagUnderMacOs() throws IOException { - VMOptions.setTestFile(myFile.getPath(), true); - - writeFile("\n" + - " \n" + - " \n" + - " Another section\n" + - " -Xmx111m\n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -Xmx222m\n" + - " \n" + - " \n" + - ""); - assertEquals(222, VMOptions.readXmx()); - - VMOptions.writeXmx(333); - assertEquals("\n" + - " \n" + - " \n" + - " Another section\n" + - " -Xmx111m\n" + - " " + VMOptions.MAC_ARCH_VM_OPTIONS + "\n" + - " -Xmx333m\n" + - " \n" + - " \n" + - "", - readFile()); - } - - public void testDoNothingIfNoVmOptionsSectionUnderMacOs() throws IOException { - VMOptions.setTestFile(myFile.getPath(), true); - - writeFile("\n" + - " \n" + - " \n" + - " Another section\n" + - " -Xmx111m\n" + - " \n" + - " \n" + - ""); - assertEquals(-1, VMOptions.readXmx()); - - VMOptions.writeXmx(333); - assertEquals("\n" + - " \n" + - " \n" + - " Another section\n" + - " -Xmx111m\n" + - " \n" + - " \n" + - "", - readFile()); - } private String readFile() throws IOException { return FileUtil.loadFile(myFile);