Skip to content

Commit 85ecc14

Browse files
committed
Apply formatter to minimize diff
1 parent a36e0e9 commit 85ecc14

File tree

3 files changed

+860
-856
lines changed

3 files changed

+860
-856
lines changed

src/main/java/org/openrewrite/java/migrate/guava/AbstractNoGuavaImmutableOf.java

Lines changed: 124 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
package org.openrewrite.java.migrate.guava;
1717

1818
import org.jspecify.annotations.Nullable;
19-
import org.openrewrite.*;
19+
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.Preconditions;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
2023
import org.openrewrite.java.JavaTemplate;
2124
import org.openrewrite.java.JavaVisitor;
2225
import org.openrewrite.java.MethodMatcher;
@@ -26,136 +29,137 @@
2629

2730
import java.time.Duration;
2831
import java.util.List;
29-
abstract class AbstractNoGuavaImmutableOf extends Recipe {
3032

31-
private final String guavaType;
32-
private final String javaType;
33+
abstract class AbstractNoGuavaImmutableOf extends Recipe {
3334

34-
AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
35-
this.guavaType = guavaType;
36-
this.javaType = javaType;
37-
}
35+
private final String guavaType;
36+
private final String javaType;
3837

39-
private String getShortType(String fullyQualifiedType) {
40-
return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1);
41-
}
38+
AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
39+
this.guavaType = guavaType;
40+
this.javaType = javaType;
41+
}
4242

43-
@Override
44-
public String getDisplayName() {
45-
return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher";
46-
}
43+
private String getShortType(String fullyQualifiedType) {
44+
return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1);
45+
}
4746

48-
@Override
49-
public String getDescription() {
50-
return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast.";
51-
}
47+
@Override
48+
public String getDisplayName() {
49+
return "Prefer `" + getShortType(javaType) + ".of(..)` in Java 9 or higher";
50+
}
5251

53-
@Override
54-
public Duration getEstimatedEffortPerOccurrence() {
55-
return Duration.ofMinutes(10);
56-
}
52+
@Override
53+
public String getDescription() {
54+
return "Replaces `" + getShortType(guavaType) + ".of(..)` if the returned type is immediately down-cast.";
55+
}
5756

58-
@Override
59-
public TreeVisitor<?, ExecutionContext> getVisitor() {
60-
TreeVisitor<?, ExecutionContext> check = Preconditions.and(new UsesJavaVersion<>(9),
61-
new UsesType<>(guavaType, false));
62-
final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)");
63-
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
64-
@Override
65-
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
66-
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
67-
if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) {
68-
return mi;
69-
}
70-
maybeRemoveImport(guavaType);
71-
maybeAddImport(javaType);
57+
@Override
58+
public Duration getEstimatedEffortPerOccurrence() {
59+
return Duration.ofMinutes(10);
60+
}
7261

73-
String template;
74-
Object[] templateArguments;
75-
List<Expression> methodArguments = mi.getArguments();
76-
if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) {
77-
template = getShortType(javaType) + ".of()";
78-
templateArguments = new Object[]{};
79-
} else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) {
80-
template = getShortType(javaType) + ".of(#{any()}, #{any()})";
81-
templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)};
82-
} else {
83-
template = getShortType(javaType) + ".of(#{any()})";
84-
templateArguments = new Object[]{methodArguments.get(0)};
85-
}
62+
@Override
63+
public TreeVisitor<?, ExecutionContext> getVisitor() {
64+
TreeVisitor<?, ExecutionContext> check = Preconditions.and(new UsesJavaVersion<>(9),
65+
new UsesType<>(guavaType, false));
66+
final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher(guavaType + " of(..)");
67+
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
68+
@Override
69+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
70+
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
71+
if (!IMMUTABLE_MATCHER.matches(mi) || !isParentTypeDownCast(mi)) {
72+
return mi;
73+
}
74+
maybeRemoveImport(guavaType);
75+
maybeAddImport(javaType);
8676

87-
J.MethodInvocation m = JavaTemplate.builder(template)
88-
.imports(javaType)
89-
.build()
90-
.apply(getCursor(), mi.getCoordinates().replace(), templateArguments);
91-
m = m.getPadding().withArguments(mi.getPadding().getArguments());
92-
JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx);
93-
m = m.withMethodType(newType).withName(m.getName().withType(newType));
94-
return super.visitMethodInvocation(m, ctx);
95-
}
77+
String template;
78+
Object[] templateArguments;
79+
List<Expression> methodArguments = mi.getArguments();
80+
if (methodArguments.isEmpty() || methodArguments.get(0) instanceof J.Empty) {
81+
template = getShortType(javaType) + ".of()";
82+
templateArguments = new Object[]{};
83+
} else if ("com.google.common.collect.ImmutableMap".equals(guavaType)) {
84+
template = getShortType(javaType) + ".of(#{any()}, #{any()})";
85+
templateArguments = new Object[]{methodArguments.get(0), methodArguments.get(1)};
86+
} else {
87+
template = getShortType(javaType) + ".of(#{any()})";
88+
templateArguments = new Object[]{methodArguments.get(0)};
89+
}
9690

97-
private boolean isParentTypeDownCast(MethodCall immutableMethod) {
98-
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
99-
boolean isParentTypeDownCast = false;
100-
if (parent instanceof J.VariableDeclarations.NamedVariable) {
101-
isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType());
102-
} else if (parent instanceof J.Assignment) {
103-
J.Assignment a = (J.Assignment) parent;
104-
if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) {
105-
isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType());
106-
} else if (a.getVariable() instanceof J.FieldAccess) {
107-
isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType());
108-
}
109-
} else if (parent instanceof J.Return) {
110-
// Does not currently support returns in lambda expressions.
111-
J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue();
112-
if (j instanceof J.MethodDeclaration) {
113-
TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression();
114-
if (returnType != null) {
115-
isParentTypeDownCast = isParentTypeMatched(returnType.getType());
116-
}
117-
}
118-
} else if (parent instanceof J.MethodInvocation) {
119-
J.MethodInvocation m = (J.MethodInvocation) parent;
120-
int index = m.getArguments().indexOf(immutableMethod);
121-
if (m.getMethodType() != null) {
122-
if(index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
123-
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
124-
} else {
125-
isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType() , guavaType);
126-
}
127-
}
128-
} else if (parent instanceof J.NewClass) {
129-
J.NewClass c = (J.NewClass) parent;
130-
int index = 0;
131-
if (c.getConstructorType() != null) {
132-
for (Expression argument : c.getArguments()) {
133-
if (IMMUTABLE_MATCHER.matches(argument)) {
134-
break;
135-
}
136-
index++;
91+
J.MethodInvocation m = JavaTemplate.builder(template)
92+
.imports(javaType)
93+
.build()
94+
.apply(getCursor(), mi.getCoordinates().replace(), templateArguments);
95+
m = m.getPadding().withArguments(mi.getPadding().getArguments());
96+
JavaType.Method newType = (JavaType.Method) visitType(mi.getMethodType(), ctx);
97+
m = m.withMethodType(newType).withName(m.getName().withType(newType));
98+
return super.visitMethodInvocation(m, ctx);
13799
}
138-
if (c.getConstructorType() != null) {
139-
isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index));
140-
}
141-
}
142-
} else if (parent instanceof J.NewArray) {
143-
J.NewArray a = (J.NewArray) parent;
144-
JavaType arrayType = a.getType();
145-
while (arrayType instanceof JavaType.Array) {
146-
arrayType = ((JavaType.Array) arrayType).getElemType();
147-
}
148100

149-
isParentTypeDownCast = isParentTypeMatched(arrayType);
150-
}
151-
return isParentTypeDownCast;
152-
}
101+
private boolean isParentTypeDownCast(MethodCall immutableMethod) {
102+
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
103+
boolean isParentTypeDownCast = false;
104+
if (parent instanceof J.VariableDeclarations.NamedVariable) {
105+
isParentTypeDownCast = isParentTypeMatched(((J.VariableDeclarations.NamedVariable) parent).getType());
106+
} else if (parent instanceof J.Assignment) {
107+
J.Assignment a = (J.Assignment) parent;
108+
if (a.getVariable() instanceof J.Identifier && ((J.Identifier) a.getVariable()).getFieldType() != null) {
109+
isParentTypeDownCast = isParentTypeMatched(((J.Identifier) a.getVariable()).getFieldType().getType());
110+
} else if (a.getVariable() instanceof J.FieldAccess) {
111+
isParentTypeDownCast = isParentTypeMatched(a.getVariable().getType());
112+
}
113+
} else if (parent instanceof J.Return) {
114+
// Does not currently support returns in lambda expressions.
115+
J j = getCursor().dropParentUntil(is -> is instanceof J.MethodDeclaration || is instanceof J.CompilationUnit).getValue();
116+
if (j instanceof J.MethodDeclaration) {
117+
TypeTree returnType = ((J.MethodDeclaration) j).getReturnTypeExpression();
118+
if (returnType != null) {
119+
isParentTypeDownCast = isParentTypeMatched(returnType.getType());
120+
}
121+
}
122+
} else if (parent instanceof J.MethodInvocation) {
123+
J.MethodInvocation m = (J.MethodInvocation) parent;
124+
int index = m.getArguments().indexOf(immutableMethod);
125+
if (m.getMethodType() != null) {
126+
if (index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
127+
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
128+
} else {
129+
isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType(), guavaType);
130+
}
131+
}
132+
} else if (parent instanceof J.NewClass) {
133+
J.NewClass c = (J.NewClass) parent;
134+
int index = 0;
135+
if (c.getConstructorType() != null) {
136+
for (Expression argument : c.getArguments()) {
137+
if (IMMUTABLE_MATCHER.matches(argument)) {
138+
break;
139+
}
140+
index++;
141+
}
142+
if (c.getConstructorType() != null) {
143+
isParentTypeDownCast = isParentTypeMatched(c.getConstructorType().getParameterTypes().get(index));
144+
}
145+
}
146+
} else if (parent instanceof J.NewArray) {
147+
J.NewArray a = (J.NewArray) parent;
148+
JavaType arrayType = a.getType();
149+
while (arrayType instanceof JavaType.Array) {
150+
arrayType = ((JavaType.Array) arrayType).getElemType();
151+
}
152+
153+
isParentTypeDownCast = isParentTypeMatched(arrayType);
154+
}
155+
return isParentTypeDownCast;
156+
}
153157

154-
private boolean isParentTypeMatched(@Nullable JavaType type) {
155-
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
156-
return TypeUtils.isOfClassType(fq, javaType) ||
157-
TypeUtils.isOfClassType(fq, "java.lang.Object");
158-
}
159-
});
160-
}
158+
private boolean isParentTypeMatched(@Nullable JavaType type) {
159+
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
160+
return TypeUtils.isOfClassType(fq, javaType) ||
161+
TypeUtils.isOfClassType(fq, "java.lang.Object");
162+
}
163+
});
164+
}
161165
}

0 commit comments

Comments
 (0)