shorten command line: java 9 @argFile explicitly chosen

This commit is contained in:
Anna.Kozlova
2017-10-04 20:46:26 +02:00
parent 98d6440c8b
commit 6827fec286
8 changed files with 87 additions and 22 deletions
@@ -67,6 +67,7 @@ public class ApplicationConfigurable extends SettingsEditor<ApplicationConfigura
ClassBrowser.createApplicationClassBrowser(project, myModuleSelector).setField(getMainClassField());
myVersionDetector = new JreVersionDetector();
myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject, myJrePathEditor));
myAnchor = UIUtil.mergeComponentsWithAnchor(myMainClass, myCommonProgramParameters, myJrePathEditor, myModule,
myShortenClasspathModeCombo);
}
@@ -136,7 +137,6 @@ public class ApplicationConfigurable extends SettingsEditor<ApplicationConfigura
}
}));
myShortenClasspathModeCombo = new LabeledComponent<>();
myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject));
}
@Override
@@ -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<JreComboBoxItem> comboBox = new ComboBox<>(myComboBoxModel, 100);
comboBox.setEditable(true);
comboBox.setRenderer(new ColoredListCellRendererWrapper<JreComboBoxItem>() {
comboBox.setRenderer(new ColoredListCellRenderer<JreComboBoxItem>() {
@Override
protected void doCustomize(JList list, JreComboBoxItem value, int index, boolean selected, boolean hasFocus) {
protected void customizeCellRenderer(@NotNull JList<? extends JreComboBoxItem> 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();
@@ -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<ShortenClasspath> {
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<ShortenClasspath>() {
@Override
protected void customizeCellRenderer(@NotNull JList<? extends ShortenClasspath> list,
@@ -27,7 +27,7 @@ public class ShortenClasspathModeCombo extends ComboBox<ShortenClasspath> {
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<ShortenClasspath> {
}
}
});
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;
}
}
@@ -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;
}
@@ -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() {
@@ -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");
}
@@ -278,6 +278,8 @@ public class JUnitConfigurable<T extends JUnitConfiguration> 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<T extends JUnitConfiguration> extends SettingsEdi
myMethod.setComponent(textFieldWithBrowseButton);
myShortenClasspathModeCombo = new LabeledComponent<>();
myShortenClasspathModeCombo.setComponent(new ShortenClasspathModeCombo(myProject));
}
@Override
@@ -203,6 +203,7 @@ public class TestNGConfigurationEditor<T extends TestNGConfiguration> 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<T extends TestNGConfiguration> extends Se
private void createUIComponents() {
myShortenCommandLineCombo = new LabeledComponent<>();
myShortenCommandLineCombo.setComponent(new ShortenClasspathModeCombo(project));
}
private static void registerListener(JRadioButton[] buttons, ChangeListener changelistener) {