Skip to content

Call visit overload with parent cursor when called from another visitor #41

@timtebeek

Description

@timtebeek

We recently added a note to the docs about calling visitors from other visitors:

What problem are you trying to solve?

Avoid folks calling incorrect (more specific) visit methods on visitors from another visitor.

What precondition(s) should be checked before applying this recipe?

We're inside a visitor, calling another visit method.

Describe the situation before applying the recipe

class SomeJavaVisitor extends JavaVisitor<ExecutionContext> {
    @Override
    public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
        J m = super.visitMethodInvocation(method, ctx);
        m = new OtherJavaVisitor().visitMethodInvocation(m, ctx);
        return m;
    }
}

Describe the situation after applying the recipe

class SomeJavaVisitor extends JavaVisitor<ExecutionContext> {
    @Override
    public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
        J m = super.visitMethodInvocation(method, ctx);
        m = new OtherJavaVisitor().visit(m, ctx, getCursor().getParentTreeCursor());
        return m;
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Recipes Wanted

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions