Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joda Recipe NullpointerException when visting Java Records #635

Closed
christopherfischer opened this issue Dec 19, 2024 · 3 comments · Fixed by #637
Closed

Joda Recipe NullpointerException when visting Java Records #635

christopherfischer opened this issue Dec 19, 2024 · 3 comments · Fixed by #637
Labels
bug Something isn't working question Further information is requested

Comments

@christopherfischer
Copy link

What version of OpenRewrite are you using?

I am using

  • Java 21
  • Maven CLI 3.9.6
  • rewrite-migrate-java-2.31.0

How are you running OpenRewrite?

I am using the Maven CLI , and my project is a multi module project.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.migrate.joda.JodaTimeRecipe -Drewrite.exportDatatables=true

What is the full stack trace of any errors you encountered?

java.lang.NullPointerException: Cannot read field "variables" because "x0" is null
[ERROR]   org.openrewrite.java.migrate.joda.ScopeAwareVisitor$VariablesInScope.access$000(ScopeAwareVisitor.java:86)
[ERROR]   org.openrewrite.java.migrate.joda.ScopeAwareVisitor.preVisit(ScopeAwareVisitor.java:46)
[ERROR]   org.openrewrite.java.migrate.joda.ScopeAwareVisitor.preVisit(ScopeAwareVisitor.java:31)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:247)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
[ERROR]   org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27(JavaVisitor.java:961)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:243)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:265)
[ERROR]   org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:961)
[ERROR]   org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5812)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
[ERROR]   org.openrewrite.java.JavaVisitor.lambda$visitContainer$35(JavaVisitor.java:1417)

Are you interested in contributing a fix to OpenRewrite?

@christopherfischer christopherfischer added the bug Something isn't working label Dec 19, 2024
@timtebeek
Copy link
Contributor

Thanks for the report! This one is a bit puzzling, given that it's reporting an error on line 86 here:

@Value
public static class VariablesInScope {
Cursor scope;
Set<J.VariableDeclarations.NamedVariable> variables;
public VariablesInScope(Cursor scope) {
this.scope = scope;
this.variables = new HashSet<>();
}
}

Any clue @amishra-u ?

@timtebeek timtebeek added the question Further information is requested label Dec 19, 2024
@christopherfischer
Copy link
Author

It looks like scopes.peek() is null

@amishra-u
Copy link
Contributor

amishra-u commented Dec 19, 2024

We need to update the preVisit method in ScopeAwareVisitor to handle Record. As with Records, classes can have variable as parameter (not sure the right term)

    public J preVisit(J j, ExecutionContext ctx) {
        if (j instanceof J.Class) {
            scopes.push(new VariablesInScope(getCursor()));
        }
        if (j instanceof J.Block) {
            scopes.push(new VariablesInScope(getCursor()));
        }
        if (j instanceof J.MethodDeclaration) {
            scopes.push(new VariablesInScope(getCursor()));
        }
        if (j instanceof J.VariableDeclarations.NamedVariable) {
            assert !scopes.isEmpty();
            NamedVariable variable = (NamedVariable) j;
            scopes.peek().variables.add(variable);
        }
        return super.preVisit(j, ctx);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants