Skip to content

Commit 31e4ac7

Browse files
amaembointellij-monorepo-bot
authored andcommitted
DefaultJdkConfigurator is not component anymore
It's necessary to configure JDK before creating a new project on the new project wizard, so we just trigger this configuration in ProjectSdksModel#reset via ProjectJdkTable#preconfigure. A DefaultJdkConfigurator service now used to detect the best JDK on current system. We could omit this service, but Android Studio wants to disable this. GitOrigin-RevId: 1ac74ec06d17a571abece70cb4de1ac9bacd940b
1 parent 2be1161 commit 31e4ac7

File tree

7 files changed

+71
-47
lines changed

7 files changed

+71
-47
lines changed

java/java-impl/src/META-INF/JavaPlugin.xml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@
3838
<xi:fallback/>
3939
</xi:include>
4040

41-
<application-components>
42-
<component>
43-
<implementation-class>com.intellij.openapi.projectRoots.impl.DefaultJdkConfigurator</implementation-class>
44-
<headless-implementation-class/>
45-
</component>
46-
</application-components>
47-
4841
<project-components>
4942
<component>
5043
<implementation-class>com.intellij.framework.detection.impl.FrameworkDetectionManager</implementation-class>
@@ -397,6 +390,9 @@
397390
<projectService serviceInterface="com.intellij.facet.impl.ui.FacetEditorsStateManager"
398391
serviceImplementation="com.intellij.facet.impl.ui.FacetEditorsStateManagerImpl"/>
399392

393+
<applicationService serviceInterface="com.intellij.openapi.projectRoots.DefaultJdkConfigurator"
394+
serviceImplementation="com.intellij.openapi.projectRoots.impl.DefaultJdkConfiguratorImpl"/>
395+
400396
<applicationService serviceInterface="com.intellij.facet.ui.FacetEditorsFactory"
401397
serviceImplementation="com.intellij.facet.impl.ui.FacetEditorsFactoryImpl"/>
402398

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2000-2020 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.
2+
package com.intellij.openapi.projectRoots;
3+
4+
import org.jetbrains.annotations.Nullable;
5+
6+
public interface DefaultJdkConfigurator {
7+
/**
8+
* Guess the best JDK location on this system
9+
*
10+
* @return absolute path to JDK home, or null if nothing is found
11+
*/
12+
@Nullable String guessJavaHome();
13+
}

java/java-impl/src/com/intellij/openapi/projectRoots/impl/DefaultJdkConfigurator.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2000-2020 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.
2+
package com.intellij.openapi.projectRoots.impl;
3+
4+
import com.intellij.openapi.projectRoots.DefaultJdkConfigurator;
5+
import com.intellij.openapi.projectRoots.JavaSdk;
6+
import com.intellij.util.containers.ContainerUtil;
7+
8+
import java.util.Collection;
9+
10+
final class DefaultJdkConfiguratorImpl implements DefaultJdkConfigurator {
11+
@Override
12+
public String guessJavaHome() {
13+
Collection<String> homePaths = JavaSdk.getInstance().suggestHomePaths();
14+
return ContainerUtil.getFirstItem(homePaths);
15+
}
16+
}

java/java-impl/src/com/intellij/openapi/projectRoots/impl/JavaAwareProjectJdkTableImpl.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2000-2019 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.
22
package com.intellij.openapi.projectRoots.impl;
33

4+
import com.intellij.ide.util.PropertiesComponent;
5+
import com.intellij.openapi.application.ApplicationManager;
46
import com.intellij.openapi.application.WriteAction;
57
import com.intellij.openapi.components.ServiceManager;
68
import com.intellij.openapi.projectRoots.*;
@@ -12,14 +14,41 @@
1214
import org.jetbrains.jps.model.java.JdkVersionDetector;
1315

1416
import java.io.File;
17+
import java.util.List;
1518

1619
public final class JavaAwareProjectJdkTableImpl extends ProjectJdkTableImpl {
20+
private static final String DEFAULT_JDK_CONFIGURED = "defaultJdkConfigured";
21+
1722
public static JavaAwareProjectJdkTableImpl getInstanceEx() {
1823
return (JavaAwareProjectJdkTableImpl)ServiceManager.getService(ProjectJdkTable.class);
1924
}
2025

2126
private Sdk myInternalJdk;
2227

28+
@Override
29+
public void preconfigure() {
30+
PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
31+
if (propertiesComponent.getBoolean(DEFAULT_JDK_CONFIGURED, false) ||
32+
ApplicationManager.getApplication().isUnitTestMode()) {
33+
return;
34+
}
35+
36+
JavaSdk javaSdk = JavaSdk.getInstance();
37+
List<Sdk> jdks = getSdksOfType(javaSdk);
38+
if (jdks.isEmpty()) {
39+
String homePath = ServiceManager.getService(DefaultJdkConfigurator.class).guessJavaHome();
40+
if (homePath != null && javaSdk.isValidSdkHome(homePath)) {
41+
String suggestedName = JdkUtil.suggestJdkName(javaSdk.getVersionString(homePath));
42+
if (suggestedName != null) {
43+
ApplicationManager.getApplication().runWriteAction(
44+
() -> addJdk(javaSdk.createJdk(suggestedName, homePath, false))
45+
);
46+
}
47+
}
48+
}
49+
propertiesComponent.setValue(DEFAULT_JDK_CONFIGURED, true);
50+
}
51+
2352
/**
2453
* @deprecated Bundled JDK must not be used. See IDEA-225960"
2554
*/

platform/lang-impl/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ProjectSdksModel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public void syncSdks() {
9595

9696
public void reset(@Nullable Project project) {
9797
myProjectSdks.clear();
98-
final Sdk[] projectSdks = ProjectJdkTable.getInstance().getAllJdks();
98+
ProjectJdkTable jdkTable = ProjectJdkTable.getInstance();
99+
jdkTable.preconfigure();
100+
final Sdk[] projectSdks = jdkTable.getAllJdks();
99101
for (Sdk sdk : projectSdks) {
100102
try {
101103
Sdk editable = (Sdk)sdk.clone();

platform/projectModel-api/src/com/intellij/openapi/projectRoots/ProjectJdkTable.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,12 @@ public static class Adapter implements Listener {
8585
@NotNull
8686
public abstract Sdk createSdk(@NotNull String name, @NotNull SdkTypeId sdkType);
8787

88+
/**
89+
* This method may automatically detect Sdk if none are configured.
90+
*/
91+
public void preconfigure() {
92+
93+
}
94+
8895
public static final Topic<Listener> JDK_TABLE_TOPIC = Topic.create("Project JDK table", Listener.class);
8996
}

0 commit comments

Comments
 (0)