Skip to content

Commit 4fde1be

Browse files
committedJan 30, 2017
load AppMain as agent for Java >= 1.5
(IDEA-107789)
1 parent 9425b2e commit 4fde1be

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed
 

Diff for: ‎java/execution/impl/src/com/intellij/execution/runners/ProcessProxyFactoryImpl.java

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2000-2016 JetBrains s.r.o.
2+
* Copyright 2000-2017 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import com.intellij.execution.process.ProcessHandler;
2323
import com.intellij.openapi.application.PathManager;
2424
import com.intellij.openapi.diagnostic.Logger;
25+
import com.intellij.openapi.projectRoots.JavaSdkVersion;
2526
import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
2627
import com.intellij.openapi.util.text.StringUtil;
2728
import com.intellij.rt.execution.application.AppMain;
@@ -37,35 +38,31 @@ public ProcessProxy createCommandLineProxy(JavaCommandLine javaCmdLine) throws E
3738
String mainClass = javaParameters.getMainClass();
3839

3940
if (ourMayUseLauncher && mainClass != null) {
40-
String moduleName = javaParameters.getModuleName();
41-
String rtJarPath = JavaSdkUtil.getIdeaRtJarPath();
41+
try {
42+
ProcessProxyImpl proxy = new ProcessProxyImpl(StringUtil.getShortName(mainClass));
4243

43-
if (moduleName == null || new File(rtJarPath).isFile()) {
44-
try {
45-
ProcessProxyImpl proxy = new ProcessProxyImpl(StringUtil.getShortName(mainClass));
44+
String rtJarPath = JavaSdkUtil.getIdeaRtJarPath();
45+
String port = String.valueOf(proxy.getPortNumber());
46+
String binPath = PathManager.getBinPath();
4647

47-
String port = String.valueOf(proxy.getPortNumber());
48-
String binPath = PathManager.getBinPath();
49-
50-
if (moduleName == null) {
51-
JavaSdkUtil.addRtJar(javaParameters.getClassPath());
52-
53-
ParametersList vmParametersList = javaParameters.getVMParametersList();
54-
vmParametersList.defineProperty(AppMain.LAUNCHER_PORT_NUMBER, port);
55-
vmParametersList.defineProperty(AppMain.LAUNCHER_BIN_PATH, binPath);
48+
if (new File(rtJarPath).isFile() && JavaSdkUtil.isAtLeast(javaParameters.getJdk(), JavaSdkVersion.JDK_1_5)) {
49+
javaParameters.getVMParametersList().add("-javaagent:" + rtJarPath + '=' + port + ':' + binPath);
50+
}
51+
else {
52+
JavaSdkUtil.addRtJar(javaParameters.getClassPath());
5653

57-
javaParameters.getProgramParametersList().prepend(mainClass);
58-
javaParameters.setMainClass(AppMain.class.getName());
59-
}
60-
else {
61-
javaParameters.getVMParametersList().add("-javaagent:" + rtJarPath + '=' + port + ':' + binPath);
62-
}
54+
ParametersList vmParametersList = javaParameters.getVMParametersList();
55+
vmParametersList.defineProperty(AppMain.LAUNCHER_PORT_NUMBER, port);
56+
vmParametersList.defineProperty(AppMain.LAUNCHER_BIN_PATH, binPath);
6357

64-
return proxy;
65-
}
66-
catch (Exception e) {
67-
Logger.getInstance(ProcessProxy.class).warn(e);
58+
javaParameters.getProgramParametersList().prepend(mainClass);
59+
javaParameters.setMainClass(AppMain.class.getName());
6860
}
61+
62+
return proxy;
63+
}
64+
catch (Exception e) {
65+
Logger.getInstance(ProcessProxy.class).warn(e);
6966
}
7067
}
7168

Diff for: ‎java/java-impl/src/com/intellij/openapi/projectRoots/ex/JavaSdkUtil.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2000-2015 JetBrains s.r.o.
2+
* Copyright 2000-2017 JetBrains s.r.o.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import com.intellij.openapi.module.Module;
1919
import com.intellij.openapi.project.Project;
2020
import com.intellij.openapi.projectRoots.JavaSdkVersion;
21+
import com.intellij.openapi.projectRoots.JdkUtil;
2122
import com.intellij.openapi.projectRoots.JdkVersionUtil;
2223
import com.intellij.openapi.projectRoots.Sdk;
2324
import com.intellij.openapi.roots.ModuleRootManager;
@@ -27,13 +28,16 @@
2728
import com.intellij.util.PathUtil;
2829
import com.intellij.util.PathsList;
2930
import com.intellij.util.ReflectionUtil;
31+
import org.jetbrains.annotations.Contract;
3032
import org.jetbrains.annotations.NonNls;
3133
import org.jetbrains.annotations.NotNull;
3234
import org.jetbrains.annotations.Nullable;
3335

3436
import java.util.Arrays;
3537
import java.util.List;
3638

39+
import static java.util.jar.Attributes.Name.IMPLEMENTATION_VERSION;
40+
3741
public class JavaSdkUtil {
3842
@NonNls public static final String IDEA_PREPEND_RTJAR = "idea.prepend.rtjar";
3943

@@ -83,4 +87,17 @@ private static Sdk getRelevantJdk(@NotNull Project project, @NotNull Module modu
8387
Sdk moduleJdk = ModuleRootManager.getInstance(module).getSdk();
8488
return moduleJdk == null ? projectJdk : moduleJdk;
8589
}
90+
91+
@Contract("null, _ -> false")
92+
public static boolean isAtLeast(@Nullable Sdk jdk, @NotNull JavaSdkVersion version) {
93+
if (jdk == null) return false;
94+
95+
String sdkVersionString = JdkUtil.getJdkMainAttribute(jdk, IMPLEMENTATION_VERSION);
96+
if (sdkVersionString == null) return false;
97+
98+
JavaSdkVersion sdkVersion = JdkVersionUtil.getVersion(sdkVersionString);
99+
if (sdkVersion == null) return false;
100+
101+
return sdkVersion.isAtLeast(version);
102+
}
86103
}

0 commit comments

Comments
 (0)
Please sign in to comment.