Skip to content

Commit

Permalink
Only use contextSensitive sparingly
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 31, 2024
1 parent ddb41a5 commit c522d53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class MigrateAuditorAwareToOptional extends Recipe {
private static final TypeMatcher isAuditorAware = new TypeMatcher("org.springframework.data.domain.AuditorAware", true);
private static final MethodMatcher isCurrentAuditor = new MethodMatcher("org.springframework.data.domain.AuditorAware getCurrentAuditor()", true);
private static final TypeMatcher isOptional = new TypeMatcher("java.util.Optional");
private static final JavaTemplate wrapOptional = JavaTemplate.builder("Optional.ofNullable(#{any()})").contextSensitive().imports("java.util.Optional").build();

@Override
public String getDisplayName() {
Expand Down Expand Up @@ -97,7 +96,10 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
if (expression == null) {
return return_;
}
J.Return altered = wrapOptional.apply(getCursor(), expression.getCoordinates().replace(), expression);
J.Return altered = JavaTemplate.builder("Optional.ofNullable(#{any()})")
.imports("java.util.Optional")
.build()
.apply(getCursor(), expression.getCoordinates().replace(), expression);
if (altered == null) {
return return_;
}
Expand Down Expand Up @@ -137,7 +139,11 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
J.Lambda lambda = ((J.Lambda) expression);
J body = lambda.getBody();
if (body instanceof J.Literal) {
body = wrapOptional.apply(new Cursor(getCursor(), lambda), lambda.getCoordinates().replace(), body);
body = JavaTemplate.builder("Optional.ofNullable(#{any()})")
.contextSensitive()
.imports("java.util.Optional")
.build()
.apply(new Cursor(getCursor(), lambda), lambda.getCoordinates().replace(), body);
return return_.withExpression(lambda.withBody(body));
} else {
return super.visitReturn(return_, ctx);
Expand All @@ -146,7 +152,10 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
if (isOptional.matches(((J.MethodInvocation) expression).getMethodType().getReturnType())) {
return return_;
}
return return_.withExpression(wrapOptional.apply(new Cursor(getCursor(), expression), expression.getCoordinates().replace(), expression));
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));
return return_.withExpression(implementationVisitor.visitNewClass((J.NewClass) expression, ctx));
Expand All @@ -158,7 +167,10 @@ public J.Return visitReturn(J.Return return_, ExecutionContext ctx) {
}
Expression containing = memberReference.getContaining();
//TODO Question for TIM: If I use #{any()} for the method name, as getName returns a String, I get a java.lang.ClassCastException: class java.lang.String cannot be cast to class org.openrewrite.java.tree.J
JavaTemplate template = JavaTemplate.builder("() -> Optional.ofNullable(#{any()}." + methodType.getName() + "())").imports("java.util.Optional").contextSensitive().build();
JavaTemplate template = JavaTemplate.builder("() -> Optional.ofNullable(#{any()}." + methodType.getName() + "())")
.contextSensitive()
.imports("java.util.Optional")
.build();
return return_.withExpression(template.apply(new Cursor(getCursor(), expression), memberReference.getCoordinates().replace(), containing));
}
return return_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getCurrentAuditor() {
public class MyAuditorAware implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
public Optional<java.lang.String> getCurrentAuditor() {
return Optional.ofNullable("admin");
}
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public class Configuration {
public AuditorAware<String> auditorAware() {
return new AuditorAware<String>() {
@Override
public Optional<String> getCurrentAuditor() {
public Optional<java.lang.String> getCurrentAuditor() {
return Optional.ofNullable("admin");
}
};
Expand Down

0 comments on commit c522d53

Please sign in to comment.