Skip to content

Commit 21514b8

Browse files
cushonCompile-Testing Team
authored andcommitted
Prepare for javac API changes to AST end positions
After the upcoming changes in https://bugs.openjdk.org/browse/JDK-8372948, end positions will be stored directly in each `JCTree`, and the separate `EndPosTable` map will no longer exist. This change uses reflection to support both versions of the internal API, before and after the changes in JDK-8372948. RELNOTES=n/a PiperOrigin-RevId: 862702224
1 parent fba8f37 commit 21514b8

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/main/java/com/google/testing/compile/Parser.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ static ParseResult parse(Iterable<? extends JavaFileObject> sources, String sour
6363
for (JavaFileObject source : sources) {
6464
log.useSource(source);
6565
JavacParser parser =
66-
parserFactory.newParser(
66+
newParser(
67+
parserFactory,
6768
source.getCharContent(false),
6869
/* keepDocComments= */ true,
6970
/* keepEndPos= */ true,
@@ -84,6 +85,31 @@ static ParseResult parse(Iterable<? extends JavaFileObject> sources, String sour
8485
}
8586
}
8687

88+
private static final boolean IS_END_POS_TABLE_PRESENT = getIsEndPosTablePresent();
89+
90+
private static boolean getIsEndPosTablePresent() {
91+
try {
92+
// JDK versions before https://bugs.openjdk.org/browse/JDK-8372948
93+
Class.forName("com.sun.tools.javac.tree.EndPosTable");
94+
return true;
95+
} catch (ClassNotFoundException e) {
96+
return false;
97+
}
98+
}
99+
100+
private static JavacParser newParser(
101+
ParserFactory parserFactory,
102+
CharSequence source,
103+
boolean keepDocComments,
104+
boolean keepEndPos,
105+
boolean keepLineMap) {
106+
if (IS_END_POS_TABLE_PRESENT) {
107+
return parserFactory.newParser(source, keepDocComments, keepEndPos, keepLineMap);
108+
}
109+
return parserFactory.newParser(
110+
source, keepDocComments, keepLineMap, /* parseModuleInfo */ false);
111+
}
112+
87113
/** Returns {@code true} if errors were found while parsing source files. */
88114
private static boolean foundParseErrors(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
89115
return diagnostics.stream().anyMatch(d -> d.getKind().equals(ERROR));

0 commit comments

Comments
 (0)