I want to convert
x.foo(a, b);
into
x.foo(a).bar(b);
I can easily match for acme.X foo(acme.A, acme.B)
, but how do I build a JavaTemplate that can do that replacement for me?
When I run
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<>() {
private final JavaTemplate template = JavaTemplate.builder(this::getCursor,
"foo(#{any(java.lang.String)}).bar(#{any(java.lang.String)})")
.build();
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
if (....matches(method)) {
List<Expression> arguments = m.getArguments();
m = m.withTemplate(template, m.getCoordinates().replace(), arguments.get(0), arguments.get(1));
}
return m;
}
};
}
I get
foo(a).bar(b);
instead of
x.foo(a).bar(b);
This worked for me (credit to Patrick Way on slack):