Skip to content

Commit

Permalink
Add AST getAll[Supported]Versions and isSupportedVersion
Browse files Browse the repository at this point in the history
New methods to ease finding all/supported JLS versions. Supported JLS
versions helpers are needed in order ease limiting AST support to what
compiler supports.
  • Loading branch information
akurtakov committed Jan 17, 2025
1 parent 7ddead3 commit a94da3e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
48 changes: 47 additions & 1 deletion org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@ public final class AST {
@Deprecated
public static final int JLS_Latest = JLS_INTERNAL_Latest;

private static final List<Integer> ALL_VERSIONS = List.of(JLS2, JLS3, JLS4, JLS8, JLS9, JLS10, JLS11, JLS12, JLS13, JLS14, JLS15, JLS16, JLS17, JLS18, JLS19, JLS20, JLS21, JLS22, JLS23);
private static final List<Integer> UNSUPPORTED_VERSIONS = List.of(JLS2, JLS3, JLS4);
private static final List<Integer> SUPPORTED_VERSIONS;
static {
List<Integer> temp = new ArrayList<>();
temp.addAll(ALL_VERSIONS);
temp.removeAll(UNSUPPORTED_VERSIONS);
SUPPORTED_VERSIONS = Collections.unmodifiableList(temp);
}


/*
* Must not collide with a value for ICompilationUnit constants
*/
Expand Down Expand Up @@ -1193,7 +1204,7 @@ private AST(int level, boolean previewEnabled) {
false/*isPreviewEnabled*/);
break;
default:
if (level < JLS2_INTERNAL && level > JLS_Latest) {
if (!ALL_VERSIONS.contains(level)) {
throw new IllegalArgumentException("Unsupported JLS level : " + level); //$NON-NLS-1$
}
this.apiLevel = level;
Expand Down Expand Up @@ -4019,4 +4030,39 @@ public boolean isPreviewEnabled() {
public static int getJLSLatest() {
return JLS_INTERNAL_Latest;
}

/**
* Returns all {@link AST}{@code #JLS*} levels in the order of their
* introduction. For e.g., {@link AST#JLS8} appears before {@link AST#JLS10}
*
* @return all available versions
* @since 3.41
*/
public static List<Integer> getAllVersions() {
return ALL_VERSIONS;
}

/**
* Returns all {@link AST}{@code #JLS*} levels fully supported by JDT in the order of their
* introduction. For e.g., {@link AST#JLS8} appears before {@link AST#JLS10}
*
* @return all available versions
* @since 3.41
*/
public static List<Integer> getAllSupportedVersions() {
return SUPPORTED_VERSIONS;
}

/**
* Not all known JLS versions are fully supported by JDT. This method answers if the given Java source
* version is fully supported.
*
* @return {@code true} if the given string represents Java language standard version is fully supported
* @see #getAllSupportedVersions()
* @since 3.41
*/
public static boolean isSupportedVersion(int version) {
return SUPPORTED_VERSIONS.contains(version);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2024 IBM Corporation and others.
* Copyright (c) 2019, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -199,16 +199,9 @@ public static boolean isJavaDocCodeSnippetSupported(int apiLevel) {


public static void checkASTLevel(int level) {
// Clients can use AST.getJLSLatest()
if(level >=AST.JLS8 && level <= AST.getJLSLatest() )
return;
switch (level) {
case AST.JLS2 :
case AST.JLS3 :
case AST.JLS4 :
return;
if (!AST.getAllVersions().contains(level)) {
throw new IllegalArgumentException(Integer.toString(level));
}
throw new IllegalArgumentException(Integer.toString(level));
}

private static final String[] AST_COMPLIANCE_MAP = {"-1","-1",JavaCore.VERSION_1_2, JavaCore.VERSION_1_3, JavaCore.VERSION_1_7, //$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -223,7 +216,7 @@ public static void checkASTLevel(int level) {
* @return JavaCore Option value string corresponding to the ast level
*/
public static String getCompliance(int astLevel) {
if (astLevel < AST.JLS2 && astLevel > AST.getJLSLatest()) return JavaCore.latestSupportedJavaVersion();
if (!AST.getAllVersions().contains(astLevel)) return JavaCore.latestSupportedJavaVersion();
return AST_COMPLIANCE_MAP[astLevel];
}

Expand Down

0 comments on commit a94da3e

Please sign in to comment.