Skip to content

Commit e57f699

Browse files
committed
Handle lexical printer exception and revert to default code printer
Signed-off-by: Saurabh Sinha <sinha108@gmail.com>
1 parent 7dbfded commit e57f699

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/com/ibm/cldk/SymbolTable.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.javaparser.ast.stmt.*;
1212
import com.github.javaparser.ast.type.ReferenceType;
1313
import com.github.javaparser.ast.type.Type;
14+
import com.github.javaparser.printer.DefaultPrettyPrinter;
1415
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
1516
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
1617
import com.github.javaparser.resolution.declarations.ResolvedMethodLikeDeclaration;
@@ -554,7 +555,15 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
554555
callableNode.setStartLine(callableDecl.getRange().isPresent() ? callableDecl.getRange().get().begin.line : -1);
555556
callableNode.setEndLine(callableDecl.getRange().isPresent() ? callableDecl.getRange().get().end.line : -1);
556557
callableNode.setReferencedTypes(getReferencedTypes(body));
557-
callableNode.setCode(body.isPresent() ? LexicalPreservingPrinter.print(body.get()) : "");
558+
try {
559+
callableNode.setCode(body.isPresent() ? LexicalPreservingPrinter.print(body.get()) : "");
560+
} catch (UnsupportedOperationException uoe) {
561+
Log.warn("LexicalPreservingPrinter.print() failed on method " + callableDecl.getSignature() +
562+
" of type "+typeName);
563+
Log.warn("Reverting to default printing");
564+
Log.warn(body.get().toString());
565+
callableNode.setCode(body.get().toString());
566+
}
558567
callableNode.setCodeStartLine(body.isPresent()? body.get().getBegin().get().line : -1);
559568

560569
callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));

src/test/java/com/ibm/cldk/SymbolTableTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public void testExtractSingleMissingNodeRange() throws IOException {
5656
Assertions.assertEquals(2, typeDeclaration.size());
5757
}
5858

59+
@Test
60+
public void testExtractSingleDefaultKeywordMethodDecl() throws IOException {
61+
String javaCode = getJavaCodeForTestResource("test-applications/default-keyword-method-decl/IndexExtractor.java");
62+
Map<String, JavaCompilationUnit> symbolTable = SymbolTable.extractSingle(javaCode).getLeft();
63+
Assertions.assertEquals(1, symbolTable.size());
64+
Map<String, Type> typeDeclaration = symbolTable.values().iterator().next().getTypeDeclarations();
65+
Assertions.assertEquals(1, typeDeclaration.size());
66+
Map<String, Callable> callables = typeDeclaration.values().iterator().next().getCallableDeclarations();
67+
Assertions.assertEquals(5, callables.size());
68+
}
69+
5970
@Test
6071
public void testCallSiteArgumentExpression() throws IOException {
6172
String javaCode = getJavaCodeForTestResource("test-applications/generics-varargs-duplicate-signature-test/Validate.java");

0 commit comments

Comments
 (0)