Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not convert return types in NoGuavaJava21 by default #601

Merged
merged 14 commits into from
Nov 25, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ abstract class AbstractNoGuavaImmutableOf extends Recipe {
private final String guavaType;
private final String javaType;

@Option(displayName = "Whether to convert return type (the default value is false).",
description = "converting the return type from Guava Type to Java Type " +
"The default value is false.",
example = "true",
required = false)
@Nullable
Boolean convertReturnType;

AbstractNoGuavaImmutableOf(String guavaType, String javaType) {
this.guavaType = guavaType;
this.javaType = javaType;
}

AbstractNoGuavaImmutableOf(String guavaType, String javaType, @Nullable Boolean convertReturnType) {
this.guavaType = guavaType;
this.javaType = javaType;
this.convertReturnType = convertReturnType;
}

private String getShortType(String fullyQualifiedType) {
return fullyQualifiedType.substring(javaType.lastIndexOf(".") + 1);
}
Expand Down Expand Up @@ -103,8 +117,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);

if (multiVariable != mv && TypeUtils.isOfClassType(mv.getType(), guavaType)) {
if (Boolean.TRUE.equals(convertReturnType) &&
multiVariable != mv &&
TypeUtils.isOfClassType(mv.getType(), guavaType)) {
JavaType newType = JavaType.buildType(javaType);
mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
null : createNewTypeExpression(mv.getTypeExpression(), newType));
Expand All @@ -117,7 +132,6 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
return variable;
}));
}

return mv;
}

Expand Down Expand Up @@ -148,7 +162,6 @@ private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) {
);
}


private boolean isParentTypeDownCast(MethodCall immutableMethod) {
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
boolean isParentTypeDownCast = false;
Expand All @@ -173,10 +186,12 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
} else if (parent instanceof J.MethodInvocation) {
J.MethodInvocation m = (J.MethodInvocation) parent;
int index = m.getArguments().indexOf(immutableMethod);
if (m.getMethodType() != null && index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
} else {
isParentTypeDownCast = true;
if (m.getMethodType() != null) {
if (index != -1 && !m.getMethodType().getParameterTypes().isEmpty()) {
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
} else {
isParentTypeDownCast = !TypeUtils.isOfClassType(m.getMethodType().getReturnType(), guavaType);
}
}
} else if (parent instanceof J.NewClass) {
J.NewClass c = (J.NewClass) parent;
Expand Down Expand Up @@ -207,8 +222,8 @@ private boolean isParentTypeDownCast(MethodCall immutableMethod) {
private boolean isParentTypeMatched(@Nullable JavaType type) {
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(type);
return TypeUtils.isOfClassType(fq, javaType) ||
TypeUtils.isOfClassType(fq, "java.lang.Object") ||
TypeUtils.isOfClassType(fq, guavaType);
(Boolean.TRUE.equals(convertReturnType) && TypeUtils.isOfClassType(fq, guavaType)) ||
TypeUtils.isOfClassType(fq, "java.lang.Object");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.openrewrite.java.migrate.guava;

import org.jspecify.annotations.Nullable;

public class NoGuavaImmutableListOf extends AbstractNoGuavaImmutableOf {
public NoGuavaImmutableListOf() {
super("com.google.common.collect.ImmutableList", "java.util.List");
}

public NoGuavaImmutableListOf(@Nullable Boolean convertReturnType) {
super("com.google.common.collect.ImmutableList", "java.util.List", convertReturnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.openrewrite.java.migrate.guava;

import org.jspecify.annotations.Nullable;

public class NoGuavaImmutableMapOf extends AbstractNoGuavaImmutableOf {
public NoGuavaImmutableMapOf() {
super("com.google.common.collect.ImmutableMap", "java.util.Map");
}

public NoGuavaImmutableMapOf(@Nullable Boolean convertReturnType) {
super("com.google.common.collect.ImmutableMap", "java.util.Map", convertReturnType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.openrewrite.java.migrate.guava;

import org.jspecify.annotations.Nullable;

public class NoGuavaImmutableSetOf extends AbstractNoGuavaImmutableOf {
public NoGuavaImmutableSetOf() {
super("com.google.common.collect.ImmutableSet", "java.util.Set");
}

public NoGuavaImmutableSetOf(@Nullable Boolean convertReturnType) {
super("com.google.common.collect.ImmutableSet", "java.util.Set", convertReturnType);
}
}
Loading