From 14de07478274d854ed353159132ea0f54b616f1d Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Fri, 10 Nov 2023 17:39:04 -0700 Subject: [PATCH] Catch exceptions and create a J.Unknown in visitKtFile. --- .../internal/KotlinTreeParserVisitor.java | 20 ++++++++++++++++++- .../kotlin/tree/ClassDeclarationTest.java | 9 --------- .../openrewrite/kotlin/tree/PackageTest.java | 9 +++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 268a80b77..e43c17d45 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.types.Variance; import org.openrewrite.ExecutionContext; import org.openrewrite.FileAttributes; +import org.openrewrite.ParseExceptionResult; import org.openrewrite.Tree; import org.openrewrite.internal.EncodingDetectingInputStream; import org.openrewrite.internal.ListUtils; @@ -48,6 +49,7 @@ import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.marker.TrailingComma; import org.openrewrite.java.tree.*; +import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.kotlin.marker.*; import org.openrewrite.kotlin.tree.K; import org.openrewrite.marker.Markers; @@ -62,6 +64,7 @@ import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; import static org.openrewrite.Tree.randomId; +import static org.openrewrite.java.tree.Space.EMPTY; /** * PSI based parser @@ -1644,7 +1647,22 @@ public J visitKtFile(KtFile file, ExecutionContext data) { List> statements = new ArrayList<>(file.getDeclarations().size()); List declarations = file.getDeclarations(); for (KtDeclaration declaration : declarations) { - Statement statement = convertToStatement(declaration.accept(this, data)); + Statement statement; + try { + statement = convertToStatement(declaration.accept(this, data)); + } catch (Exception e) { + statement = new J.Unknown( + randomId(), + deepPrefix(declaration), + Markers.EMPTY, + new J.Unknown.Source( + randomId(), + EMPTY, + Markers.build(singletonList(ParseExceptionResult.build(KotlinParser.builder().build(), e) + .withTreeType(declaration.getClass().getName()))), + file.getText().substring(PsiUtilsKt.getStartOffsetSkippingComments(declaration), + declaration.getTextRange().getEndOffset()))); + } statements.add(padRight(statement, Space.EMPTY)); } diff --git a/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java b/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java index 3effa448a..c04e198b9 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ClassDeclarationTest.java @@ -29,15 +29,6 @@ @SuppressWarnings("ALL") class ClassDeclarationTest implements RewriteTest { - @Test - void whitespaceInPackage() { - rewriteRun( - kotlin( - "package foo . bar" - ) - ); - } - @Test void whitespaceInImport() { rewriteRun( diff --git a/src/test/java/org/openrewrite/kotlin/tree/PackageTest.java b/src/test/java/org/openrewrite/kotlin/tree/PackageTest.java index 3aeed9d07..57c666d31 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/PackageTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/PackageTest.java @@ -23,6 +23,15 @@ class PackageTest implements RewriteTest { + @Test + void whitespaceInPackage() { + rewriteRun( + kotlin( + "package foo . bar" + ) + ); + } + @Test void regular() { rewriteRun(