Skip to content

Commit

Permalink
fix NPE for records
Browse files Browse the repository at this point in the history
  • Loading branch information
amishra-u committed Dec 19, 2024
1 parent 713b88e commit 5485d36
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ScopeAwareVisitor extends JavaVisitor<ExecutionContext> {

@Override
public J preVisit(J j, ExecutionContext ctx) {
if (j instanceof J.ClassDeclaration) {
scopes.push(new VariablesInScope(getCursor()));
}
if (j instanceof J.Block) {
scopes.push(new VariablesInScope(getCursor()));
}
Expand All @@ -50,9 +53,15 @@ public J preVisit(J j, ExecutionContext ctx) {

@Override
public J postVisit(J j, ExecutionContext ctx) {
if (j instanceof J.ClassDeclaration) {
scopes.pop();
}
if (j instanceof J.Block) {
scopes.pop();
}
if (j instanceof J.MethodDeclaration) {
scopes.pop();
}
return super.postVisit(j, ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,45 @@ public void bar(ZonedDateTime dt) {
);
}

@Test
void migrationWithRecord() {
//language=java
rewriteRun(
java(
"""
import org.joda.time.DateTime;
record A(String x) {
public void foo() {
new Bar().bar(new DateTime());
}
private static class Bar {
public void bar(DateTime dt) {
dt.getMillis();
}
}
}
""",
"""
import java.time.ZonedDateTime;
record A(String x) {
public void foo() {
new Bar().bar(ZonedDateTime.now());
}
private static class Bar {
public void bar(ZonedDateTime dt) {
dt.toInstant().toEpochMilli();
}
}
}
"""
)
);
}

@Test
void migrateMethodWithSafeReturnExpression() {
//language=java
Expand Down

0 comments on commit 5485d36

Please sign in to comment.