[java] migrates "most recent" SDK detection to the new API

This commit is contained in:
Roman Shevchenko
2018-01-30 11:51:08 +03:00
parent 82576450a7
commit 7ac275f048
3 changed files with 60 additions and 68 deletions
@@ -10,16 +10,23 @@ import com.intellij.ide.wizard.Step;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider;
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.projectImport.ProjectImportBuilder;
import com.intellij.projectImport.ProjectImportProvider;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static java.util.stream.Collectors.*;
public class AddModuleWizard extends AbstractProjectWizard {
private ProjectImportProvider[] myImportProviders;
@@ -96,9 +103,20 @@ public class AddModuleWizard extends AbstractProjectWizard {
@Nullable
public static Sdk getMostRecentSuitableSdk(final WizardContext context) {
if (context.getProject() == null) {
List<Sdk> sdks = Arrays.asList(ProjectJdkTable.getInstance().getAllJdks());
ProjectBuilder builder = context.getProjectBuilder();
return ProjectJdkTable.getInstance().findMostRecentSdk(sdk -> builder == null || builder.isSuitableSdkType(sdk.getSdkType()));
if (builder != null) {
sdks = ContainerUtil.filter(sdks, sdk -> builder.isSuitableSdkType(sdk.getSdkType()));
}
Map<SdkTypeId, List<Sdk>> sdksByType = sdks.stream().collect(groupingBy(Sdk::getSdkType, mapping(Function.identity(), toList())));
Map.Entry<SdkTypeId, List<Sdk>> pair = ContainerUtil.getFirstItem(sdksByType.entrySet());
if (pair != null) {
return pair.getValue().stream().max(pair.getKey().versionComparator()).orElse(null);
}
}
return null;
}
@@ -119,7 +137,7 @@ public class AddModuleWizard extends AbstractProjectWizard {
* to return {@code true} for the step to go to
* @return {@code true} if current wizard is navigated to the target step; {@code false} otherwise
*/
public boolean navigateToStep(@NotNull Function<Step, Boolean> filter) {
public boolean navigateToStep(@NotNull com.intellij.util.Function<Step, Boolean> filter) {
for (int i = 0, myStepsSize = mySteps.size(); i < myStepsSize; i++) {
ModuleWizardStep step = mySteps.get(i);
if (filter.fun(step) != Boolean.TRUE) {
@@ -1,18 +1,4 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.externalSystem.service.execution;
import com.intellij.openapi.application.ApplicationManager;
@@ -23,13 +9,17 @@ import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.EnvironmentUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.stream.Stream;
import static com.intellij.openapi.util.Pair.pair;
public class ExternalSystemJdkUtil {
public static final String USE_INTERNAL_JAVA = "#JAVA_INTERNAL";
public static final String USE_PROJECT_JDK = "#USE_PROJECT_JDK";
@@ -82,33 +72,34 @@ public class ExternalSystemJdkUtil {
@NotNull
public static Pair<String, Sdk> getAvailableJdk(@Nullable Project project) throws ExternalSystemJdkException {
Condition<Sdk> sdkCondition = sdk -> sdk != null && sdk.getSdkType() == JavaSdk.getInstance() && isValidJdk(sdk.getHomePath());
if (project != null) {
Sdk res = ProjectRootManager.getInstance(project).getProjectSdk();
if (sdkCondition.value(res)) return Pair.create(USE_PROJECT_JDK, res);
JavaSdk javaSdkType = JavaSdk.getInstance();
Module[] modules = ModuleManager.getInstance(project).getModules();
for (Module module : modules) {
Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
if (sdkCondition.value(res)) {
return Pair.create(USE_PROJECT_JDK, sdk);
}
if (project != null) {
Stream<Sdk> projectSdks = Stream.concat(
Stream.of(ProjectRootManager.getInstance(project).getProjectSdk()),
Stream.of(ModuleManager.getInstance(project).getModules()).map(module -> ModuleRootManager.getInstance(module).getSdk()));
Sdk projectSdk = projectSdks
.filter(sdk -> sdk != null && sdk.getSdkType() == javaSdkType && isValidJdk(sdk.getHomePath()))
.findFirst().orElse(null);
if (projectSdk != null) {
return pair(USE_PROJECT_JDK, projectSdk);
}
}
Sdk mostRecentSdk = ProjectJdkTable.getInstance().findMostRecentSdk(sdkCondition);
List<Sdk> allJdks = ProjectJdkTable.getInstance().getSdksOfType(javaSdkType);
Sdk mostRecentSdk = allJdks.stream().filter(sdk -> isValidJdk(sdk.getHomePath())).max(javaSdkType.versionComparator()).orElse(null);
if (mostRecentSdk != null) {
return Pair.create(mostRecentSdk.getName(), mostRecentSdk);
return pair(mostRecentSdk.getName(), mostRecentSdk);
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
String javaHome = EnvironmentUtil.getEnvironmentMap().get("JAVA_HOME");
if (isValidJdk(javaHome)) {
return Pair.create(USE_JAVA_HOME, JavaSdk.getInstance().createJdk("", javaHome));
return pair(USE_JAVA_HOME, javaSdkType.createJdk("", javaHome));
}
}
return Pair.create(USE_INTERNAL_JAVA, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk());
return pair(USE_INTERNAL_JAVA, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk());
}
/** @deprecated trivial (to be removed in IDEA 2019) */
@@ -20,11 +20,9 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
@@ -42,17 +40,13 @@ import javax.swing.*;
import java.io.File;
import java.util.Arrays;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @since 4/15/13 2:29 PM
*/
public class GradleProjectImportBuilder extends AbstractExternalProjectImportBuilder<ImportFromGradleControl> {
private static final Pattern JAVA_VERSION = Pattern.compile("java version \"(\\d.*)\"");
/**
* @deprecated use {@link GradleProjectImportBuilder#GradleProjectImportBuilder(ProjectDataManager)}
*/
@@ -78,40 +72,40 @@ public class GradleProjectImportBuilder extends AbstractExternalProjectImportBui
@Nullable
@Override
protected Sdk resolveProjectJdk(@NotNull WizardContext context) {
JavaSdk javaSdkType = JavaSdk.getInstance();
ProjectJdkTable jdkTable = ProjectJdkTable.getInstance();
// gradle older than 4.2.1 doesn't support new java the version number format like 9.0.1, see https://github.com/gradle/gradle/issues/2992
Condition<Sdk> sdkCondition = sdk -> {
String version = getVersion(sdk);
return StringUtil.compareVersionNumbers(version, "1.6") > 0 &&
StringUtil.compareVersionNumbers(version, "9") < 0 &&
Predicate<Sdk> sdkCondition = sdk -> {
JavaSdkVersion v = javaSdkType.getVersion(sdk);
return v != null && v.isAtLeast(JavaSdkVersion.JDK_1_6) && !v.isAtLeast(JavaSdkVersion.JDK_1_9) &&
ExternalSystemJdkUtil.isValidJdk(sdk.getHomePath());
};
Sdk mostRecentSdk = ProjectJdkTable.getInstance().findMostRecentSdk(
sdk -> sdk.getSdkType() == JavaSdk.getInstance() && sdkCondition.value(sdk));
Sdk mostRecentSdk = jdkTable.getSdksOfType(javaSdkType).stream().filter(sdkCondition).max(javaSdkType.versionComparator()).orElse(null);
if (mostRecentSdk != null) {
return mostRecentSdk;
}
Set<String> existingPaths =
new THashSet<>(Arrays.stream(ProjectJdkTable.getInstance().getAllJdks()).map(sdk -> sdk.getHomePath()).collect(Collectors.toSet()),
FileUtil.PATH_HASHING_STRATEGY);
for (String javaHome : JavaSdk.getInstance().suggestHomePaths()) {
Set<String> existingPaths = Arrays.stream(jdkTable.getAllJdks())
.map(sdk -> sdk.getHomePath())
.collect(Collectors.toCollection(() -> new THashSet<>(FileUtil.PATH_HASHING_STRATEGY)));
for (String javaHome : javaSdkType.suggestHomePaths()) {
if (!existingPaths.contains(FileUtil.toCanonicalPath(javaHome))) {
JavaSdk javaSdk = JavaSdk.getInstance();
Sdk jdk = javaSdk.createJdk(ObjectUtils.notNull(javaSdk.suggestSdkName(null, javaHome), ""), javaHome);
if (sdkCondition.value(jdk)) {
ApplicationManager.getApplication().runWriteAction(() -> ProjectJdkTable.getInstance().addJdk(jdk));
Sdk jdk = javaSdkType.createJdk(ObjectUtils.notNull(javaSdkType.suggestSdkName(null, javaHome), ""), javaHome);
if (sdkCondition.test(jdk)) {
ApplicationManager.getApplication().runWriteAction(() -> jdkTable.addJdk(jdk));
return jdk;
}
}
}
Project project = context.getProject() != null ? context.getProject() : ProjectManager.getInstance().getDefaultProject();
final Pair<String, Sdk> sdkPair = ExternalSystemJdkUtil.getAvailableJdk(project);
Pair<String, Sdk> sdkPair = ExternalSystemJdkUtil.getAvailableJdk(project);
if (!ExternalSystemJdkUtil.USE_INTERNAL_JAVA.equals(sdkPair.first)) {
return sdkPair.second;
}
return null;
}
@@ -215,15 +209,4 @@ public class GradleProjectImportBuilder extends AbstractExternalProjectImportBui
public Project createProject(String name, String path) {
return ExternalProjectsManagerImpl.setupCreatedProject(super.createProject(name, path));
}
@Nullable
private static String getVersion(Sdk sdk) {
String versionString = sdk.getVersionString();
if (versionString == null) return null;
Matcher matcher = JAVA_VERSION.matcher(versionString.trim());
if (matcher.matches()) {
return matcher.group(1);
}
return versionString;
}
}
}