Skip to content

Commit

Permalink
Drop TODO item
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 1, 2024
1 parent 7a21585 commit fe15df6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.java.spring.data;

import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.java.*;
Expand All @@ -41,18 +42,14 @@ public String getDescription() {

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

ImplementationVisitor implementationVisitor = new ImplementationVisitor();
FunctionalVisitor functionalVisitor = new FunctionalVisitor(implementationVisitor);
return Preconditions.check(new UsesType<>("org.springframework.data.domain.AuditorAware", true), new JavaIsoVisitor<ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) {
if (!(tree instanceof SourceFile)) {
return tree;
}

ImplementationVisitor implementationVisitor = new ImplementationVisitor();
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
tree = implementationVisitor.visit(tree, ctx);
tree = new FunctionalVisitor(implementationVisitor).visit(tree, ctx);
return tree;
tree = functionalVisitor.visit(tree, ctx);
return (J) tree;
}
});
}
Expand Down Expand Up @@ -92,13 +89,10 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
}
}

@RequiredArgsConstructor
private static class FunctionalVisitor extends JavaIsoVisitor<ExecutionContext> {
private final JavaIsoVisitor<ExecutionContext> implementationVisitor;

public FunctionalVisitor(JavaIsoVisitor<ExecutionContext> implementationVisitor) {
this.implementationVisitor = implementationVisitor;
}

@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 @@ -108,22 +102,18 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
if (!(statement instanceof J.Return)) {
return method;
}

return super.visitMethodDeclaration(method, ctx);
}


@Override
public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {

Expression expression = return_.getExpression();
if (expression instanceof J.MemberReference) {
J.MemberReference memberReference = (J.MemberReference) expression;
JavaType.Method methodType = memberReference.getMethodType();
if (methodType == null || isOptional.matches(methodType.getReturnType())) {
return return_;
}

expression = (Expression) new MemberReferenceToMethodInvocation().visitNonNull(memberReference, ctx, new Cursor(getCursor(), expression).getParent());
}
if (expression instanceof J.Lambda) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ public AuditorAware<String> auditorAware() {

@Test
void rewriteInterfaceInstantiation() {
//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 fe15df6

Please sign in to comment.