Skip to content

Commit

Permalink
Review comments part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jente Sondervorst committed Oct 31, 2024
1 parent c522d53 commit 08d86c8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ configurations {
}

recipeDependencies {

parserClasspath("javax.persistence:javax.persistence-api:2.+")
parserClasspath("javax.validation:validation-api:2.0.1.Final")
parserClasspath("org.junit.jupiter:junit-jupiter-api:latest.release")
Expand Down Expand Up @@ -83,6 +84,7 @@ recipeDependencies {
parserClasspath("org.springframework:spring-webmvc:5.+")

parserClasspath("org.springframework.data:spring-data-commons:2.+")
parserClasspath("org.springframework.data:spring-data-commons:1.+")
parserClasspath("org.springframework.data:spring-data-jpa:2.+")
parserClasspath("org.springframework.data:spring-data-jpa:2.3.+")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

public class MigrateAuditorAwareToOptional extends Recipe {
Expand All @@ -42,9 +43,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaIsoVisitor<ExecutionContext> implementationVisitor = implementationVisitor();
JavaIsoVisitor<ExecutionContext> functionalVisitor = functionalVisitor(implementationVisitor);

//TODO the other visitors for new AuditorAware() {...} and method references.

return new TreeVisitor<Tree, ExecutionContext>() {
return Preconditions.check(new UsesType<>("org.springframework.data.domain.AuditorAware", true), new TreeVisitor<Tree, ExecutionContext>() {

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) {
Expand All @@ -56,18 +55,12 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
tree = functionalVisitor.visit(tree, ctx);
return tree;
}
};
});
}

private JavaIsoVisitor<ExecutionContext> implementationVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
maybeAddImport("java.util.Optional");
return super.visitCompilationUnit(cu, ctx);
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
if (classDeclaration.getImplements() == null || classDeclaration.getImplements().stream().noneMatch(typeTree -> isAuditorAware.matches(typeTree.getType()))) {
Expand All @@ -87,6 +80,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
returnType = TypeTree.build("java.util.Optional<" + returnType.getType() + ">");
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx).withReturnTypeExpression(returnType.withPrefix(space));
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(md));
maybeAddImport("java.util.Optional");
return md;
}

Expand All @@ -103,6 +97,7 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
if (altered == null) {
return return_;
}
maybeAddImport("java.util.Optional");

return altered;
}
Expand All @@ -111,13 +106,6 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {

private JavaIsoVisitor<ExecutionContext> functionalVisitor(JavaIsoVisitor<ExecutionContext> implementationVisitor) {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
maybeAddImport("java.util.Optional");
return super.visitCompilationUnit(cu, ctx);
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (!isAuditorAware.matches(method.getReturnTypeExpression()) || method.getBody() == null || method.getBody().getStatements().size() != 1) {
Expand All @@ -144,6 +132,7 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
.imports("java.util.Optional")
.build()
.apply(new Cursor(getCursor(), lambda), lambda.getCoordinates().replace(), body);
maybeAddImport("java.util.Optional");
return return_.withExpression(lambda.withBody(body));
} else {
return super.visitReturn(return_, ctx);
Expand All @@ -152,12 +141,14 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
if (isOptional.matches(((J.MethodInvocation) expression).getMethodType().getReturnType())) {
return return_;
}
maybeAddImport("java.util.Optional");
return return_.withExpression(JavaTemplate.builder("Optional.ofNullable(#{any()})")
.imports("java.util.Optional")
.build()
.apply(new Cursor(getCursor(), expression), expression.getCoordinates().replace(), expression));
} else if (expression instanceof J.NewClass && isAuditorAware.matches(((J.NewClass) expression).getClazz().getType())) {
implementationVisitor.setCursor(new Cursor(getCursor(), expression));
maybeAddImport("java.util.Optional");
return return_.withExpression(implementationVisitor.visitNewClass((J.NewClass) expression, ctx));
} else if (expression instanceof J.MemberReference) {
J.MemberReference memberReference = (J.MemberReference) expression;
Expand All @@ -171,6 +162,7 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
.contextSensitive()
.imports("java.util.Optional")
.build();
maybeAddImport("java.util.Optional");
return return_.withExpression(template.apply(new Cursor(getCursor(), expression), memberReference.getCoordinates().replace(), containing));
}
return return_;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -30,16 +31,13 @@ class MigrateAuditorAwareToOptionalTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("spring-data-commons"))
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-data-commons-1.13", "spring-data-commons-2.7"))
.recipe(new MigrateAuditorAwareToOptional());
}

@DocumentExample
@Test
void rewriteImplementation() {
//TODO Question for TIM: how to get rid of the types? I have the imports.
//- public Optional<String> getCurrentAuditor() {
//+ public java.util.Optional<java.lang.String> getCurrentAuditor() {
rewriteRun(
//language=java
java(
Expand Down

0 comments on commit 08d86c8

Please sign in to comment.