From 6827fec286caffbec738e1fa2f989bc8bf4e0172 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 4 Oct 2017 16:05:28 +0200 Subject: [PATCH] shorten command line: java 9 @argFile explicitly chosen --- .../application/ApplicationConfigurable.java | 2 +- .../intellij/execution/ui/JrePathEditor.java | 16 +++++-- .../ui/ShortenClasspathModeCombo.java | 46 ++++++++++++++++--- .../intellij/execution/ShortenClasspath.java | 15 +++++- .../configurations/SimpleJavaParameters.java | 18 +++++++- .../openapi/projectRoots/JdkUtil.java | 7 +-- .../configuration/JUnitConfigurable.java | 3 +- .../TestNGConfigurationEditor.java | 2 +- 8 files changed, 87 insertions(+), 22 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/application/ApplicationConfigurable.java b/java/execution/impl/src/com/intellij/execution/application/ApplicationConfigurable.java index c08054558247..cfa7a445d7dd 100644 --- a/java/execution/impl/src/com/intellij/execution/application/ApplicationConfigurable.java +++ b/java/execution/impl/src/com/intellij/execution/application/ApplicationConfigurable.java @@ -67,6 +67,7 @@ public class ApplicationConfigurable extends SettingsEditor(); - myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject)); } @Override diff --git a/java/execution/impl/src/com/intellij/execution/ui/JrePathEditor.java b/java/execution/impl/src/com/intellij/execution/ui/JrePathEditor.java index f2403eedff87..6fc8810279cf 100644 --- a/java/execution/impl/src/com/intellij/execution/ui/JrePathEditor.java +++ b/java/execution/impl/src/com/intellij/execution/ui/JrePathEditor.java @@ -34,8 +34,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.awt.event.ActionListener; import java.io.File; -import java.util.Comparator; import java.util.HashSet; import java.util.Set; @@ -100,11 +100,15 @@ public class JrePathEditor extends JPanel implements PanelWithAnchor { myComboBoxModel.add(new CustomJreItem(homePath)); } } - ComboBox comboBox = new ComboBox(myComboBoxModel, 100); + ComboBox comboBox = new ComboBox<>(myComboBoxModel, 100); comboBox.setEditable(true); - comboBox.setRenderer(new ColoredListCellRendererWrapper() { + comboBox.setRenderer(new ColoredListCellRenderer() { @Override - protected void doCustomize(JList list, JreComboBoxItem value, int index, boolean selected, boolean hasFocus) { + protected void customizeCellRenderer(@NotNull JList list, + JreComboBoxItem value, + int index, + boolean selected, + boolean hasFocus) { if (value != null) { value.render(this, selected); } @@ -195,6 +199,10 @@ public class JrePathEditor extends JPanel implements PanelWithAnchor { myLabel.setAnchor(anchor); } + public void addActionListener(ActionListener listener) { + myPathField.getComboBox().addActionListener(listener); + } + interface JreComboBoxItem { void render(SimpleColoredComponent component, boolean selected); String getPresentableText(); diff --git a/java/execution/impl/src/com/intellij/execution/ui/ShortenClasspathModeCombo.java b/java/execution/impl/src/com/intellij/execution/ui/ShortenClasspathModeCombo.java index 663b2971eb0c..7fc34abe50a9 100644 --- a/java/execution/impl/src/com/intellij/execution/ui/ShortenClasspathModeCombo.java +++ b/java/execution/impl/src/com/intellij/execution/ui/ShortenClasspathModeCombo.java @@ -3,22 +3,22 @@ package com.intellij.execution.ui; import com.intellij.execution.ShortenClasspath; import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.ProjectJdkTable; +import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.ui.ComboBox; import com.intellij.ui.ColoredListCellRenderer; import com.intellij.ui.SimpleTextAttributes; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; public class ShortenClasspathModeCombo extends ComboBox { private final Project myProject; - public ShortenClasspathModeCombo(Project project) { + public ShortenClasspathModeCombo(Project project, JrePathEditor pathEditor) { myProject = project; - addItem(null); - for (ShortenClasspath mode : ShortenClasspath.values()) { - addItem(mode); - } + initModel(null, pathEditor); setRenderer(new ColoredListCellRenderer() { @Override protected void customizeCellRenderer(@NotNull JList list, @@ -27,7 +27,7 @@ public class ShortenClasspathModeCombo extends ComboBox { boolean selected, boolean hasFocus) { if (value == null) { - ShortenClasspath defaultMode = ShortenClasspath.getDefaultMethod(myProject); + ShortenClasspath defaultMode = ShortenClasspath.getDefaultMethod(myProject, getJdkRoot(pathEditor)); append("user-local default: " + defaultMode.getPresentableName()).append(" - " + defaultMode.getDescription(), SimpleTextAttributes.GRAYED_ATTRIBUTES); } else { @@ -35,5 +35,39 @@ public class ShortenClasspathModeCombo extends ComboBox { } } }); + pathEditor.addActionListener(e -> { + Object item = getSelectedItem(); + initModel((ShortenClasspath)item, pathEditor); + }); + } + + private void initModel(ShortenClasspath preselection, JrePathEditor pathEditor) { + removeAllItems(); + + String jdkRoot = getJdkRoot(pathEditor); + addItem(null); + for (ShortenClasspath mode : ShortenClasspath.values()) { + if (mode.isApplicable(jdkRoot)) { + addItem(mode); + } + } + + setSelectedItem(preselection); + } + + @Nullable + private static String getJdkRoot(JrePathEditor pathEditor) { + String rootPath = null; + String jrePathOrName = pathEditor.getJrePathOrName(); + if (jrePathOrName != null) { + Sdk configuredJdk = ProjectJdkTable.getInstance().findJdk(jrePathOrName); + if (configuredJdk != null) { + rootPath = configuredJdk.getHomePath(); + } + else { + rootPath = jrePathOrName; + } + } + return rootPath; } } diff --git a/platform/lang-api/src/com/intellij/execution/ShortenClasspath.java b/platform/lang-api/src/com/intellij/execution/ShortenClasspath.java index 4563caf27c6b..3a923fcc612f 100644 --- a/platform/lang-api/src/com/intellij/execution/ShortenClasspath.java +++ b/platform/lang-api/src/com/intellij/execution/ShortenClasspath.java @@ -26,7 +26,13 @@ import org.jetbrains.annotations.NotNull; public enum ShortenClasspath { NONE("none", "java [options] classname [args]"), MANIFEST("JAR manifest", "java -cp classpath.jar classname [args]"), - CLASSPATH_FILE("classpath file", "java WrapperClass classpathFile [args]"); + CLASSPATH_FILE("classpath file", "java WrapperClass classpathFile [args]"), + ARGS_FILE("@argFiles", "java @argFile [args], applicable for java 9+") { + @Override + public boolean isApplicable(String jreRoot) { + return jreRoot != null && JdkUtil.isModularRuntime(jreRoot); + } + }; private final String myPresentableName; private final String myDescription; @@ -36,6 +42,10 @@ public enum ShortenClasspath { myDescription = description; } + public boolean isApplicable(String jreRoot) { + return true; + } + public String getDescription() { return myDescription; } @@ -44,8 +54,9 @@ public enum ShortenClasspath { return myPresentableName; } - public static ShortenClasspath getDefaultMethod(Project project) { + public static ShortenClasspath getDefaultMethod(Project project, String rootPath) { if (!JdkUtil.useDynamicClasspath(project)) return NONE; + if (rootPath != null && JdkUtil.isModularRuntime(rootPath)) return ARGS_FILE; if (JdkUtil.useClasspathJar()) return MANIFEST; return CLASSPATH_FILE; } diff --git a/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java b/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java index d17947d44745..0ba5cf35c911 100644 --- a/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java +++ b/platform/lang-api/src/com/intellij/execution/configurations/SimpleJavaParameters.java @@ -48,6 +48,7 @@ public class SimpleJavaParameters extends SimpleProgramParameters { private boolean myUseDynamicVMOptions; private boolean myUseDynamicParameters; private boolean myUseClasspathJar; + private boolean myArgFile; private String myJarPath; @Nullable @@ -130,6 +131,17 @@ public class SimpleJavaParameters extends SimpleProgramParameters { return myUseClasspathJar; } + public boolean isArgFile() { + return myArgFile; + } + + /** + * Option to use java 9 @argFile + */ + public void setArgFile(boolean argFile) { + myArgFile = argFile; + } + /** * Allows to use a specially crafted .jar file instead of a custom class loader to pass classpath/properties/parameters. * Would have no effect if user explicitly disabled idea.dynamic.classpath.jar @@ -139,9 +151,13 @@ public class SimpleJavaParameters extends SimpleProgramParameters { } public void setShortenClasspath(@Nullable ShortenClasspath mode, Project project) { - if (mode == null) mode = ShortenClasspath.getDefaultMethod(project); + if (mode == null) { + Sdk jdk = getJdk(); + mode = ShortenClasspath.getDefaultMethod(project, jdk != null ? jdk.getHomePath() : null); + } myUseDynamicClasspath = mode != ShortenClasspath.NONE; myUseClasspathJar = mode == ShortenClasspath.MANIFEST; + setArgFile(mode == ShortenClasspath.ARGS_FILE); } public String getJarPath() { diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java index 9d3b9aa3f287..11a861ef196c 100644 --- a/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/JdkUtil.java @@ -167,7 +167,7 @@ public class JdkUtil { if (dynamicClasspath) { Class commandLineWrapper; - if (canUseArgFile(commandLine, javaParameters)) { + if (javaParameters.isArgFile()) { setArgFileParams(commandLine, javaParameters, vmParameters, dynamicVMOptions, dynamicParameters); dynamicMainClass = dynamicParameters; } @@ -197,11 +197,6 @@ public class JdkUtil { } } - private static boolean canUseArgFile(GeneralCommandLine commandLine, SimpleJavaParameters javaParameters) { - return javaParameters.getModuleName() != null || - isModularRuntime(new File(commandLine.getExePath()).getParentFile().getParentFile()); - } - private static boolean explicitClassPath(ParametersList vmParameters) { return vmParameters.hasParameter("-cp") || vmParameters.hasParameter("-classpath") || vmParameters.hasParameter("--class-path"); } diff --git a/plugins/junit/src/com/intellij/execution/junit2/configuration/JUnitConfigurable.java b/plugins/junit/src/com/intellij/execution/junit2/configuration/JUnitConfigurable.java index 1f0daa36ec8a..132969475d28 100644 --- a/plugins/junit/src/com/intellij/execution/junit2/configuration/JUnitConfigurable.java +++ b/plugins/junit/src/com/intellij/execution/junit2/configuration/JUnitConfigurable.java @@ -278,6 +278,8 @@ public class JUnitConfigurable extends SettingsEdi for (LocalChangeList changeList : changeLists) { model.addElement(changeList.getName()); } + + myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject, myJrePathEditor)); } private static void addRadioButtonsListeners(final JRadioButton[] radioButtons, ChangeListener listener) { @@ -525,7 +527,6 @@ public class JUnitConfigurable extends SettingsEdi myMethod.setComponent(textFieldWithBrowseButton); myShortenClasspathModeCombo = new LabeledComponent<>(); - myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject)); } @Override diff --git a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfigurationEditor.java b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfigurationEditor.java index 8a2000091568..a716ede4c522 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfigurationEditor.java +++ b/plugins/testng/src/com/theoryinpractice/testng/configuration/TestNGConfigurationEditor.java @@ -203,6 +203,7 @@ public class TestNGConfigurationEditor extends Se commonJavaParameters.setProgramParametersLabel(ExecutionBundle.message("junit.configuration.test.runner.parameters.label")); + myShortenCommandLineCombo.setComponent(new ShortenClasspathModeCombo(project, alternateJDK)); setAnchor(outputDirectory.getLabel()); alternateJDK.setAnchor(moduleClasspath.getLabel()); commonJavaParameters.setAnchor(moduleClasspath.getLabel()); @@ -370,7 +371,6 @@ public class TestNGConfigurationEditor extends Se private void createUIComponents() { myShortenCommandLineCombo = new LabeledComponent<>(); - myShortenCommandLineCombo.setComponent(new ShortenClasspathModeCombo(project)); } private static void registerListener(JRadioButton[] buttons, ChangeListener changelistener) {