From 42df6e6f9e2fae728774bf0e9d78f2bc64ee1695 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Wed, 19 Jun 2024 11:08:04 +0200 Subject: [PATCH] Add new API for supported/unsupported Java versions - `List getAllJavaProjectVersions()` - all Java versions that could be used for Java projects inside Eclipse. The difference to existing `getAllVersions()` API is that later one knows almost all Java versions ever released and might be used not only in JDT core but also in debugger/PDE area. - `boolean isSupportedJavaProjectVersion(String version)` - differs from existing `isSupportedJavaVersion()` in the same way as explained above - `String getFirstSupportedJavaVersion()` - similar to existing `latestSupportedJavaVersion()` and should return minimal "default" version supported by JDT. API above will be used in JDT UI, Debug (PR's are following), and (most likely) PDE. **Internal** API added in batch compiler `CompilerOptions`: - `getFirstSupportedJavaVersion()` - `getFirstSupportedJdkLevel()` Bumped minor version segments on both core and compiler, even if compiler "only" provides internal API, it is more convenient to have them in-sync. See https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2536 --- .../META-INF/MANIFEST.MF | 2 +- org.eclipse.jdt.core.compiler.batch/pom.xml | 2 +- .../compiler/impl/CompilerOptions.java | 32 ++++++++++ org.eclipse.jdt.core/META-INF/MANIFEST.MF | 4 +- .../model/org/eclipse/jdt/core/JavaCore.java | 59 ++++++++++++++++++- org.eclipse.jdt.core/pom.xml | 2 +- 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/META-INF/MANIFEST.MF b/org.eclipse.jdt.core.compiler.batch/META-INF/MANIFEST.MF index f5d4321072b..fde7acdde60 100644 --- a/org.eclipse.jdt.core.compiler.batch/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.core.compiler.batch/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Main-Class: org.eclipse.jdt.internal.compiler.batch.Main Bundle-ManifestVersion: 2 Bundle-Name: Eclipse Compiler for Java(TM) Bundle-SymbolicName: org.eclipse.jdt.core.compiler.batch -Bundle-Version: 3.38.100.qualifier +Bundle-Version: 3.39.0.qualifier Bundle-ClassPath: . Bundle-Vendor: Eclipse.org Automatic-Module-Name: org.eclipse.jdt.core.compiler.batch diff --git a/org.eclipse.jdt.core.compiler.batch/pom.xml b/org.eclipse.jdt.core.compiler.batch/pom.xml index cc44700614e..0c7d48b0039 100644 --- a/org.eclipse.jdt.core.compiler.batch/pom.xml +++ b/org.eclipse.jdt.core.compiler.batch/pom.xml @@ -17,7 +17,7 @@ 4.33.0-SNAPSHOT org.eclipse.jdt.core.compiler.batch - 3.38.100-SNAPSHOT + 3.39.0-SNAPSHOT eclipse-plugin diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java index e0c2618ee24..b9cbec6e126 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java @@ -35,6 +35,7 @@ import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.Compiler; @@ -258,6 +259,25 @@ public class CompilerOptions { * Note: Whenever a new version is added, make sure getLatestVersion() * is updated with it. */ + + /** + * Unsupported JLS versions + */ + /* + * Note: Whenever a new version is obsoleted, make sure getFirstVersion() is updated. + */ + public static Set UNSUPPORTED_VERSIONS = Set.of( + VERSION_1_1, + VERSION_1_2, + VERSION_1_3, + VERSION_1_4, + VERSION_JSR14, + VERSION_CLDC1_1, + VERSION_1_5, + VERSION_1_6, + VERSION_1_7 + ); + public static final String ERROR = "error"; //$NON-NLS-1$ public static final String WARNING = "warning"; //$NON-NLS-1$ public static final String INFO = "info"; //$NON-NLS-1$ @@ -647,6 +667,18 @@ public CompilerOptions(Map settings, boolean parseLiteralExpress this.parseLiteralExpressionsAsConstants = parseLiteralExpressionsAsConstants; } + /** + * Return the first (oldest) Java language version supported by the Eclipse compiler + */ + public static String getFirstSupportedJavaVersion() { + return VERSION_1_8; + } + /** + * Return the first (oldest) Java language level supported by the Eclipse compiler + */ + public static long getFirstSupportedJdkLevel() { + return ClassFileConstants.JDK1_8; + } /** * Return the latest Java language version supported by the Eclipse compiler */ diff --git a/org.eclipse.jdt.core/META-INF/MANIFEST.MF b/org.eclipse.jdt.core/META-INF/MANIFEST.MF index d22fc322a19..b8dd66cb759 100644 --- a/org.eclipse.jdt.core/META-INF/MANIFEST.MF +++ b/org.eclipse.jdt.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.jdt.core; singleton:=true -Bundle-Version: 3.38.100.qualifier +Bundle-Version: 3.39.0.qualifier Bundle-Activator: org.eclipse.jdt.core.JavaCore Bundle-Vendor: %providerName Bundle-Localization: plugin @@ -47,7 +47,7 @@ Require-Bundle: org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)", org.eclipse.core.filesystem;bundle-version="[1.11.0,2.0.0)", org.eclipse.text;bundle-version="[3.6.0,4.0.0)", org.eclipse.team.core;bundle-version="[3.1.0,4.0.0)";resolution:=optional, - org.eclipse.jdt.core.compiler.batch;bundle-version="3.38.0";visibility:=reexport + org.eclipse.jdt.core.compiler.batch;bundle-version="3.39.0";visibility:=reexport Bundle-RequiredExecutionEnvironment: JavaSE-17 Eclipse-ExtensibleAPI: true Bundle-ActivationPolicy: lazy diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java index 0dc0eb2c53b..1a862caf78a 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java @@ -3322,10 +3322,28 @@ public final class JavaCore extends Plugin { * @category OptionValue */ public static final String VERSION_CLDC_1_1 = "cldc1.1"; //$NON-NLS-1$ - private static List allVersions = Collections.unmodifiableList(Arrays.asList(VERSION_CLDC_1_1, VERSION_1_1, VERSION_1_2, VERSION_1_3, VERSION_1_4, VERSION_1_5, + private static final List allVersions = Collections.unmodifiableList(Arrays.asList(VERSION_CLDC_1_1, VERSION_1_1, VERSION_1_2, VERSION_1_3, VERSION_1_4, VERSION_1_5, VERSION_1_6, VERSION_1_7, VERSION_1_8, VERSION_9, VERSION_10, VERSION_11, VERSION_12, VERSION_13, VERSION_14, VERSION_15, VERSION_16, VERSION_17, VERSION_18, VERSION_19, VERSION_20, VERSION_21, VERSION_22)); + /** + * Unordered set of all Java versions not supported by compiler anymore. + * The values are from {@link JavaCore}{@code #VERSION_*}. + */ + private static final Set UNSUPPORTED_VERSIONS = CompilerOptions.UNSUPPORTED_VERSIONS; + + /** + * Ordered set (from oldest to latest) of all Java versions supported by Eclipse Java projects. + * The values are from {@link JavaCore}{@code #VERSION_*}. + */ + private static final List SUPPORTED_VERSIONS; + static { + ArrayList temp = new ArrayList<>(); + temp.addAll(allVersions); + temp.removeAll(UNSUPPORTED_VERSIONS); + SUPPORTED_VERSIONS = Collections.unmodifiableList(temp); + } + /** * Returns all {@link JavaCore}{@code #VERSION_*} levels in the order of their * introduction. For e.g., {@link JavaCore#VERSION_1_8} appears before {@link JavaCore#VERSION_10} @@ -3337,6 +3355,18 @@ public static List getAllVersions() { return allVersions; } + /** + * Returns all supported (see {@link JavaCore#isSupportedJavaProjectVersion(String)}) Java project versions in the + * order of their introduction. For e.g., {@link JavaCore#VERSION_1_8} appears before {@link JavaCore#VERSION_10} + * + * @return all supported Java project versions + * @see #isSupportedJavaProjectVersion(String) + * @since 3.39 + */ + public static List getAllJavaProjectVersions() { + return SUPPORTED_VERSIONS; + } + /** * Returns whether the given version of Java or Java Runtime is supported * by the Java Development Toolkit. @@ -3352,6 +3382,21 @@ public static boolean isSupportedJavaVersion(String version) { return CompilerOptions.versionToJdkLevel(version, false) > 0; } + /** + * Not all known Java versions are supported by Eclipse Java projects. This method answers if the given version is + * supported for editing and compilation. + * + * @return {@code true} if the given string represents Java language version supported by the Eclipse Java projects + * @see #getAllJavaProjectVersions() + * @since 3.39 + */ + public static boolean isSupportedJavaProjectVersion(String version) { + if(version == null || version.isBlank()) { + return false; + } + return SUPPORTED_VERSIONS.contains(version); + } + /** * Configurable option value: {@value}. * @since 2.0 @@ -6453,6 +6498,18 @@ public static void setOptions(Hashtable newOptions) { public static String latestSupportedJavaVersion() { return allVersions.get(allVersions.size() - 1); } + + /** + * First (oldest) Java language version supported by the Eclipse compiler. + * This is the first entry from {@link JavaCore#getAllJavaProjectVersions()}. + * + * @since 3.39 + * @return first (oldest) Java language version supported by the Eclipse compiler + */ + public static String getFirstSupportedJavaVersion() { + return SUPPORTED_VERSIONS.get(0); + } + /** * Compares two given versions of the Java platform. The versions being compared must both be * one of the supported values mentioned in diff --git a/org.eclipse.jdt.core/pom.xml b/org.eclipse.jdt.core/pom.xml index a72549fd659..dea58410b7c 100644 --- a/org.eclipse.jdt.core/pom.xml +++ b/org.eclipse.jdt.core/pom.xml @@ -17,7 +17,7 @@ 4.33.0-SNAPSHOT org.eclipse.jdt.core - 3.38.100-SNAPSHOT + 3.39.0-SNAPSHOT eclipse-plugin