Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,7 @@ private Expression transformDependencyToVersionCatalog(Expression arg, J.MethodI
if (dep != null) {
boolean hasMultipleArgs = methodInvocation.getArguments().size() > 1;

Expression catalogRef = JavaTemplate.builder("libs.#{}")
.build()
.apply(
new Cursor(getCursor(), arg),
arg.getCoordinates().replace(),
dep.getAliasName()
);
Expression catalogRef = JavaTemplate.apply("libs.#{}", new Cursor(getCursor(), arg), arg.getCoordinates().replace(), dep.getAliasName());

catalogRef = catalogRef.withPrefix(literal.getPrefix());

Expand Down Expand Up @@ -526,13 +520,7 @@ private Expression transformDependencyToVersionCatalog(Expression arg, J.MethodI
if (dep.gav.getGroupId().equals(group) && dep.gav.getArtifactId().equals(artifact)) {
boolean hasMultipleArgs = methodInvocation.getArguments().size() > 1;

Expression catalogRef = JavaTemplate.builder("libs.#{}")
.build()
.apply(
new Cursor(getCursor(), arg),
arg.getCoordinates().replace(),
dep.getAliasName()
);
Expression catalogRef = JavaTemplate.apply("libs.#{}", new Cursor(getCursor(), arg), arg.getCoordinates().replace(), dep.getAliasName());

catalogRef = catalogRef.withPrefix(gstring.getPrefix());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ void useJavaTemplateInBuildGradle() {
@Override
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
if (block.getStatements().isEmpty()) {
return JavaTemplate.builder("implementation(\"com.google.guava:guava:latest.release\")")
.build()
.apply(getCursor(), block.getCoordinates().lastStatement());
return JavaTemplate.apply("implementation(\"com.google.guava:guava:latest.release\")", getCursor(), block.getCoordinates().lastStatement());
}
return super.visitBlock(block, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ void contextFreeTypeParameter() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if ("o".equals(multiVariable.getVariables().getFirst().getSimpleName())) {
return JavaTemplate.builder("var o = #{any()};")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
return JavaTemplate.apply("var o = #{any()};", getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
}
return multiVariable;
}
Expand Down Expand Up @@ -241,9 +239,7 @@ void contextFreeTypeParameterConflictNames_broken() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
if ("printf".equals(method.getSimpleName()) && method.getArguments().size() == 2) {
return JavaTemplate.builder("System.out.printf(#{any()}, #{any()}, 0);")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().toArray());
return JavaTemplate.apply("System.out.printf(#{any()}, #{any()}, 0);", getCursor(), method.getCoordinates().replace(), method.getArguments().toArray());
}
return super.visitMethodInvocation(method, executionContext);
}
Expand Down Expand Up @@ -277,9 +273,7 @@ void contextFreeRecursiveTypeParameters() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if ("o".equals(multiVariable.getVariables().getFirst().getSimpleName())) {
return JavaTemplate.builder("var o = #{any()};")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
return JavaTemplate.apply("var o = #{any()};", getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
}
return multiVariable;
}
Expand Down Expand Up @@ -317,9 +311,7 @@ void contextFreeSequenceTypeParameters() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if ("o".equals(multiVariable.getVariables().getFirst().getSimpleName())) {
return JavaTemplate.builder("var o = #{any()};")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
return JavaTemplate.apply("var o = #{any()};", getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
}
return multiVariable;
}
Expand Down Expand Up @@ -359,9 +351,7 @@ void contextFreeMultipleBoundParameters() {
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if ("o".equals(multiVariable.getVariables().getFirst().getSimpleName())) {
return JavaTemplate.builder("var o = #{any()};")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
return JavaTemplate.apply("var o = #{any()};", getCursor(), multiVariable.getCoordinates().replace(), multiVariable.getVariables().getFirst().getInitializer());
}
return multiVariable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
}

List<Expression> arguments = mi.getArguments();
mi = JavaTemplate.builder("#{any(java.lang.String)}.formatted(#{any()})")
.build()
.apply(
updateCursor(mi),
mi.getCoordinates().replace(),
arguments.toArray()
);
mi = JavaTemplate.apply("#{any(java.lang.String)}.formatted(#{any()})", updateCursor(mi), mi.getCoordinates().replace(), arguments.toArray());

return maybeAutoFormat(mi, mi.withArguments(
ListUtils.map(arguments.subList(1, arguments.size()), (a, b) -> b.withPrefix(arguments.get(a + 1).getPrefix()))), ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ void replaceSingleStatement() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitAssert(J.Assert anAssert, ExecutionContext p) {
return JavaTemplate.builder(
"""
if(#{name:any(int)} != #{}) {
#{name}++;
}"""
)
.build()
.apply(getCursor(), anAssert.getCoordinates().replace(),
((J.Binary) anAssert.getCondition()).getLeft(), "1");
return JavaTemplate.apply("""
if(#{name:any(int)} != #{}) {
#{name}++;
}""", getCursor(), anAssert.getCoordinates().replace(), ((J.Binary) anAssert.getCondition()).getLeft(), "1");
}
})),
java(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ void annotation() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
if ("test".equals(method.getSimpleName())) {
return JavaTemplate.builder("#{} void test2() {}")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getLeadingAnnotations().getFirst());
return JavaTemplate.apply("#{} void test2() {}", getCursor(), method.getCoordinates().replace(), method.getLeadingAnnotations().getFirst());
}
return method;
}
Expand Down Expand Up @@ -194,9 +192,7 @@ void block() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
var s = method.getBody().getStatements().getFirst();
return JavaTemplate.builder("if(true) #{}")
.build()
.apply(getCursor(), s.getCoordinates().replace(), method.getBody());
return JavaTemplate.apply("if(true) #{}", getCursor(), s.getCoordinates().replace(), method.getBody());
}
}).withMaxCycles(1)),
java(
Expand Down Expand Up @@ -425,9 +421,7 @@ void anyIsGenericWithUnknownType() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
return JavaTemplate.builder("System.out.println(#{any()})")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method);
return JavaTemplate.apply("System.out.println(#{any()})", getCursor(), method.getCoordinates().replace(), method);
}
}).withMaxCycles(1)),
java(
Expand Down Expand Up @@ -457,9 +451,7 @@ void throwNewException() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitMethodInvocation(J.MethodInvocation methodInvocation, ExecutionContext executionContext) {
return JavaTemplate.builder("throw new RuntimeException()")
.build()
.apply(getCursor(), methodInvocation.getCoordinates().replace());
return JavaTemplate.apply("throw new RuntimeException()", getCursor(), methodInvocation.getCoordinates().replace());
}
})),
java(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ void addParentheses() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public <T extends J> J visitParentheses(J.Parentheses<T> parens, ExecutionContext ctx) {
return JavaTemplate.builder("#{any()}")
.build()
.apply(getCursor(), parens.getCoordinates().replace(), parens.getTree());
return JavaTemplate.apply("#{any()}", getCursor(), parens.getCoordinates().replace(), parens.getTree());
}
}))
.cycles(1)
Expand All @@ -138,9 +136,7 @@ void addParenthesesToParameter() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
return JavaTemplate.builder("#{any(int)} * 3")
.build()
.apply(getCursor(), binary.getCoordinates().replace(), binary);
return JavaTemplate.apply("#{any(int)} * 3", getCursor(), binary.getCoordinates().replace(), binary);
}
}))
.cycles(1)
Expand Down Expand Up @@ -209,9 +205,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
return multiVariable;
}
J.VariableDeclarations.NamedVariable var0 = multiVariable.getVariables().getFirst();
return JavaTemplate.builder("var #{} = #{any()};")
.build()
.apply(getCursor(), multiVariable.getCoordinates().replace(), var0.getSimpleName(), var0.getInitializer());
return JavaTemplate.apply("var #{} = #{any()};", getCursor(), multiVariable.getCoordinates().replace(), var0.getSimpleName(), var0.getInitializer());
}
})),
java(
Expand Down Expand Up @@ -399,18 +393,11 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ct
if (classDecl.getBody().getStatements().size() > 1) {
return classDecl;
}
return JavaTemplate.builder("""
return JavaTemplate.apply("""
void m2() {
#{any()}
}
"""
)
.build()
.apply(
getCursor(),
classDecl.getBody().getStatements().getFirst().getCoordinates().after(),
((J.MethodDeclaration) classDecl.getBody().getStatements().getFirst()).getBody().getStatements().getFirst()
);
""", getCursor(), classDecl.getBody().getStatements().getFirst().getCoordinates().after(), ((J.MethodDeclaration) classDecl.getBody().getStatements().getFirst()).getBody().getStatements().getFirst());
}
})),
java(
Expand Down Expand Up @@ -595,9 +582,7 @@ void templatingWhileLoopCondition() {
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
if (binary.getLeft() instanceof J.MethodInvocation) {
var mi = (J.MethodInvocation) binary.getLeft();
return JavaTemplate.builder("!#{any(java.util.List)}.isEmpty()")
.build()
.apply(getCursor(), mi.getCoordinates().replace(), mi.getSelect());
return JavaTemplate.apply("!#{any(java.util.List)}.isEmpty()", getCursor(), mi.getCoordinates().replace(), mi.getSelect());
} else if (binary.getLeft() instanceof J.Unary) {
return binary.getLeft();
}
Expand Down Expand Up @@ -678,12 +663,7 @@ void replaceExpressionWithAnotherExpression() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitUnary(J.Unary unary, ExecutionContext ctx) {
return JavaTemplate.builder("#{any()}++")
.build().apply(
getCursor(),
unary.getCoordinates().replace(),
unary.getExpression()
);
return JavaTemplate.apply("#{any()}++", getCursor(), unary.getCoordinates().replace(), unary.getExpression());
}
}).withMaxCycles(1)),
java(
Expand Down Expand Up @@ -1017,9 +997,7 @@ void nestedEnums() {
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
return JavaTemplate.builder("\"ab\"")
.build()
.apply(getCursor(), binary.getCoordinates().replace());
return JavaTemplate.apply("\"ab\"", getCursor(), binary.getCoordinates().replace());
}
})),
java(
Expand Down Expand Up @@ -1354,9 +1332,7 @@ void replaceMethodInChainFollowedByGenericTypeParameters() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (new MethodMatcher("batch.StepBuilder create()").matches(method)) {
return JavaTemplate.builder("new StepBuilder()")
.build()
.apply(getCursor(), method.getCoordinates().replace());
return JavaTemplate.apply("new StepBuilder()", getCursor(), method.getCoordinates().replace());
}
return super.visitMethodInvocation(method, ctx);
}
Expand Down Expand Up @@ -1406,9 +1382,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (new MethodMatcher("Foo bar(..)").matches(mi) &&
mi.getArguments().getFirst() instanceof J.Binary) {
return JavaTemplate.builder("\"Hello, {}\", \"World!\"")
.build()
.apply(new Cursor(getCursor().getParent(), mi), mi.getCoordinates().replaceArguments());
return JavaTemplate.apply("\"Hello, {}\", \"World!\"", new Cursor(getCursor().getParent(), mi), mi.getCoordinates().replaceArguments());
}
return mi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ void replacePackage() {
@Override
public J.Package visitPackage(J.Package pkg, ExecutionContext p) {
if ("a".equals(pkg.getExpression().printTrimmed(getCursor()))) {
return JavaTemplate.builder("b")
.build()
.apply(getCursor(), pkg.getCoordinates().replace());
return JavaTemplate.apply("b", getCursor(), pkg.getCoordinates().replace());
}
return super.visitPackage(pkg, p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ void replaceAndInterpolateMethodParameters() {
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext p) {
{
if ("test".equals(method.getSimpleName()) && method.getParameters().size() == 1) {
return JavaTemplate.builder("int n, #{}")
.build()
.apply(getCursor(), method.getCoordinates().replaceParameters(), method.getParameters().getFirst());
return JavaTemplate.apply("int n, #{}", getCursor(), method.getCoordinates().replaceParameters(), method.getParameters().getFirst());
}
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ void replaceThrows() {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext p) {
if (method.getThrows() == null) {
return JavaTemplate.builder("Exception")
.build()
.apply(getCursor(), method.getCoordinates().replaceThrows());
return JavaTemplate.apply("Exception", getCursor(), method.getCoordinates().replaceThrows());
}
return super.visitMethodDeclaration(method, p);
}
Expand Down
Loading