Skip to content

Commit 59d56a6

Browse files
BramliAKtimtebeekgithub-actions[bot]
authored
Do not convert return types in NoGuavaJava21 by default (#601)
* [HotFix] NoGuavaJava21 causing invalid code since v2.28.0 * Apply formatter to minimize diff * add Ability To Convert Return Type as an option * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix conflicts * Apply formatter * Minimize diff by restoring original method order * Minimize diff by restoring original method order * Move tests into nested sub class * Correctly evaluate nullable Boolean --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent d6ecd54 commit 59d56a6

File tree

8 files changed

+461
-283
lines changed

8 files changed

+461
-283
lines changed

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@ abstract class AbstractNoGuavaImmutableOf extends Recipe {
3737
private final String guavaType;
3838
private final String javaType;
3939

40+
@Option(displayName = "Whether to convert return type (the default value is false).",
41+
description = "converting the return type from Guava Type to Java Type " +
42+
"The default value is false.",
43+
example = "true",
44+
required = false)
45+
@Nullable
46+
Boolean convertReturnType;
47+
4048
AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
4149
this.guavaType = guavaType;
4250
this.javaType = javaType;
4351
}
4452

53+
AbstractNoGuavaImmutableOf(String guavaType, String javaType, @Nullable Boolean convertReturnType) {
54+
this.guavaType = guavaType;
55+
this.javaType = javaType;
56+
this.convertReturnType = convertReturnType;
57+
}
58+
4559
private String getShortType(String fullyQualifiedType) {
4660
return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1);
4761
}
@@ -103,8 +117,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
103117
@Override
104118
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
105119
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
106-
107-
if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) {
120+
if (Boolean.TRUE.equals(convertReturnType) &&
121+
multiVariable != mv &&
122+
TypeUtils.isOfClassType(mv.getType(), guavaType)) {
108123
JavaType newType = JavaType.buildType(javaType);
109124
mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
110125
null : createNewTypeExpression(mv.getTypeExpression(), newType));
@@ -117,7 +132,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
117132
return variable;
118133
}));
119134
}
120-
121135
return mv;
122136
}
123137

@@ -148,7 +162,6 @@ private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) {
148162
);
149163
}
150164

151-
152165
private boolean isParentTypeDownCast(MethodCall immutableMethod) {
153166
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
154167
boolean isParentTypeDownCast = false;
@@ -173,10 +186,12 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
173186
} else if (parent instanceof J.MethodInvocation) {
174187
J.MethodInvocation m = (J.MethodInvocation) parent;
175188
int index = m.getArguments().indexOf(immutableMethod);
176-
if (m.getMethodType() != null && index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
177-
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
178-
} else {
179-
isParentTypeDownCast = true;
189+
if (m.getMethodType() != null) {
190+
if (index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
191+
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
192+
} else {
193+
isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType(), guavaType);
194+
}
180195
}
181196
} else if (parent instanceof J.NewClass) {
182197
J.NewClass c = (J.NewClass) parent;
@@ -207,8 +222,8 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
207222
private boolean isParentTypeMatched(@Nullable JavaType type) {
208223
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
209224
return TypeUtils.isOfClassType(fq, javaType) ||
210-
TypeUtils.isOfClassType(fq, "java.lang.Object") ||
211-
TypeUtils.isOfClassType(fq, guavaType);
225+
(Boolean.TRUE.equals(convertReturnType) && TypeUtils.isOfClassType(fq, guavaType)) ||
226+
TypeUtils.isOfClassType(fq, "java.lang.Object");
212227
}
213228
});
214229
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableListOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableListOf() {
2022
super("com.google.common.collect.ImmutableList", "java.util.List");
2123
}
24+
25+
public NoGuavaImmutableListOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableList", "java.util.List", convertReturnType);
27+
}
2228
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableMapOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableMapOf() {
2022
super("com.google.common.collect.ImmutableMap", "java.util.Map");
2123
}
24+
25+
public NoGuavaImmutableMapOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableMap", "java.util.Map", convertReturnType);
27+
}
2228
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.openrewrite.java.migrate.guava;
1717

18+
import org.jspecify.annotations.Nullable;
19+
1820
public class NoGuavaImmutableSetOf extends AbstractNoGuavaImmutableOf {
1921
public NoGuavaImmutableSetOf() {
2022
super("com.google.common.collect.ImmutableSet", "java.util.Set");
2123
}
24+
25+
public NoGuavaImmutableSetOf(@Nullable Boolean convertReturnType) {
26+
super("com.google.common.collect.ImmutableSet", "java.util.Set", convertReturnType);
27+
}
2228
}

0 commit comments

Comments
 (0)