Skip to content

Commit 9a50b85

Browse files
authored
fix NPE for records (#637)
1 parent 40b77d2 commit 9a50b85

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class ScopeAwareVisitor extends JavaVisitor<ExecutionContext> {
3434

3535
@Override
3636
public J preVisit(J j, ExecutionContext ctx) {
37+
if (j instanceof J.ClassDeclaration) {
38+
scopes.push(new VariablesInScope(getCursor()));
39+
}
3740
if (j instanceof J.Block) {
3841
scopes.push(new VariablesInScope(getCursor()));
3942
}
@@ -50,9 +53,15 @@ public J preVisit(J j, ExecutionContext ctx) {
5053

5154
@Override
5255
public J postVisit(J j, ExecutionContext ctx) {
56+
if (j instanceof J.ClassDeclaration) {
57+
scopes.pop();
58+
}
5359
if (j instanceof J.Block) {
5460
scopes.pop();
5561
}
62+
if (j instanceof J.MethodDeclaration) {
63+
scopes.pop();
64+
}
5665
return super.postVisit(j, ctx);
5766
}
5867

src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,45 @@ public void bar(ZonedDateTime dt) {
280280
);
281281
}
282282

283+
@Test
284+
void migrationWithRecord() {
285+
//language=java
286+
rewriteRun(
287+
java(
288+
"""
289+
import org.joda.time.DateTime;
290+
291+
record A(String x) {
292+
public void foo() {
293+
new Bar().bar(new DateTime());
294+
}
295+
296+
private static class Bar {
297+
public void bar(DateTime dt) {
298+
dt.getMillis();
299+
}
300+
}
301+
}
302+
""",
303+
"""
304+
import java.time.ZonedDateTime;
305+
306+
record A(String x) {
307+
public void foo() {
308+
new Bar().bar(ZonedDateTime.now());
309+
}
310+
311+
private static class Bar {
312+
public void bar(ZonedDateTime dt) {
313+
dt.toInstant().toEpochMilli();
314+
}
315+
}
316+
}
317+
"""
318+
)
319+
);
320+
}
321+
283322
@Test
284323
void migrateMethodWithSafeReturnExpression() {
285324
//language=java

0 commit comments

Comments
 (0)