From b0c3e6b6338f3ffd3dd66f270a952fc7bb7d5da4 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 8 Oct 2024 11:44:03 +0200 Subject: [PATCH] Polish to use JavaTemplate.apply & drop annotation handling --- .../EmptyInterceptorToInterface.java | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/openrewrite/hibernate/EmptyInterceptorToInterface.java b/src/main/java/org/openrewrite/hibernate/EmptyInterceptorToInterface.java index 5f90829..938ded4 100644 --- a/src/main/java/org/openrewrite/hibernate/EmptyInterceptorToInterface.java +++ b/src/main/java/org/openrewrite/hibernate/EmptyInterceptorToInterface.java @@ -21,19 +21,17 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.ListUtils; -import org.openrewrite.java.*; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; import org.openrewrite.java.search.FindImplementations; import org.openrewrite.java.tree.*; -import java.util.ArrayList; -import java.util.List; - public class EmptyInterceptorToInterface extends Recipe { private final String EMPTY_INTERCEPTOR = "org.hibernate.EmptyInterceptor"; private final String INTERCEPTOR = "org.hibernate.Interceptor"; private final String STATEMENT_INSPECTOR = "org.hibernate.resource.jdbc.spi.StatementInspector"; - private static final AnnotationMatcher OVERRIDE_ANNOTATION_MATCHER = new AnnotationMatcher("java.lang.Override"); private static final MethodMatcher ON_PREPARE_STATEMENT = new MethodMatcher("org.hibernate.Interceptor onPrepareStatement(java.lang.String)", true); @Override @@ -49,23 +47,21 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return Preconditions.check(new FindImplementations(EMPTY_INTERCEPTOR), - new JavaIsoVisitor() { + return Preconditions.check(new FindImplementations(EMPTY_INTERCEPTOR), new JavaIsoVisitor() { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); if (cd.getExtends() != null && TypeUtils.isOfClassType(cd.getExtends().getType(), EMPTY_INTERCEPTOR)) { cd = cd.withExtends(null).withImplements(ListUtils.concat(cd.getImplements(), (TypeTree) TypeTree.build("Interceptor").withType(JavaType.buildType(INTERCEPTOR)).withPrefix(Space.SINGLE_SPACE))); - Boolean prepareStatement = getCursor().pollMessage("prepareStatementFound"); - if (Boolean.TRUE.equals(prepareStatement)) { + maybeAddImport(INTERCEPTOR); + if (getCursor().pollMessage("prepareStatementFound") != null) { cd = cd.withImplements(ListUtils.concat(cd.getImplements(), (TypeTree) TypeTree.build("StatementInspector").withType(JavaType.buildType(STATEMENT_INSPECTOR)).withPrefix(Space.SINGLE_SPACE))); + maybeAddImport(STATEMENT_INSPECTOR); } - maybeAddImport(INTERCEPTOR); - maybeAddImport(STATEMENT_INSPECTOR); maybeRemoveImport(EMPTY_INTERCEPTOR); - } - if (cd.getPadding().getImplements() != null) { - cd = cd.getPadding().withImplements(cd.getPadding().getImplements().withBefore(Space.SINGLE_SPACE)); + if (cd.getPadding().getImplements() != null) { + cd = cd.getPadding().withImplements(cd.getPadding().getImplements().withBefore(Space.SINGLE_SPACE)); + } } return cd; } @@ -76,18 +72,10 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex J.ClassDeclaration cd = getCursor().firstEnclosing(J.ClassDeclaration.class); if (cd != null && ON_PREPARE_STATEMENT.matches(md, cd)) { getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "prepareStatementFound", true); - String template = "@Override\n" + - "public String inspect() {\n" + - "}\n"; - J.MethodDeclaration inspect = JavaTemplate.builder(template) - .javaParser(JavaParser.fromJavaVersion()) - .build() - .apply(getCursor(), md.getCoordinates().replace()); - List annotations = new ArrayList<>(md.getLeadingAnnotations()); - if (annotations.stream().noneMatch(OVERRIDE_ANNOTATION_MATCHER::matches)) { - annotations.addAll(inspect.getLeadingAnnotations()); - } - md = inspect.withBody(md.getBody()).withLeadingAnnotations(annotations).withParameters(md.getParameters()); + J.MethodDeclaration inspect = JavaTemplate.apply( + "@Override public String inspect(String overriddenBelow) { return overriddenBelow; }", + getCursor(), md.getCoordinates().replace()); + return inspect.withParameters(md.getParameters()).withBody(md.getBody()); } return md; }