Skip to content

Commit

Permalink
DRAFT: Make AST map pre 1.8 versions to 1.8
Browse files Browse the repository at this point in the history
As the compiler mandates 1.8 already it makes sense for the AST to do
the same.
Adjusted tests to fix expectations and not run(at least reduce) pre JLS8
runs.
  • Loading branch information
akurtakov committed Jan 24, 2025
1 parent b47b6be commit 46840fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,6 @@ public static Test suite() {
Method[] methods = c.getMethods();
for (int i = 0, max = methods.length; i < max; i++) {
if (methods[i].getName().startsWith("test")) { //$NON-NLS-1$
suite.addTest(new ASTTest(methods[i].getName(), AST.JLS2));
suite.addTest(new ASTTest(methods[i].getName(), JLS3_INTERNAL));
suite.addTest(new ASTTest(methods[i].getName(), AST.JLS4));
suite.addTest(new ASTTest(methods[i].getName(), getJLS8()));
suite.addTest(new ASTTest(methods[i].getName(), AST_INTERNAL_JLS23));
}
Expand Down Expand Up @@ -1320,46 +1317,31 @@ void genericPropertyListTest(ASTNode node, List children, Property prop) {

@SuppressWarnings("deprecation")
private static int getApiLevel(String s) {
if (s == null)
return AST.JLS12;
switch (s) {
case JavaCore.VERSION_1_2 : return AST.JLS2;
case JavaCore.VERSION_1_3: return AST.JLS3;
case JavaCore.VERSION_1_4: return AST.JLS4;
case JavaCore.VERSION_1_5: return AST.JLS4;
case JavaCore.VERSION_1_6: return AST.JLS4;
case JavaCore.VERSION_1_7: return AST.JLS4;
case JavaCore.VERSION_1_8: return AST.JLS8;
case JavaCore.VERSION_9: return AST.JLS9;
case JavaCore.VERSION_10: return AST.JLS10;
case JavaCore.VERSION_11: return AST.JLS11;
case JavaCore.VERSION_12: return AST.JLS12;
case JavaCore.VERSION_13: return AST.JLS13;
case JavaCore.VERSION_14: return AST.JLS14;
case JavaCore.VERSION_15: return AST.JLS15;
case JavaCore.VERSION_16: return AST.JLS16;
case JavaCore.VERSION_17: return AST.JLS17;
case JavaCore.VERSION_18: return AST.JLS18;
case JavaCore.VERSION_19: return AST.JLS19;
case JavaCore.VERSION_20: return AST.JLS20;
default: return AST.JLS2;
}
try {
int apiLevel = Integer.valueOf(s);
if (AST.isSupportedVersion(apiLevel)) {
return apiLevel;
}
} catch (NumberFormatException e) {
if (s.equals(JavaCore.VERSION_1_8)) {
return AST.JLS8;
}
}
return AST.getJLSLatest();

}
/** @deprecated using deprecated code */
public void testAST() {

assertSame(AST.JLS2, 2);
assertSame(JLS3_INTERNAL, 3);

AST a0 = new AST(); // deprecated, now 3 from JavaCore.defaultOptions
int apiLevelCal = ASTTest.getApiLevel(JavaCore.getDefaultOptions().get(JavaCore.COMPILER_SOURCE));
int apiLevelCal = getApiLevel(JavaCore.getDefaultOptions().get(JavaCore.COMPILER_SOURCE));
assertTrue(a0.apiLevel() == apiLevelCal);
AST a1 = new AST(new HashMap()); // deprecated, but still 2.0
assertTrue(a1.apiLevel() == AST.JLS2);
assertEquals(AST.JLS8,a1.apiLevel());
AST a2 = AST.newAST(AST.JLS2, false);
assertTrue(a2.apiLevel() == AST.JLS2);
assertEquals(AST.JLS8, a2.apiLevel());
AST a3 = AST.newAST(JLS3_INTERNAL, false);
assertTrue(a3.apiLevel() == JLS3_INTERNAL);
assertEquals(AST.JLS8, a3.apiLevel());

// modification count is always non-negative
assertTrue(this.ast.modificationCount() >= 0);
Expand Down
80 changes: 19 additions & 61 deletions org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -1131,36 +1131,10 @@ public AST() {
*/
private AST(int level, boolean previewEnabled) {
this.previewEnabled = previewEnabled;
if (ALL_VERSIONS.contains(level) && !SUPPORTED_VERSIONS.contains(level)) {
level = SUPPORTED_VERSIONS.get(0);
}
switch(level) {
case JLS2_INTERNAL :
case JLS3_INTERNAL :
this.apiLevel = level;
// initialize a scanner
this.scanner = new Scanner(
true /*comment*/,
true /*whitespace*/,
false /*nls*/,
ClassFileConstants.JDK1_3 /*sourceLevel*/,
ClassFileConstants.JDK1_5 /*complianceLevel*/,
null/*taskTag*/,
null/*taskPriorities*/,
true/*taskCaseSensitive*/,
false/*isPreviewEnabled*/);
break;
case JLS4_INTERNAL :
this.apiLevel = level;
// initialize a scanner
this.scanner = new Scanner(
true /*comment*/,
true /*whitespace*/,
false /*nls*/,
ClassFileConstants.JDK1_7 /*sourceLevel*/,
ClassFileConstants.JDK1_7 /*complianceLevel*/,
null/*taskTag*/,
null/*taskPriorities*/,
true/*taskCaseSensitive*/,
false/*isPreviewEnabled*/);
break;
case JLS8_INTERNAL :
this.apiLevel = level;
// initialize a scanner
Expand Down Expand Up @@ -1234,8 +1208,6 @@ private AST(int level, boolean previewEnabled) {
* indicates the api level and source compatibility mode (as per <code>JavaCore</code>) - defaults to 1.3
* <ul>
* <li>
* <code>"1.3"</code> means the source code is as per JDK 1.3 and api level {@link #JLS3}.</li>
* <li><code>"1.4", "1.5", "1.6", "1.7"</code> implies the respective source JDK levels 1.4, 1.5, 1.6, 1.7 and api level {@link #JLS4}.</li>
* <li><code>"1.8"</code> implies the respective source JDK level 1.8 and api level {@link #JLS8}.</li>
* <li><code>"9", "10", "11" up to "23"</code> implies the respective JDK levels 9, 10, 11 up to 23
* and api levels {@link #JLS9}, {@link #JLS10}, {@link #JLS11} up to {@link #JLS23}.</li>
Expand All @@ -1256,22 +1228,8 @@ public AST(Map options) {
this(apiLevelMap.get(options.get(JavaCore.COMPILER_SOURCE)),
JavaCore.ENABLED.equals(options.get(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES)));

long sourceLevel;
long complianceLevel;
switch(this.apiLevel) {
case JLS2_INTERNAL :
case JLS3_INTERNAL :
sourceLevel = ClassFileConstants.JDK1_3;
complianceLevel = ClassFileConstants.JDK1_5;
break;
case JLS4_INTERNAL :
sourceLevel = ClassFileConstants.JDK1_7;
complianceLevel = ClassFileConstants.JDK1_7;
break;
default :
sourceLevel = AST.jdkLevelMap.get(options.get(JavaCore.COMPILER_SOURCE));
complianceLevel = sourceLevel;
}
long sourceLevel = AST.jdkLevelMap.get(options.get(JavaCore.COMPILER_SOURCE));
long complianceLevel = sourceLevel;
this.scanner = new Scanner(
true /*comment*/,
true /*whitespace*/,
Expand All @@ -1286,13 +1244,13 @@ public AST(Map options) {

private static Map<String, Long> getLevelMapTable() {
Map<String, Long> t = new HashMap<>();
t.put(null, ClassFileConstants.JDK1_2);
t.put(JavaCore.VERSION_1_2, ClassFileConstants.JDK1_2);
t.put(JavaCore.VERSION_1_3, ClassFileConstants.JDK1_3);
t.put(JavaCore.VERSION_1_4, ClassFileConstants.JDK1_4);
t.put(JavaCore.VERSION_1_5, ClassFileConstants.JDK1_5);
t.put(JavaCore.VERSION_1_6, ClassFileConstants.JDK1_6);
t.put(JavaCore.VERSION_1_7, ClassFileConstants.JDK1_7);
t.put(null, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_2, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_3, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_4, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_5, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_6, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_7, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_1_8, ClassFileConstants.JDK1_8);
t.put(JavaCore.VERSION_9, ClassFileConstants.JDK9);
t.put(JavaCore.VERSION_10, ClassFileConstants.JDK10);
Expand All @@ -1313,13 +1271,13 @@ private static Map<String, Long> getLevelMapTable() {
}
private static Map<String, Integer> getApiLevelMapTable() {
Map<String, Integer> t = new HashMap<>();
t.put(null, JLS2_INTERNAL);
t.put(JavaCore.VERSION_1_2, JLS2_INTERNAL);
t.put(JavaCore.VERSION_1_3, JLS3_INTERNAL);
t.put(JavaCore.VERSION_1_4, JLS4_INTERNAL);
t.put(JavaCore.VERSION_1_5, JLS4_INTERNAL);
t.put(JavaCore.VERSION_1_6, JLS4_INTERNAL);
t.put(JavaCore.VERSION_1_7, JLS4_INTERNAL);
t.put(null, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_2, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_3, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_4, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_5, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_6, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_7, JLS8_INTERNAL);
t.put(JavaCore.VERSION_1_8, JLS8_INTERNAL);
t.put(JavaCore.VERSION_9, JLS9_INTERNAL);
t.put(JavaCore.VERSION_10, JLS10_INTERNAL);
Expand Down

0 comments on commit 46840fd

Please sign in to comment.