Skip to content

Commit

Permalink
Isolate JavaTemplate issue with generic type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Aug 2, 2024
1 parent 65fb0cf commit 45ca436
Showing 1 changed file with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1200,19 +1200,69 @@ void finalMethodParameter() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
java(
"""
import org.jetbrains.annotations.NotNull;
class A {
String testMethod(@NotNull final String test) {}
}
""", """
import lombok.NonNull;
class A {
String testMethod(@NonNull final String test) {}
"""
import org.jetbrains.annotations.NotNull;
class A {
String testMethod(@NotNull final String test) {}
}
""", """
import lombok.NonNull;
class A {
String testMethod(@NonNull final String test) {}
}
""")
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-spring/pull/284")
void replaceMethodInChainFollowedByGenericTypeParameters() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
if (new MethodMatcher("batch.StepBuilder create()").matches(method)) {
return JavaTemplate.builder("new StepBuilder()")
.contextSensitive()
.build()
.apply(getCursor(), method.getCoordinates().replace());
}
""")
return super.visitMethodInvocation(method, executionContext);
}
}))
.parser(JavaParser.fromJavaVersion().dependsOn(
"""
package batch;
public class StepBuilder {
public static StepBuilder create() { return new StepBuilder(); }
public StepBuilder() {}
public <T> T method() { return null; }
}
"""
)
),
java(
"""
import batch.StepBuilder;
class Foo {
void test() {
StepBuilder.create()
.<String>method();
}
}
""",
"""
import batch.StepBuilder;
class Foo {
void test() {
new StepBuilder()
.<String>method();
}
}
"""
)
);
}
}

0 comments on commit 45ca436

Please sign in to comment.