16
16
import java .io .IOException ;
17
17
import java .io .InputStream ;
18
18
import java .io .StreamCorruptedException ;
19
+ import java .lang .Runtime .Version ;
19
20
import java .net .URL ;
20
21
import java .nio .file .Files ;
21
22
import java .nio .file .Path ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
30
31
import java .util .Optional ;
31
- import java .util .regex .Pattern ;
32
+ import java .util .Set ;
33
+ import java .util .Map .Entry ;
34
+ import java .util .stream .Stream ;
32
35
36
+ import org .apache .commons .lang3 .StringUtils ;
33
37
import org .eclipse .buildship .core .BuildConfiguration ;
34
38
import org .eclipse .buildship .core .GradleBuild ;
35
39
import org .eclipse .buildship .core .GradleCore ;
39
43
import org .eclipse .core .runtime .IProgressMonitor ;
40
44
import org .eclipse .core .runtime .IStatus ;
41
45
import org .eclipse .jdt .core .JavaCore ;
46
+ import org .eclipse .jdt .internal .compiler .impl .CompilerOptions ;
47
+ import org .eclipse .jdt .internal .launching .StandardVMType ;
48
+ import org .eclipse .jdt .launching .AbstractVMInstall ;
49
+ import org .eclipse .jdt .launching .IVMInstall ;
50
+ import org .eclipse .jdt .launching .IVMInstallType ;
51
+ import org .eclipse .jdt .launching .JavaRuntime ;
42
52
import org .eclipse .jdt .ls .core .internal .JavaLanguageServerPlugin ;
43
53
import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
54
+ import org .eclipse .jdt .ls .core .internal .RuntimeEnvironment ;
55
+ import org .eclipse .jdt .ls .core .internal .preferences .Preferences ;
44
56
import org .gradle .tooling .model .build .BuildEnvironment ;
45
57
import org .gradle .tooling .model .build .GradleEnvironment ;
46
58
47
59
public class GradleUtils {
48
60
49
- public static String MAX_SUPPORTED_JAVA = JavaCore .VERSION_17 ;
50
61
// see https://github.com/gradle/gradle/pull/17397
51
62
public static String INVALID_TYPE_FIXED_VERSION = "7.2" ;
52
63
// see https://github.com/gradle/gradle/issues/890
@@ -55,11 +66,6 @@ public class GradleUtils {
55
66
56
67
private static final String MESSAGE_DIGEST_ALGORITHM = "SHA-256" ;
57
68
58
- /**
59
- * A pattern to parse annotation processing arguments.
60
- */
61
- private static final Pattern OPTION_PATTERN = Pattern .compile ("-A([^ \\ t\" ']+)" );
62
-
63
69
public static boolean isIncompatible (GradleVersion gradleVersion , String javaVersion ) {
64
70
if (gradleVersion == null || javaVersion == null || javaVersion .isEmpty ()) {
65
71
return false ;
@@ -72,7 +78,13 @@ public static String getHighestSupportedJava(GradleVersion gradleVersion) {
72
78
GradleVersion baseVersion = gradleVersion .getBaseVersion ();
73
79
try {
74
80
// https://docs.gradle.org/current/userguide/compatibility.html
75
- if (baseVersion .compareTo (GradleVersion .version ("7.3" )) >= 0 ) {
81
+ if (baseVersion .compareTo (GradleVersion .version ("8.3" )) >= 0 ) {
82
+ return JavaCore .VERSION_20 ;
83
+ } else if (baseVersion .compareTo (GradleVersion .version ("7.6" )) >= 0 ) {
84
+ return JavaCore .VERSION_19 ;
85
+ } else if (baseVersion .compareTo (GradleVersion .version ("7.5" )) >= 0 ) {
86
+ return JavaCore .VERSION_18 ;
87
+ } else if (baseVersion .compareTo (GradleVersion .version ("7.3" )) >= 0 ) {
76
88
return JavaCore .VERSION_17 ;
77
89
} else if (baseVersion .compareTo (GradleVersion .version ("7.0" )) >= 0 ) {
78
90
return JavaCore .VERSION_16 ;
@@ -91,10 +103,10 @@ public static String getHighestSupportedJava(GradleVersion gradleVersion) {
91
103
} else if (baseVersion .compareTo (GradleVersion .version ("4.3" )) >= 0 ) {
92
104
return JavaCore .VERSION_9 ;
93
105
}
94
- return JavaCore .VERSION_1_8 ;
95
106
} catch (IllegalArgumentException e ) {
96
- return MAX_SUPPORTED_JAVA ;
107
+ // ignore
97
108
}
109
+ return JavaCore .VERSION_1_8 ;
98
110
}
99
111
100
112
public static boolean hasGradleInvalidTypeCodeException (IStatus status , Path projectFolder , IProgressMonitor monitor ) {
@@ -293,4 +305,75 @@ public static void synchronizeAnnotationProcessingConfiguration(IProgressMonitor
293
305
}
294
306
}
295
307
}
308
+
309
+ /**
310
+ * Find the latest JDK but equal or lower than the {@code highestJavaVersion}.
311
+ */
312
+ public static File getJdkToLaunchDaemon (String highestJavaVersion ) {
313
+ if (StringUtils .isBlank (highestJavaVersion )) {
314
+ return null ;
315
+ }
316
+
317
+ Map <String , File > jdks = getAllVmInstalls ();;
318
+ Entry <String , File > selected = null ;
319
+ for (Entry <String , File > jdk : jdks .entrySet ()) {
320
+ String javaVersion = jdk .getKey ();
321
+ if (Version .parse (javaVersion ).compareTo (Version .parse (highestJavaVersion )) <= 0
322
+ && (selected == null || Version .parse (selected .getKey ()).compareTo (Version .parse (javaVersion )) < 0 )) {
323
+ selected = jdk ;
324
+ }
325
+ }
326
+
327
+ return selected == null ? null : selected .getValue ();
328
+ }
329
+
330
+ /**
331
+ * Get all the available JDK installations in the Eclipse VM registry. If multiple installations
332
+ * are found for the same major version, the first found one is return.
333
+ *
334
+ * The results are returned as map, where key is the major version and value is the file instance of
335
+ * the installation.
336
+ */
337
+ private static Map <String , File > getAllVmInstalls () {
338
+ List <IVMInstall > vmList = Stream .of (JavaRuntime .getVMInstallTypes ())
339
+ .map (IVMInstallType ::getVMInstalls )
340
+ .flatMap (Arrays ::stream )
341
+ .toList ();
342
+ Map <String , File > vmInstalls = new HashMap <>();
343
+ for (IVMInstall vmInstall : vmList ) {
344
+ if (vmInstall instanceof AbstractVMInstall vm ) {
345
+ String javaVersion = getMajorJavaVersion (vm .getJavaVersion ());
346
+ if (StringUtils .isBlank (javaVersion ) || vm .getInstallLocation () == null ) {
347
+ continue ;
348
+ }
349
+
350
+ vmInstalls .putIfAbsent (javaVersion , vm .getInstallLocation ());
351
+ }
352
+ }
353
+
354
+ Preferences preferences = JavaLanguageServerPlugin .getPreferencesManager ().getPreferences ();
355
+ Set <RuntimeEnvironment > runtimes = preferences .getRuntimes ();
356
+ for (RuntimeEnvironment runtime : runtimes ) {
357
+ if (StringUtils .isBlank (runtime .getPath ())) {
358
+ continue ;
359
+ }
360
+ File javaHome = new File (runtime .getPath ());
361
+ if (vmInstalls .containsValue (javaHome )) {
362
+ continue ;
363
+ }
364
+
365
+ String javaVersion = new StandardVMType ().readReleaseVersion (javaHome );
366
+ if (StringUtils .isNotBlank (javaVersion )) {
367
+ // the user preference should have higher priority and replace
368
+ // the existing one if the major version is the same.
369
+ vmInstalls .put (getMajorJavaVersion (javaVersion ), javaHome );
370
+ }
371
+ }
372
+
373
+ return vmInstalls ;
374
+ }
375
+
376
+ public static String getMajorJavaVersion (String version ) {
377
+ return CompilerOptions .versionFromJdkLevel (CompilerOptions .versionToJdkLevel (version ));
378
+ }
296
379
}
0 commit comments