@@ -593,7 +593,7 @@ private static String getTypeErasureSignature(CallableDeclaration callableDecl)
593593 Parameter parameter = (Parameter ) param ;
594594 ResolvedType resolvedType = parameter .getType ().resolve ();
595595 if (parameter .isVarArgs ()) {
596- erasureParameterTypes .add (resolvedType .describe () + "[]" );
596+ erasureParameterTypes .add (resolvedType .erasure (). describe () + "[]" );
597597 } else {
598598 erasureParameterTypes .add (resolvedType .erasure ().describe ());
599599 }
@@ -1147,21 +1147,44 @@ private static boolean excludeSourceRoot(Path sourceRoot) {
11471147 return false ;
11481148 }
11491149
1150+ /**
1151+ * Sets up lexical preserving printer for the given compilation unit in a safe manner by checking
1152+ * whether any node in the unit is missing ranges, which can result in exception.
1153+ *
1154+ * @param compilationUnit Compilation unit to be set with lexical preserving printer
1155+ * @return compilation unit set up with lexical preserving printer or the original compilation
1156+ * unit if the unit contains range-missing nodes
1157+ */
1158+ private static CompilationUnit safeLexicalPreservingPrinterSetup (CompilationUnit compilationUnit ) {
1159+ // setup lexical-preserving printer only if CU has no missing-range nodes
1160+ boolean hasNodeWithMissingRange = compilationUnit .findAll (Node .class ).stream ()
1161+ .anyMatch (n -> !n .getRange ().isPresent ());
1162+ if (!hasNodeWithMissingRange ) {
1163+ return LexicalPreservingPrinter .setup (compilationUnit );
1164+ }
1165+ return compilationUnit ;
1166+ }
1167+
11501168 public static Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> extractAll (Path projectRootPath )
11511169 throws IOException {
1152- SymbolSolverCollectionStrategy symbolSolverCollectionStrategy = new SymbolSolverCollectionStrategy ();
1170+ ParserConfiguration config = new ParserConfiguration ()
1171+ .setStoreTokens (true )
1172+ .setAttributeComments (true )
1173+ .setLanguageLevel (ParserConfiguration .LanguageLevel .JAVA_21 );
1174+ SymbolSolverCollectionStrategy symbolSolverCollectionStrategy = new SymbolSolverCollectionStrategy (config );
11531175 ProjectRoot projectRoot = symbolSolverCollectionStrategy .collect (projectRootPath );
11541176 javaSymbolSolver = (JavaSymbolSolver ) symbolSolverCollectionStrategy .getParserConfiguration ()
1155- .setLanguageLevel ( ParserConfiguration . LanguageLevel . JAVA_21 ). getSymbolResolver ().get ();
1177+ .getSymbolResolver ().get ();
11561178 Map <String , JavaCompilationUnit > symbolTable = new LinkedHashMap <>();
11571179 Map <String , List <Problem >> parseProblems = new HashMap <>();
11581180 for (SourceRoot sourceRoot : projectRoot .getSourceRoots ()) {
11591181 if (excludeSourceRoot (sourceRoot .getRoot ())) {
11601182 continue ;
11611183 }
1184+ sourceRoot .setParserConfiguration (config );
11621185 for (ParseResult <CompilationUnit > parseResult : sourceRoot .tryToParse ()) {
11631186 if (parseResult .isSuccessful ()) {
1164- CompilationUnit compilationUnit = LexicalPreservingPrinter . setup (parseResult .getResult ().get ());
1187+ CompilationUnit compilationUnit = safeLexicalPreservingPrinterSetup (parseResult .getResult ().get ());
11651188 symbolTable .put (compilationUnit .getStorage ().get ().getPath ().toString (),
11661189 processCompilationUnit (compilationUnit ));
11671190 } else {
@@ -1181,13 +1204,15 @@ public static Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>>
11811204 combinedTypeSolver .add (new ReflectionTypeSolver ());
11821205
11831206 ParserConfiguration parserConfiguration = new ParserConfiguration ()
1207+ .setStoreTokens (true )
1208+ .setAttributeComments (true )
11841209 .setLanguageLevel (ParserConfiguration .LanguageLevel .JAVA_21 );
11851210 parserConfiguration .setSymbolResolver (new JavaSymbolSolver (combinedTypeSolver ));
11861211
11871212 JavaParser javaParser = new JavaParser (parserConfiguration );
11881213 ParseResult <CompilationUnit > parseResult = javaParser .parse (code );
11891214 if (parseResult .isSuccessful ()) {
1190- CompilationUnit compilationUnit = LexicalPreservingPrinter . setup (parseResult .getResult ().get ());
1215+ CompilationUnit compilationUnit = safeLexicalPreservingPrinterSetup (parseResult .getResult ().get ());
11911216 Log .debug ("Successfully parsed code. Now processing compilation unit" );
11921217 symbolTable .put ("<pseudo-path>" , processCompilationUnit (compilationUnit ));
11931218 } else {
@@ -1216,6 +1241,8 @@ public static Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>>
12161241 .getSymbolResolver ().get ();
12171242 Log .info ("Setting parser language level to JAVA_21" );
12181243 ParserConfiguration parserConfiguration = new ParserConfiguration ()
1244+ .setStoreTokens (true )
1245+ .setAttributeComments (true )
12191246 .setLanguageLevel (ParserConfiguration .LanguageLevel .JAVA_21 );
12201247 parserConfiguration .setSymbolResolver (javaSymbolSolver );
12211248
@@ -1229,7 +1256,7 @@ public static Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>>
12291256 for (Path javaFilePath : javaFilePaths ) {
12301257 ParseResult <CompilationUnit > parseResult = javaParser .parse (javaFilePath );
12311258 if (parseResult .isSuccessful ()) {
1232- CompilationUnit compilationUnit = LexicalPreservingPrinter . setup (parseResult .getResult ().get ());
1259+ CompilationUnit compilationUnit = safeLexicalPreservingPrinterSetup (parseResult .getResult ().get ());
12331260 System .out .println ("Successfully parsed file: " + javaFilePath .toString ());
12341261 symbolTable .put (compilationUnit .getStorage ().get ().getPath ().toString (),
12351262 processCompilationUnit (compilationUnit ));
0 commit comments