Skip to content

Commit

Permalink
Update JdtParser to parse Java 14 code and use the Java 14 AST.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696729771
  • Loading branch information
rluble authored and copybara-github committed Nov 15, 2024
1 parent caa0ceb commit bc04f3a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public static String strip(String fileContent, String annotationName) {
}

Map<String, String> compilerOptions = new HashMap<>();
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_11);
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_11);
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_11);
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_14);
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_14);
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_14);

// Parse the file.
ASTParser parser = ASTParser.newParser(AST.JLS11);
ASTParser parser = ASTParser.newParser(AST.JLS14);
parser.setCompilerOptions(compilerOptions);
parser.setResolveBindings(false);
parser.setSource(fileContent.toCharArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,16 +1273,19 @@ private SwitchStatement convert(org.eclipse.jdt.core.dom.SwitchStatement switchS
.build();
}

private SwitchCase.Builder convert(org.eclipse.jdt.core.dom.SwitchCase statement) {
return statement.isDefault()
private SwitchCase.Builder convert(org.eclipse.jdt.core.dom.SwitchCase switchCase) {
return switchCase.isDefault()
? SwitchCase.newBuilder()
: SwitchCase.newBuilder()
// Fold the constant in the switch case to avoid complex expressions. Otherwise JDT
// would represent negative values as unary expressions, e.g - <constant>. The Wasm
// backend relies on switch case constant for switch on integral values to be
// literals.
.setCaseExpressions(
ImmutableList.of(convertAndFoldExpression(statement.getExpression())));
ImmutableList.of(
convertAndFoldExpression(
Iterables.getOnlyElement(
JdtEnvironment.asTypedList(switchCase.expressions())))));
}

private SynchronizedStatement convert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
* into compilation unit.
*/
public class JdtParser {
private static final String JAVA_VERSION = JavaCore.VERSION_11;
private static final int AST_JLS_VERSION = AST.JLS11;
private static final String JAVA_VERSION = JavaCore.VERSION_14;
private static final int AST_JLS_VERSION = AST.JLS14;

private final Map<String, String> compilerOptions = new HashMap<>();
private final Problems problems;
Expand Down

0 comments on commit bc04f3a

Please sign in to comment.