Skip to content
Permalink
Browse files

load AppMain as agent for Java >= 1.5

(IDEA-107789)
  • Loading branch information...
dovchinnikov committed Jan 30, 2017
1 parent 9425b2e commit 4fde1be3df5f7c145f943a969eb261e32bf72ef6
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -22,6 +22,7 @@
import com.intellij.execution.process.ProcessHandler;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.rt.execution.application.AppMain;
@@ -37,35 +38,31 @@ public ProcessProxy createCommandLineProxy(JavaCommandLine javaCmdLine) throws E
String mainClass = javaParameters.getMainClass();

if (ourMayUseLauncher && mainClass != null) {
String moduleName = javaParameters.getModuleName();
String rtJarPath = JavaSdkUtil.getIdeaRtJarPath();
try {
ProcessProxyImpl proxy = new ProcessProxyImpl(StringUtil.getShortName(mainClass));

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

String port = String.valueOf(proxy.getPortNumber());
String binPath = PathManager.getBinPath();

if (moduleName == null) {
JavaSdkUtil.addRtJar(javaParameters.getClassPath());

ParametersList vmParametersList = javaParameters.getVMParametersList();
vmParametersList.defineProperty(AppMain.LAUNCHER_PORT_NUMBER, port);
vmParametersList.defineProperty(AppMain.LAUNCHER_BIN_PATH, binPath);
if (new File(rtJarPath).isFile() && JavaSdkUtil.isAtLeast(javaParameters.getJdk(), JavaSdkVersion.JDK_1_5)) {
javaParameters.getVMParametersList().add("-javaagent:" + rtJarPath + '=' + port + ':' + binPath);
}
else {
JavaSdkUtil.addRtJar(javaParameters.getClassPath());

javaParameters.getProgramParametersList().prepend(mainClass);
javaParameters.setMainClass(AppMain.class.getName());
}
else {
javaParameters.getVMParametersList().add("-javaagent:" + rtJarPath + '=' + port + ':' + binPath);
}
ParametersList vmParametersList = javaParameters.getVMParametersList();
vmParametersList.defineProperty(AppMain.LAUNCHER_PORT_NUMBER, port);
vmParametersList.defineProperty(AppMain.LAUNCHER_BIN_PATH, binPath);

return proxy;
}
catch (Exception e) {
Logger.getInstance(ProcessProxy.class).warn(e);
javaParameters.getProgramParametersList().prepend(mainClass);
javaParameters.setMainClass(AppMain.class.getName());
}

return proxy;
}
catch (Exception e) {
Logger.getInstance(ProcessProxy.class).warn(e);
}
}

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -18,6 +18,7 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.projectRoots.JdkVersionUtil;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
@@ -27,13 +28,16 @@
import com.intellij.util.PathUtil;
import com.intellij.util.PathsList;
import com.intellij.util.ReflectionUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;

import static java.util.jar.Attributes.Name.IMPLEMENTATION_VERSION;

public class JavaSdkUtil {
@NonNls public static final String IDEA_PREPEND_RTJAR = "idea.prepend.rtjar";

@@ -83,4 +87,17 @@ private static Sdk getRelevantJdk(@NotNull Project project, @NotNull Module modu
Sdk moduleJdk = ModuleRootManager.getInstance(module).getSdk();
return moduleJdk == null ? projectJdk : moduleJdk;
}

@Contract("null, _ -> false")
public static boolean isAtLeast(@Nullable Sdk jdk, @NotNull JavaSdkVersion version) {
if (jdk == null) return false;

String sdkVersionString = JdkUtil.getJdkMainAttribute(jdk, IMPLEMENTATION_VERSION);
if (sdkVersionString == null) return false;

JavaSdkVersion sdkVersion = JdkVersionUtil.getVersion(sdkVersionString);
if (sdkVersion == null) return false;

return sdkVersion.isAtLeast(version);
}
}

0 comments on commit 4fde1be

Please sign in to comment.
You can’t perform that action at this time.