use of com.intellij.openapi.projectRoots.JavaSdkVersion in project intellij-community by JetBrains.
the class NewProjectUtil method applyJdkToProject.
public static void applyJdkToProject(@NotNull Project project, @NotNull Sdk jdk) {
ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
rootManager.setProjectSdk(jdk);
JavaSdkVersion version = JavaSdk.getInstance().getVersion(jdk);
if (version != null) {
LanguageLevel maxLevel = version.getMaxLanguageLevel();
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(ProjectManager.getInstance().getDefaultProject());
LanguageLevelProjectExtension ext = LanguageLevelProjectExtension.getInstance(project);
if (extension.isDefault() || maxLevel.compareTo(ext.getLanguageLevel()) < 0) {
ext.setLanguageLevel(maxLevel);
}
}
}
use of com.intellij.openapi.projectRoots.JavaSdkVersion in project intellij-community by JetBrains.
the class JavaProjectDataService method findJdk.
@Nullable
private static Sdk findJdk(@NotNull JavaSdkVersion version) {
JavaSdk javaSdk = JavaSdk.getInstance();
List<Sdk> javaSdks = ProjectJdkTable.getInstance().getSdksOfType(javaSdk);
Sdk candidate = null;
for (Sdk sdk : javaSdks) {
JavaSdkVersion v = javaSdk.getVersion(sdk);
if (v == version) {
return sdk;
}
if (candidate == null && v != null && version.getMaxLanguageLevel().isAtLeast(version.getMaxLanguageLevel())) {
candidate = sdk;
}
}
return candidate;
}
use of com.intellij.openapi.projectRoots.JavaSdkVersion in project intellij-community by JetBrains.
the class BuildManager method launchBuildProcess.
private OSProcessHandler launchBuildProcess(Project project, final int port, final UUID sessionId, boolean requestProjectPreload) throws ExecutionException {
final String compilerPath;
final String vmExecutablePath;
JavaSdkVersion sdkVersion = null;
final String forcedCompiledJdkHome = Registry.stringValue(COMPILER_PROCESS_JDK_PROPERTY);
if (StringUtil.isEmptyOrSpaces(forcedCompiledJdkHome)) {
// choosing sdk with which the build process should be run
final Pair<Sdk, JavaSdkVersion> pair = getBuildProcessRuntimeSdk(project);
final Sdk projectJdk = pair.first;
sdkVersion = pair.second;
final Sdk internalJdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
// validate tools.jar presence
final JavaSdkType projectJdkType = (JavaSdkType) projectJdk.getSdkType();
if (FileUtil.pathsEqual(projectJdk.getHomePath(), internalJdk.getHomePath())) {
// important: because internal JDK can be either JDK or JRE,
// this is the most universal way to obtain tools.jar path in this particular case
final JavaCompiler systemCompiler = ToolProvider.getSystemJavaCompiler();
if (systemCompiler == null) {
throw new ExecutionException("No system java compiler is provided by the JRE. Make sure tools.jar is present in IntelliJ IDEA classpath.");
}
compilerPath = ClasspathBootstrap.getResourcePath(systemCompiler.getClass());
} else {
compilerPath = projectJdkType.getToolsPath(projectJdk);
if (compilerPath == null) {
throw new ExecutionException("Cannot determine path to 'tools.jar' library for " + projectJdk.getName() + " (" + projectJdk.getHomePath() + ")");
}
}
vmExecutablePath = projectJdkType.getVMExecutablePath(projectJdk);
} else {
compilerPath = new File(forcedCompiledJdkHome, "lib/tools.jar").getAbsolutePath();
vmExecutablePath = new File(forcedCompiledJdkHome, "bin/java").getAbsolutePath();
}
final CompilerConfiguration projectConfig = CompilerConfiguration.getInstance(project);
final CompilerWorkspaceConfiguration config = CompilerWorkspaceConfiguration.getInstance(project);
final GeneralCommandLine cmdLine = new GeneralCommandLine();
cmdLine.setExePath(vmExecutablePath);
//cmdLine.addParameter("-XX:MaxPermSize=150m");
//cmdLine.addParameter("-XX:ReservedCodeCacheSize=64m");
boolean isProfilingMode = false;
String userDefinedHeapSize = null;
final List<String> userAdditionalOptionsList = new SmartList<>();
final String userAdditionalVMOptions = config.COMPILER_PROCESS_ADDITIONAL_VM_OPTIONS;
final boolean userLocalOptionsActive = !StringUtil.isEmptyOrSpaces(userAdditionalVMOptions);
final String additionalOptions = userLocalOptionsActive ? userAdditionalVMOptions : projectConfig.getBuildProcessVMOptions();
if (!StringUtil.isEmptyOrSpaces(additionalOptions)) {
final StringTokenizer tokenizer = new StringTokenizer(additionalOptions, " ", false);
while (tokenizer.hasMoreTokens()) {
final String option = tokenizer.nextToken();
if (StringUtil.startsWithIgnoreCase(option, "-Xmx")) {
if (userLocalOptionsActive) {
userDefinedHeapSize = option;
}
} else {
if ("-Dprofiling.mode=true".equals(option)) {
isProfilingMode = true;
}
userAdditionalOptionsList.add(option);
}
}
}
if (userDefinedHeapSize != null) {
cmdLine.addParameter(userDefinedHeapSize);
} else {
final int heapSize = projectConfig.getBuildProcessHeapSize(JavacConfiguration.getOptions(project, JavacConfiguration.class).MAXIMUM_HEAP_SIZE);
cmdLine.addParameter("-Xmx" + heapSize + "m");
}
if (SystemInfo.isMac && sdkVersion != null && JavaSdkVersion.JDK_1_6.equals(sdkVersion) && Registry.is("compiler.process.32bit.vm.on.mac")) {
// unfortunately -d32 is supported on jdk 1.6 only
cmdLine.addParameter("-d32");
}
cmdLine.addParameter("-Djava.awt.headless=true");
if (sdkVersion != null && sdkVersion.ordinal() < JavaSdkVersion.JDK_1_9.ordinal()) {
//-Djava.endorsed.dirs is not supported in JDK 9+, may result in abnormal process termination
// turn off all jre customizations for predictable behaviour
cmdLine.addParameter("-Djava.endorsed.dirs=\"\"");
}
if (IS_UNIT_TEST_MODE) {
cmdLine.addParameter("-Dtest.mode=true");
}
// always run eclipse compiler in single-threaded mode
cmdLine.addParameter("-Djdt.compiler.useSingleThread=true");
if (requestProjectPreload) {
cmdLine.addParameter("-Dpreload.project.path=" + FileUtil.toCanonicalPath(getProjectPath(project)));
cmdLine.addParameter("-Dpreload.config.path=" + FileUtil.toCanonicalPath(PathManager.getOptionsPath()));
}
final String shouldGenerateIndex = System.getProperty(GlobalOptions.GENERATE_CLASSPATH_INDEX_OPTION);
if (shouldGenerateIndex != null) {
cmdLine.addParameter("-D" + GlobalOptions.GENERATE_CLASSPATH_INDEX_OPTION + "=" + shouldGenerateIndex);
}
cmdLine.addParameter("-D" + GlobalOptions.COMPILE_PARALLEL_OPTION + "=" + Boolean.toString(config.PARALLEL_COMPILATION));
cmdLine.addParameter("-D" + GlobalOptions.REBUILD_ON_DEPENDENCY_CHANGE_OPTION + "=" + Boolean.toString(config.REBUILD_ON_DEPENDENCY_CHANGE));
if (Boolean.TRUE.equals(Boolean.valueOf(System.getProperty("java.net.preferIPv4Stack", "false")))) {
cmdLine.addParameter("-Djava.net.preferIPv4Stack=true");
}
// this will make netty initialization faster on some systems
cmdLine.addParameter("-Dio.netty.initialSeedUniquifier=" + ThreadLocalRandom.getInitialSeedUniquifier());
for (String option : userAdditionalOptionsList) {
cmdLine.addParameter(option);
}
if (isProfilingMode) {
cmdLine.addParameter("-agentlib:yjpagent=disablealloc,delay=10000,sessionname=ExternalBuild");
}
// debugging
int debugPort = -1;
if (myBuildProcessDebuggingEnabled) {
debugPort = Registry.intValue("compiler.process.debug.port");
if (debugPort <= 0) {
try {
debugPort = NetUtils.findAvailableSocketPort();
} catch (IOException e) {
throw new ExecutionException("Cannot find free port to debug build process", e);
}
}
if (debugPort > 0) {
cmdLine.addParameter("-XX:+HeapDumpOnOutOfMemoryError");
cmdLine.addParameter("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + debugPort);
}
}
if (!Registry.is("compiler.process.use.memory.temp.cache")) {
cmdLine.addParameter("-D" + GlobalOptions.USE_MEMORY_TEMP_CACHE_OPTION + "=false");
}
// javac's VM should use the same default locale that IDEA uses in order for javac to print messages in 'correct' language
cmdLine.setCharset(mySystemCharset);
cmdLine.addParameter("-D" + CharsetToolkit.FILE_ENCODING_PROPERTY + "=" + mySystemCharset.name());
cmdLine.addParameter("-D" + JpsGlobalLoader.FILE_TYPES_COMPONENT_NAME_KEY + "=" + FileTypeManagerImpl.getFileTypeComponentName());
String[] propertiesToPass = { "user.language", "user.country", "user.region", PathManager.PROPERTY_PATHS_SELECTOR, "idea.case.sensitive.fs" };
for (String name : propertiesToPass) {
final String value = System.getProperty(name);
if (value != null) {
cmdLine.addParameter("-D" + name + "=" + value);
}
}
cmdLine.addParameter("-D" + PathManager.PROPERTY_HOME_PATH + "=" + PathManager.getHomePath());
cmdLine.addParameter("-D" + PathManager.PROPERTY_CONFIG_PATH + "=" + PathManager.getConfigPath());
cmdLine.addParameter("-D" + PathManager.PROPERTY_PLUGINS_PATH + "=" + PathManager.getPluginsPath());
cmdLine.addParameter("-D" + GlobalOptions.LOG_DIR_OPTION + "=" + FileUtil.toSystemIndependentName(getBuildLogDirectory().getAbsolutePath()));
cmdLine.addParameters(myFallbackJdkParams);
cmdLine.addParameter("-Dio.netty.noUnsafe=true");
final File workDirectory = getBuildSystemDirectory();
//noinspection ResultOfMethodCallIgnored
workDirectory.mkdirs();
final File projectSystemRoot = getProjectSystemDirectory(project);
if (projectSystemRoot != null) {
cmdLine.addParameter("-Djava.io.tmpdir=" + FileUtil.toSystemIndependentName(projectSystemRoot.getPath()) + "/" + TEMP_DIR_NAME);
}
for (BuildProcessParametersProvider provider : project.getExtensions(BuildProcessParametersProvider.EP_NAME)) {
final List<String> args = provider.getVMArguments();
cmdLine.addParameters(args);
}
@SuppressWarnings("UnnecessaryFullyQualifiedName") final Class<?> launcherClass = org.jetbrains.jps.cmdline.Launcher.class;
final List<String> launcherCp = new ArrayList<>();
launcherCp.add(ClasspathBootstrap.getResourcePath(launcherClass));
launcherCp.addAll(BuildProcessClasspathManager.getLauncherClasspath(project));
launcherCp.add(compilerPath);
ClasspathBootstrap.appendJavaCompilerClasspath(launcherCp, shouldIncludeEclipseCompiler(projectConfig));
cmdLine.addParameter("-classpath");
cmdLine.addParameter(classpathToString(launcherCp));
cmdLine.addParameter(launcherClass.getName());
final List<String> cp = ClasspathBootstrap.getBuildProcessApplicationClasspath();
cp.addAll(myClasspathManager.getBuildProcessPluginsClasspath(project));
if (isProfilingMode) {
cp.add(new File(workDirectory, "yjp-controller-api-redist.jar").getPath());
}
cmdLine.addParameter(classpathToString(cp));
cmdLine.addParameter(BuildMain.class.getName());
cmdLine.addParameter(Boolean.valueOf(System.getProperty("java.net.preferIPv6Addresses", "false")) ? "::1" : "127.0.0.1");
cmdLine.addParameter(Integer.toString(port));
cmdLine.addParameter(sessionId.toString());
cmdLine.addParameter(FileUtil.toSystemIndependentName(workDirectory.getPath()));
cmdLine.setWorkDirectory(workDirectory);
try {
ApplicationManager.getApplication().getMessageBus().syncPublisher(BuildManagerListener.TOPIC).beforeBuildProcessStarted(project, sessionId);
} catch (Throwable e) {
LOG.error(e);
}
final OSProcessHandler processHandler = new OSProcessHandler(cmdLine) {
@Override
protected boolean shouldDestroyProcessRecursively() {
return true;
}
@NotNull
@Override
protected BaseOutputReader.Options readerOptions() {
return BaseOutputReader.Options.BLOCKING;
}
};
processHandler.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(ProcessEvent event, Key outputType) {
// re-translate builder's output to idea.log
final String text = event.getText();
if (!StringUtil.isEmptyOrSpaces(text)) {
if (ProcessOutputTypes.SYSTEM.equals(outputType)) {
if (LOG.isDebugEnabled()) {
LOG.debug("BUILDER_PROCESS [" + outputType.toString() + "]: " + text.trim());
}
} else {
LOG.info("BUILDER_PROCESS [" + outputType.toString() + "]: " + text.trim());
}
}
}
});
if (debugPort > 0) {
processHandler.putUserData(COMPILER_PROCESS_DEBUG_PORT, debugPort);
}
return processHandler;
}
use of com.intellij.openapi.projectRoots.JavaSdkVersion in project intellij-community by JetBrains.
the class BuildManager method getRuntimeSdk.
private static Pair<Sdk, JavaSdkVersion> getRuntimeSdk(@NotNull Project project, final JavaSdkVersion oldestPossibleVersion) {
final Set<Sdk> candidates = new LinkedHashSet<>();
final Sdk defaultSdk = ProjectRootManager.getInstance(project).getProjectSdk();
if (defaultSdk != null && defaultSdk.getSdkType() instanceof JavaSdkType) {
candidates.add(defaultSdk);
}
for (Module module : ModuleManager.getInstance(project).getModules()) {
final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
if (sdk != null && sdk.getSdkType() instanceof JavaSdkType) {
candidates.add(sdk);
}
}
// now select the latest version from the sdks that are used in the project, but not older than the internal sdk version
final JavaSdk javaSdkType = JavaSdk.getInstance();
Sdk projectJdk = null;
int sdkMinorVersion = 0;
JavaSdkVersion sdkVersion = null;
for (Sdk candidate : candidates) {
final String vs = candidate.getVersionString();
if (vs != null) {
final JavaSdkVersion candidateVersion = getSdkVersion(javaSdkType, vs);
if (candidateVersion != null) {
final int candidateMinorVersion = getMinorVersion(vs);
if (projectJdk == null) {
sdkVersion = candidateVersion;
sdkMinorVersion = candidateMinorVersion;
projectJdk = candidate;
} else {
final int result = candidateVersion.compareTo(sdkVersion);
if (result > 0 || result == 0 && candidateMinorVersion > sdkMinorVersion) {
sdkVersion = candidateVersion;
sdkMinorVersion = candidateMinorVersion;
projectJdk = candidate;
}
}
}
}
}
if (projectJdk == null || sdkVersion == null || !sdkVersion.isAtLeast(oldestPossibleVersion)) {
final Sdk internalJdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
projectJdk = internalJdk;
sdkVersion = javaSdkType.getVersion(internalJdk);
}
return Pair.create(projectJdk, sdkVersion);
}
use of com.intellij.openapi.projectRoots.JavaSdkVersion in project android by JetBrains.
the class Jdks method getVersion.
@NotNull
private static JavaSdkVersion getVersion(@NotNull String jdkRoot) {
String version = JavaSdk.getInstance().getVersionString(jdkRoot);
if (version == null) {
return JavaSdkVersion.JDK_1_0;
}
JavaSdkVersion sdkVersion = JavaSdk.getInstance().getVersion(version);
return sdkVersion == null ? JavaSdkVersion.JDK_1_0 : sdkVersion;
}
Aggregations