Skip to content

Commit 22c6dfa

Browse files
committed
Correctly evaluate nullable Boolean
1 parent 05d79fd commit 22c6dfa

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ abstract class AbstractNoGuavaImmutableOf extends Recipe {
4242
"The default value is false.",
4343
example = "true",
4444
required = false)
45+
@Nullable
4546
Boolean convertReturnType;
4647

4748
AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
4849
this.guavaType = guavaType;
4950
this.javaType = javaType;
5051
}
5152

52-
AbstractNoGuavaImmutableOf(String guavaType, String javaType, boolean convertReturnType) {
53+
AbstractNoGuavaImmutableOf(String guavaType, String javaType, @Nullable Boolean convertReturnType) {
5354
this.guavaType = guavaType;
5455
this.javaType = javaType;
5556
this.convertReturnType = convertReturnType;
@@ -116,10 +117,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
116117
@Override
117118
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
118119
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
119-
if (!convertReturnType) {
120-
return mv;
121-
}
122-
if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) {
120+
if (Boolean.TRUE.equals(convertReturnType) &&
121+
multiVariable != mv &&
122+
TypeUtils.isOfClassType(mv.getType(), guavaType)) {
123123
JavaType newType = JavaType.buildType(javaType);
124124
mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
125125
null : createNewTypeExpression(mv.getTypeExpression(), newType));
@@ -132,7 +132,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
132132
return variable;
133133
}));
134134
}
135-
136135
return mv;
137136
}
138137

@@ -223,7 +222,7 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
223222
private boolean isParentTypeMatched(@Nullable JavaType type) {
224223
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
225224
return TypeUtils.isOfClassType(fq, javaType) ||
226-
(convertReturnType && TypeUtils.isOfClassType(fq, guavaType)) ||
225+
(Boolean.TRUE.equals(convertReturnType) && TypeUtils.isOfClassType(fq, guavaType)) ||
227226
TypeUtils.isOfClassType(fq, "java.lang.Object");
228227
}
229228
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +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
}
2224

23-
public NoGuavaImmutableListOf(boolean convertReturnType) {
25+
public NoGuavaImmutableListOf(@Nullable Boolean convertReturnType) {
2426
super("com.google.common.collect.ImmutableList", "java.util.List", convertReturnType);
2527
}
2628
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +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
}
2224

23-
public NoGuavaImmutableMapOf(boolean convertReturnType) {
25+
public NoGuavaImmutableMapOf(@Nullable Boolean convertReturnType) {
2426
super("com.google.common.collect.ImmutableMap", "java.util.Map", convertReturnType);
2527
}
2628
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +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
}
2224

23-
public NoGuavaImmutableSetOf(boolean convertReturnType) {
25+
public NoGuavaImmutableSetOf(@Nullable Boolean convertReturnType) {
2426
super("com.google.common.collect.ImmutableSet", "java.util.Set", convertReturnType);
2527
}
2628
}

0 commit comments

Comments
 (0)