Skip to content

Commit 38cb45e

Browse files
committed
Fix RenameExceptionInEmptyCatch crash on Kotlin/Groovy files (#701)
1 parent cbc1692 commit 38cb45e

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/main/java/org/openrewrite/staticanalysis/RenameExceptionInEmptyCatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private boolean validIdentifier() {
155155
// Sets structure for cursor positions to aggregate namespaces.
156156
private Cursor getCursorToParentScope() {
157157
return getCursor().dropParentUntil(is ->
158-
is instanceof J.CompilationUnit ||
158+
is instanceof JavaSourceFile ||
159159
is instanceof J.ClassDeclaration ||
160160
is instanceof J.Block ||
161161
is instanceof J.MethodDeclaration ||

src/test/java/org/openrewrite/staticanalysis/RenameExceptionInEmptyCatchTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.Issue;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
2223

24+
import static org.openrewrite.groovy.Assertions.groovy;
2325
import static org.openrewrite.java.Assertions.java;
26+
import static org.openrewrite.kotlin.Assertions.kotlin;
2427

2528
@SuppressWarnings({"EmptyTryBlock", "CatchMayIgnoreException"})
2629
class RenameExceptionInEmptyCatchTest implements RewriteTest {
@@ -150,4 +153,30 @@ void method() {
150153
)
151154
);
152155
}
156+
157+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/701")
158+
@Test
159+
void kotlinImportOnly() {
160+
rewriteRun(
161+
//language=kotlin
162+
kotlin(
163+
"""
164+
import nebula.plugin.contacts.Contact
165+
"""
166+
)
167+
);
168+
}
169+
170+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/701")
171+
@Test
172+
void groovyTopLevelStatement() {
173+
rewriteRun(
174+
//language=groovy
175+
groovy(
176+
"""
177+
def file = new File("test")
178+
"""
179+
)
180+
);
181+
}
153182
}

0 commit comments

Comments
 (0)