[platform] unified SimpleJavaParameters to GeneralCommandLine conversion

This commit is contained in:
Roman Shevchenko
2016-12-02 13:34:58 +01:00
parent 615b3d3985
commit 13148178d9
5 changed files with 95 additions and 95 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,20 +20,13 @@
package com.intellij.execution.configurations;
import com.intellij.execution.CantRunException;
import com.intellij.execution.ExecutionBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkType;
import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.util.Computable;
public class CommandLineBuilder {
private CommandLineBuilder() { }
public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters) throws CantRunException {
return createFromJavaParameters(javaParameters, false);
return javaParameters.toCommandLine();
}
/**
@@ -50,7 +43,13 @@ public class CommandLineBuilder {
public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters,
final Project project,
final boolean dynamicClasspath) throws CantRunException {
return createFromJavaParameters(javaParameters, dynamicClasspath && JdkUtil.useDynamicClasspath(project));
if (dynamicClasspath) {
javaParameters.setUseDynamicClasspath(project);
}
else {
javaParameters.setUseDynamicClasspath(false);
}
return javaParameters.toCommandLine();
}
/**
@@ -61,43 +60,7 @@ public class CommandLineBuilder {
*/
public static GeneralCommandLine createFromJavaParameters(final SimpleJavaParameters javaParameters,
final boolean forceDynamicClasspath) throws CantRunException {
try {
return ApplicationManager.getApplication().runReadAction(new Computable<GeneralCommandLine>() {
public GeneralCommandLine compute() {
try {
final Sdk jdk = javaParameters.getJdk();
if (jdk == null) {
throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
}
final SdkTypeId sdkType = jdk.getSdkType();
if (!(sdkType instanceof JavaSdkType)) {
throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
}
final String exePath = ((JavaSdkType)sdkType).getVMExecutablePath(jdk);
if (exePath == null) {
throw new CantRunException(ExecutionBundle.message("run.configuration.cannot.find.vm.executable"));
}
if (javaParameters.getMainClass() == null && javaParameters.getJarPath() == null) {
throw new CantRunException(ExecutionBundle.message("main.class.is.not.specified.error.message"));
}
return JdkUtil.setupJVMCommandLine(exePath, javaParameters, forceDynamicClasspath);
}
catch (CantRunException e) {
throw new RuntimeException(e);
}
}
});
}
catch (RuntimeException e) {
if (e.getCause() instanceof CantRunException) {
throw (CantRunException)e.getCause();
}
else {
throw e;
}
}
javaParameters.setUseDynamicClasspath(forceDynamicClasspath);
return javaParameters.toCommandLine();
}
}
}
@@ -74,6 +74,11 @@ class JavadocHtmlLintAnnotator(private val manual: Boolean = false) :
val annotations = parse(output.stdoutLines)
return if (annotations.isNotEmpty()) Result(annotations) else null
}
catch (e: Exception) {
val log = Logger.getInstance(JavadocHtmlLintAnnotator::class.java)
log.debug(file.path, e)
return null
}
finally {
FileUtil.delete(copy)
}
@@ -15,11 +15,12 @@
*/
package com.intellij.execution.configurations;
import com.intellij.execution.CantRunException;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessTerminatedListener;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.JavaSdkType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.vfs.CharsetToolkit;
@@ -94,10 +95,18 @@ public class SimpleJavaParameters extends SimpleProgramParameters {
myCharset = charset;
}
public boolean isDynamicClasspath() {
return myUseDynamicClasspath;
}
public void setUseDynamicClasspath(boolean useDynamicClasspath) {
myUseDynamicClasspath = useDynamicClasspath;
}
public void setUseDynamicClasspath(@Nullable Project project) {
myUseDynamicClasspath = JdkUtil.useDynamicClasspath(project);
}
public boolean isDynamicVMOptions() {
return myUseDynamicVMOptions;
}
@@ -139,12 +148,13 @@ public class SimpleJavaParameters extends SimpleProgramParameters {
myJarPath = jarPath;
}
/**
* @throws CantRunException when incorrect Java SDK is specified
* @see JdkUtil#setupJVMCommandLine(SimpleJavaParameters)
*/
@NotNull
public GeneralCommandLine toCommandLine() {
Sdk jdk = getJdk();
if (jdk == null) throw new IllegalArgumentException("SDK should be defined");
String exePath = ((JavaSdkType)jdk.getSdkType()).getVMExecutablePath(jdk);
return JdkUtil.setupJVMCommandLine(exePath, this, myUseDynamicClasspath);
public GeneralCommandLine toCommandLine() throws CantRunException {
return JdkUtil.setupJVMCommandLine(this);
}
@NotNull
@@ -17,6 +17,7 @@ package com.intellij.openapi.projectRoots;
import com.intellij.execution.CantRunException;
import com.intellij.execution.CommandLineWrapperUtil;
import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.GeneralCommandLine.ParentEnvironmentType;
import com.intellij.execution.configurations.ParametersList;
@@ -103,22 +104,6 @@ public class JdkUtil {
return null;
}
/** @deprecated to be removed in IDEA 2018 */
@Nullable
public static String getJarMainAttribute(@NotNull VirtualFile jarRoot, @NotNull Attributes.Name attribute) {
VirtualFile manifestFile = jarRoot.findFileByRelativePath(JarFile.MANIFEST_NAME);
if (manifestFile != null) {
try (InputStream stream = manifestFile.getInputStream()) {
return new Manifest(stream).getMainAttributes().getValue(attribute);
}
catch (IOException e) {
LOG.debug(e);
}
}
return null;
}
public static boolean checkForJdk(@NotNull String homePath) {
return checkForJdk(new File(FileUtil.toSystemDependentName(homePath)));
}
@@ -162,18 +147,27 @@ public class JdkUtil {
new File(homePath, "classes").isDirectory(); // custom build
}
public static GeneralCommandLine setupJVMCommandLine(final String exePath,
final SimpleJavaParameters javaParameters,
final boolean forceDynamicClasspath) {
final GeneralCommandLine commandLine = new GeneralCommandLine(exePath);
public static GeneralCommandLine setupJVMCommandLine(@NotNull SimpleJavaParameters javaParameters) throws CantRunException {
Sdk jdk = javaParameters.getJdk();
if (jdk == null) throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
SdkTypeId type = jdk.getSdkType();
if (!(type instanceof JavaSdkType)) throw new CantRunException(ExecutionBundle.message("run.configuration.error.no.jdk.specified"));
String exePath = ((JavaSdkType)type).getVMExecutablePath(jdk);
if (exePath == null) throw new CantRunException(ExecutionBundle.message("run.configuration.cannot.find.vm.executable"));
GeneralCommandLine commandLine = new GeneralCommandLine(exePath);
setupCommandLine(commandLine, javaParameters);
return commandLine;
}
private static void setupCommandLine(GeneralCommandLine commandLine, SimpleJavaParameters javaParameters) throws CantRunException {
final ParametersList vmParameters = javaParameters.getVMParametersList();
commandLine.withEnvironment(javaParameters.getEnv());
commandLine.withParentEnvironmentType(javaParameters.isPassParentEnvs() ? ParentEnvironmentType.CONSOLE : ParentEnvironmentType.NONE);
final Class commandLineWrapper;
boolean passProgramParametersViaClassPathJar = false;
if (forceDynamicClasspath &&
if (javaParameters.isDynamicClasspath() &&
!explicitClassPath(vmParameters) &&
javaParameters.getModulePath().isEmpty() &&
(commandLineWrapper = getCommandLineWrapperClass()) != null) {
@@ -209,8 +203,6 @@ public class JdkUtil {
}
commandLine.withWorkDirectory(javaParameters.getWorkingDirectory());
return commandLine;
}
private static boolean explicitClassPath(ParametersList vmParameters) {
@@ -224,7 +216,7 @@ public class JdkUtil {
private static void appendOldCommandLineWrapper(SimpleJavaParameters javaParameters,
GeneralCommandLine commandLine,
ParametersList vmParametersList,
Class commandLineWrapper) {
Class commandLineWrapper) throws CantRunException {
File vmParamsFile = null;
if (javaParameters.isDynamicVMOptions() && useDynamicVMOptions()) {
try {
@@ -283,8 +275,7 @@ public class JdkUtil {
commandLine.addParameter(classpath);
}
catch (IOException e) {
LOG.info(e);
throwUnableToCreateTempFile();
throwUnableToCreateTempFile(e);
}
appendEncoding(javaParameters, commandLine, vmParametersList);
@@ -305,7 +296,7 @@ public class JdkUtil {
GeneralCommandLine commandLine,
ParametersList vmParametersList,
Class commandLineWrapper,
boolean storeProgramParametersInJar) {
boolean storeProgramParametersInJar) throws CantRunException {
try {
final Manifest manifest = new Manifest();
manifest.getMainAttributes().putValue("Created-By", ApplicationNamesInfo.getInstance().getFullProductName());
@@ -353,13 +344,12 @@ public class JdkUtil {
}
}
catch (IOException e) {
LOG.info(e);
throwUnableToCreateTempFile();
throwUnableToCreateTempFile(e);
}
}
private static void throwUnableToCreateTempFile() {
throw new RuntimeException(new CantRunException("Failed to create temp file with long classpath in " + FileUtilRt.getTempDirectory()));
private static void throwUnableToCreateTempFile(IOException cause) throws CantRunException {
throw new CantRunException("Failed to a create temporary file in " + FileUtilRt.getTempDirectory(), cause);
}
private static boolean isClassPathJarEnabled(SimpleJavaParameters javaParameters, String currentPath) {
@@ -460,4 +450,37 @@ public class JdkUtil {
public static boolean useClasspathJar() {
return PropertiesComponent.getInstance().getBoolean("idea.dynamic.classpath.jar", true);
}
//<editor-fold desc="Deprecated stuff.">
/** @deprecated to be removed in IDEA 2018 */
@Nullable
public static String getJarMainAttribute(@NotNull VirtualFile jarRoot, @NotNull Attributes.Name attribute) {
VirtualFile manifestFile = jarRoot.findFileByRelativePath(JarFile.MANIFEST_NAME);
if (manifestFile != null) {
try (InputStream stream = manifestFile.getInputStream()) {
return new Manifest(stream).getMainAttributes().getValue(attribute);
}
catch (IOException e) {
LOG.debug(e);
}
}
return null;
}
/** @deprecated use {@link SimpleJavaParameters#toCommandLine()} (to be removed in IDEA 2018) */
public static GeneralCommandLine setupJVMCommandLine(final String exePath,
final SimpleJavaParameters javaParameters,
final boolean forceDynamicClasspath) {
try {
javaParameters.setUseDynamicClasspath(forceDynamicClasspath);
GeneralCommandLine commandLine = new GeneralCommandLine(exePath);
setupCommandLine(commandLine, javaParameters);
return commandLine;
}
catch (CantRunException e) {
throw new RuntimeException(e);
}
}
//</editor-fold>
}
@@ -180,22 +180,21 @@ public class GrabDependencies implements IntentionAction {
for (String grabText : queries.keySet()) {
final JavaParameters javaParameters = GroovyScriptRunConfiguration.createJavaParametersWithSdk(module);
//debug
//javaParameters.getVMParametersList().add("-Xdebug"); javaParameters.getVMParametersList().add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5239");
//javaParameters.getVMParametersList().add("-Xdebug");
//javaParameters.getVMParametersList().add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5239");
try {
DefaultGroovyScriptRunner.configureGenericGroovyRunner(javaParameters, module, GRAPE_RUNNER, false, true);
javaParameters.getClassPath().add(PathUtil.getJarPathForClass(GrapeRunner.class));
javaParameters.getProgramParametersList().add(queries.get(grabText));
javaParameters.setUseDynamicClasspath(true);
lines.put(grabText, javaParameters.toCommandLine());
}
catch (CantRunException e) {
NOTIFICATION_GROUP.createNotification("Can't run @Grab: " + ExceptionUtil.getMessage(e), ExceptionUtil.getThrowableText(e), NotificationType.ERROR, null).notify(project);
String title = "Can't run @Grab: " + ExceptionUtil.getMessage(e);
NOTIFICATION_GROUP.createNotification(title, ExceptionUtil.getThrowableText(e), NotificationType.ERROR, null).notify(project);
return;
}
javaParameters.getClassPath().add(PathUtil.getJarPathForClass(GrapeRunner.class));
javaParameters.getProgramParametersList().add(queries.get(grabText));
javaParameters.setUseDynamicClasspath(true);
lines.put(grabText, javaParameters.toCommandLine());
}
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Processing @Grab Annotations") {