From 9a65777129a6aa1f6bcfa56356d935144459f5ca Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 12 May 2017 15:12:07 +0200 Subject: [PATCH] [java] common methods for JRE/JDK check --- .../compiler/server/BuildManager.java | 25 +++--- .../execution/applet/AppletConfiguration.java | 12 +-- .../projectRoots/impl/JavaSdkImpl.java | 2 +- .../openapi/projectRoots/JavaSdk.java | 4 +- .../intellij/testFramework/IdeaTestUtil.java | 4 +- .../execution/ExternalSystemJdkUtil.java | 83 +++++++------------ .../projectRoots/SimpleJavaSdkType.java | 8 +- .../testGuiFramework/idea/IdeaGuiTestUtil.kt | 2 +- .../service/GradleInstallationManager.java | 7 +- 9 files changed, 58 insertions(+), 89 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java index 3a79cf39173c..a673c8aa4424 100644 --- a/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java +++ b/java/compiler/impl/src/com/intellij/compiler/server/BuildManager.java @@ -55,10 +55,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.project.ProjectManagerListener; import com.intellij.openapi.project.ProjectUtil; -import com.intellij.openapi.projectRoots.JavaSdk; -import com.intellij.openapi.projectRoots.JavaSdkType; -import com.intellij.openapi.projectRoots.JavaSdkVersion; -import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.openapi.projectRoots.*; import com.intellij.openapi.projectRoots.ex.JavaSdkUtil; import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl; import com.intellij.openapi.roots.*; @@ -335,18 +332,16 @@ public class BuildManager implements Disposable { @Nullable private static String getFallbackSdkHome() { - final String home = SystemProperties.getJavaHome(); // should point either to jre or jdk - if (home == null) { - return null; - } - File javaHome = new File(home); - if (!JavaSdk.checkForJdk(javaHome)) { - final File parent = javaHome.getParentFile(); - if (parent != null && JavaSdk.checkForJdk(parent)) { - javaHome = parent; + String home = SystemProperties.getJavaHome(); // should point either to jre or jdk + if (home == null) return null; + + if (!JdkUtil.checkForJdk(home)) { + String parent = new File(home).getParent(); + if (parent != null && JdkUtil.checkForJdk(parent)) { + home = parent; } } - return FileUtil.toSystemIndependentName(javaHome.getAbsolutePath()); + return FileUtil.toSystemIndependentName(home); } private List getOpenProjects() { @@ -502,7 +497,7 @@ public class BuildManager implements Disposable { myAutoMakeTask.schedule(); } } - + @NotNull private static String getThreadTrace(Thread thread, final int depth) { // debugging final StringBuilder buf = new StringBuilder(); diff --git a/java/execution/impl/src/com/intellij/execution/applet/AppletConfiguration.java b/java/execution/impl/src/com/intellij/execution/applet/AppletConfiguration.java index 70b271959e16..4914c6ce3d05 100644 --- a/java/execution/impl/src/com/intellij/execution/applet/AppletConfiguration.java +++ b/java/execution/impl/src/com/intellij/execution/applet/AppletConfiguration.java @@ -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. @@ -26,7 +26,7 @@ import com.intellij.execution.util.JavaParametersUtil; import com.intellij.openapi.module.Module; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.project.Project; -import com.intellij.openapi.projectRoots.JavaSdk; +import com.intellij.openapi.projectRoots.JdkUtil; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.WriteExternalException; @@ -250,12 +250,8 @@ public class AppletConfiguration extends ModuleBasedConfiguration paths = ContainerUtil.packNullables(javaHome, new File(javaHome).getParent(), System.getenv("JDK_16_x64"), System.getenv("JDK_16")); for (String path : paths) { - if (JdkUtil.checkForJdk(new File(path))) { + if (JdkUtil.checkForJdk(path)) { return path; } } diff --git a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java index a3a066af43bd..55afe98ed0fb 100644 --- a/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java +++ b/platform/external-system-impl/src/com/intellij/openapi/externalSystem/service/execution/ExternalSystemJdkUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 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. @@ -25,17 +25,13 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.File; - public class ExternalSystemJdkUtil { - - @NonNls public static final String USE_INTERNAL_JAVA = "#JAVA_INTERNAL"; - @NonNls public static final String USE_PROJECT_JDK = "#USE_PROJECT_JDK"; - @NonNls public static final String USE_JAVA_HOME = "#JAVA_HOME"; + public static final String USE_INTERNAL_JAVA = "#JAVA_INTERNAL"; + public static final String USE_PROJECT_JDK = "#USE_PROJECT_JDK"; + public static final String USE_JAVA_HOME = "#JAVA_HOME"; @Nullable public static Sdk getJdk(@Nullable Project project, @Nullable String jdkName) throws ExternalSystemJdkException { @@ -59,50 +55,31 @@ public class ExternalSystemJdkUtil { if (project == null) { Sdk recent = ProjectJdkTable.getInstance().findMostRecentSdkOfType(JavaSdk.getInstance()); - if (recent != null) return recent; - - return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk(); + return recent != null ? recent : JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk(); } throw new ProjectJdkNotFoundException(); } if (USE_JAVA_HOME.equals(jdkName)) { - final String javaHome = System.getenv("JAVA_HOME"); - if (StringUtil.isEmptyOrSpaces(javaHome)) { - throw new UndefinedJavaHomeException(); - } - - if (JdkUtil.checkForJdk(new File(javaHome)) || JdkUtil.checkForJre(javaHome)) { - final Sdk jdk = JavaSdk.getInstance().createJdk("", javaHome); - if (jdk == null) { - throw new InvalidJavaHomeException(javaHome); - } - return jdk; - } - else { - throw new InvalidJavaHomeException(javaHome); - } + String javaHome = System.getenv("JAVA_HOME"); + if (StringUtil.isEmptyOrSpaces(javaHome)) throw new UndefinedJavaHomeException(); + if (!isValidJdk(javaHome)) throw new InvalidJavaHomeException(javaHome); + return JavaSdk.getInstance().createJdk("", javaHome); } - for (Sdk projectJdk : ProjectJdkTable.getInstance().getAllJdks()) { - if (projectJdk.getName().equals(jdkName)) { - if (projectJdk.getHomePath() != null && - (JdkUtil.checkForJdk(new File(projectJdk.getHomePath())) || JdkUtil.checkForJre(projectJdk.getHomePath()))) { - return projectJdk; - } - else { - throw new InvalidSdkException(projectJdk.getHomePath()); - } - } + Sdk projectJdk = ProjectJdkTable.getInstance().findJdk(jdkName); + if (projectJdk != null) { + String homePath = projectJdk.getHomePath(); + if (!isValidJdk(homePath)) throw new InvalidSdkException(homePath); + return projectJdk; } return null; } - @Nullable + @NotNull public static Pair getAvailableJdk(@Nullable Project project) throws ExternalSystemJdkException { - if (project != null) { Sdk res = ProjectRootManager.getInstance(project).getProjectSdk(); if (res != null) return Pair.create(USE_PROJECT_JDK, res); @@ -110,39 +87,39 @@ public class ExternalSystemJdkUtil { Module[] modules = ModuleManager.getInstance(project).getModules(); for (Module module : modules) { Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); - if (sdk != null && sdk.getSdkType() instanceof JavaSdkType) return Pair.create(USE_PROJECT_JDK, sdk); + if (sdk != null && sdk.getSdkType() instanceof JavaSdkType) { + return Pair.create(USE_PROJECT_JDK, sdk); + } } } if (!ApplicationManager.getApplication().isUnitTestMode()) { - final String javaHome = System.getenv("JAVA_HOME"); - if (!StringUtil.isEmptyOrSpaces(javaHome) && (JdkUtil.checkForJdk(new File(javaHome)) || JdkUtil.checkForJre(javaHome))) { + String javaHome = System.getenv("JAVA_HOME"); + if (isValidJdk(javaHome)) { return Pair.create(USE_JAVA_HOME, JavaSdk.getInstance().createJdk("", javaHome)); } } for (Sdk projectJdk : ProjectJdkTable.getInstance().getAllJdks()) { - if (projectJdk.getHomePath() != null && - (JdkUtil.checkForJdk(new File(projectJdk.getHomePath())) || JdkUtil.checkForJre(projectJdk.getHomePath()))) { + if (isValidJdk(projectJdk.getHomePath())) { return Pair.create(projectJdk.getName(), projectJdk); } } - final Sdk internalJdk = JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk(); - if (internalJdk != null) { - return Pair.create(USE_INTERNAL_JAVA, internalJdk); - } - - return null; + return Pair.create(USE_INTERNAL_JAVA, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk()); } + /** @deprecated trivial (to be removed in IDEA 2019) */ public static boolean checkForJdk(@NotNull Project project, @Nullable String jdkName) { try { final Sdk sdk = getJdk(project, jdkName); - return sdk != null && sdk.getHomePath() != null && JdkUtil.checkForJdk(new File(sdk.getHomePath())); - } - catch (ExternalSystemJdkException ignore) { + return sdk != null && sdk.getHomePath() != null && JdkUtil.checkForJdk(sdk.getHomePath()); } + catch (ExternalSystemJdkException ignore) { } return false; } -} + + private static boolean isValidJdk(@Nullable String homePath) { + return !StringUtil.isEmptyOrSpaces(homePath) && (JdkUtil.checkForJdk(homePath) || JdkUtil.checkForJre(homePath)); + } +} \ No newline at end of file diff --git a/platform/lang-api/src/com/intellij/openapi/projectRoots/SimpleJavaSdkType.java b/platform/lang-api/src/com/intellij/openapi/projectRoots/SimpleJavaSdkType.java index dac1d63ce4dd..8c4797797418 100644 --- a/platform/lang-api/src/com/intellij/openapi/projectRoots/SimpleJavaSdkType.java +++ b/platform/lang-api/src/com/intellij/openapi/projectRoots/SimpleJavaSdkType.java @@ -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. @@ -93,7 +93,7 @@ public class SimpleJavaSdkType extends SdkType implements JavaSdkType { @Override public boolean isValidSdkHome(String path) { - return JdkUtil.checkForJdk(new File(path)); + return JdkUtil.checkForJdk(path); } @Override @@ -101,10 +101,8 @@ public class SimpleJavaSdkType extends SdkType implements JavaSdkType { return currentSdkName; } - @Override public final String getVersionString(final String sdkHome) { return SdkVersionUtil.detectJdkVersion(sdkHome); } - -} +} \ No newline at end of file diff --git a/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt b/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt index 86c5569ea15f..13a47a69e5c3 100644 --- a/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt +++ b/platform/testGuiFramework/src/com/intellij/testGuiFramework/idea/IdeaGuiTestUtil.kt @@ -59,7 +59,7 @@ object IdeaGuiTestUtil{ } fun setJdkPath(path: File) { - if (JavaSdk.checkForJdk(path)) { + if (JdkUtil.checkForJdk(path)) { ApplicationManager.getApplication().assertWriteAccessAllowed() var chosenJdk: Sdk? = null diff --git a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/GradleInstallationManager.java b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/GradleInstallationManager.java index 2e8f849e3bb0..a89ccbff67bb 100644 --- a/plugins/gradle/src/org/jetbrains/plugins/gradle/service/GradleInstallationManager.java +++ b/plugins/gradle/src/org/jetbrains/plugins/gradle/service/GradleInstallationManager.java @@ -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. @@ -162,13 +162,14 @@ public class GradleInstallationManager { linkedProjectPath, null, OpenExternalSystemSettingsCallback.ID); } - final File sdkHomePath = sdk != null && sdk.getHomePath() != null ? new File(sdk.getHomePath()) : null; - if (sdkHomePath != null && JdkUtil.checkForJre(sdkHomePath.getPath()) && !JdkUtil.checkForJdk(sdkHomePath)) { + String sdkHomePath = sdk != null ? sdk.getHomePath() : null; + if (sdkHomePath != null && JdkUtil.checkForJre(sdkHomePath) && !JdkUtil.checkForJdk(sdkHomePath)) { throw new ExternalSystemJdkException( String.format("Please, use JDK instead of JRE for Gradle importer. Open Gradle Settings \n", OpenExternalSystemSettingsCallback.ID), linkedProjectPath, null, OpenExternalSystemSettingsCallback.ID); } + return sdk; }