Skip to content

Commit

Permalink
Removing JavaVisitor#template
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 25, 2021
1 parent a245586 commit 67ac1cc
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;

import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Replace method declaration @RequestMapping annotations with the associated variant
Expand Down Expand Up @@ -100,13 +98,13 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
if (resolvedRequestMappingAnnotationClassName != null) {
maybeAddImport("org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName);
if (a.getArguments() == null || a.getArguments().isEmpty()) {
a = a.withTemplate(template("@" + associatedRequestMapping(requestType.get()))
a = a.withTemplate(JavaTemplate.builder(this::getCursor, "@" + associatedRequestMapping(requestType.get()))
.imports("org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName)
.javaParser(JAVA_PARSER::get).build(), a.getCoordinates().replace());
} else {
String annotationTemplateString = "@" + associatedRequestMapping(requestType.get()) +
"(" + a.getArguments().stream().map(J::print).collect(Collectors.joining(",")) + ")";
JavaTemplate tb = template(annotationTemplateString)
JavaTemplate tb = JavaTemplate.builder(this::getCursor, annotationTemplateString)
.imports("org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName)
.javaParser(JAVA_PARSER::get).build();
a = a.withTemplate(tb, a.getCoordinates().replace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private static class ConditionalOnBeanAnyNestedConditionVisitor extends JavaIsoV
private static final String ANY_CONDITION_TEMPLATES = "any_condition_templates";
private static final AnnotationMatcher CONDITIONAL_BEAN = new AnnotationMatcher("@org.springframework.boot.autoconfigure.condition.ConditionalOnBean");

private final JavaTemplate conditionalTemplate = template("@Conditional(#{}.class)")
private final JavaTemplate conditionalTemplate = JavaTemplate.builder(this::getCursor, "@Conditional(#{}.class)")
.imports("org.springframework.context.annotation.Conditional")
.javaParser(JAVA_PARSER::get)
.build();
Expand Down Expand Up @@ -162,7 +162,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
Set<String> conditionalTemplates = getCursor().pollMessage(ANY_CONDITION_TEMPLATES);
if (conditionalTemplates != null && !conditionalTemplates.isEmpty()) {
for (String s : conditionalTemplates) {
JavaTemplate t = template(s)
JavaTemplate t = JavaTemplate.builder(this::getCursor, s)
.imports("org.springframework.boot.autoconfigure.condition.AnyNestedCondition")
.javaParser(() -> JavaParser.fromJavaVersion()
.dependsOn(Parser.Input.fromResource("/AnyNestedCondition.java", "---"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private final JavaTemplate addOutputCaptureExtension = template("@ExtendWith(OutputCaptureExtension.class)")
private final JavaTemplate addOutputCaptureExtension = JavaTemplate.builder(this::getCursor, "@ExtendWith(OutputCaptureExtension.class)")
.javaParser(JAVA_PARSER::get)
.imports("org.junit.jupiter.api.extension.ExtendWith",
"org.springframework.boot.test.system.OutputCaptureExtension")
Expand Down Expand Up @@ -149,7 +149,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
);

private static class ConvertExpectMethods extends JavaIsoVisitor<ExecutionContext> {
private final JavaTemplate matchesTemplate = template("#{any()}.matches(#{}.getAll())")
private final JavaTemplate matchesTemplate = JavaTemplate.builder(this::getCursor, "#{any()}.matches(#{}.getAll())")
.javaParser(JAVA_PARSER::get)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.SemanticallyEqual;
import org.openrewrite.java.search.UsesType;
Expand Down Expand Up @@ -253,7 +254,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (maybeMarker.isPresent()) {
ReplaceEnvironmentUtilsMarker marker = maybeMarker.get();
m = m.withTemplate(
template(marker.templateString)
JavaTemplate.builder(this::getCursor, marker.templateString)
.javaParser(JAVA_PARSER::get)
.imports("org.springframework.boot.test.util.TestPropertyValues")
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
method.getArguments().get(0).getType());

if (REQUEST_FACTORY.matches(method) && isArgumentClientHttpRequestFactory) {
JavaTemplate.Builder t = template("() -> #{any(org.springframework.http.client.ClientHttpRequestFactory)}")
JavaTemplate.Builder t = JavaTemplate.builder(this::getCursor, "() -> #{any(org.springframework.http.client.ClientHttpRequestFactory)}")
.javaParser(() -> JavaParser.fromJavaVersion()
.dependsOn(Parser.Input.fromResource("/RestTemplateBuilder.java", "---"))
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package org.openrewrite.java.spring.boot2

import org.junit.jupiter.api.Test
import org.openrewrite.Issue
import org.openrewrite.Parser
import org.openrewrite.Recipe
import org.openrewrite.java.JavaParser
import org.openrewrite.java.JavaRecipeTest
Expand Down

0 comments on commit 67ac1cc

Please sign in to comment.