Skip to content

Commit

Permalink
Handle JavaType.Intersection in DefaultJavaTypeSignatureBuilder (#…
Browse files Browse the repository at this point in the history
…3732)

Fixes: #3731
  • Loading branch information
knutwannheden authored Nov 23, 2023
1 parent f2bd62e commit d1f9512
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ private String signature(@Nullable Type type) {
if (type == null || type instanceof Type.UnknownType || type instanceof NullType) {
return "{undefined}";
} else if (type instanceof Type.IntersectionClassType) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
return intersectionSignature(type);
} else if (type instanceof Type.ClassType) {
try {
return ((Type.ClassType) type).typarams_field != null && ((Type.ClassType) type).typarams_field.length() > 0 ? parameterizedSignature(type) : classSignature(type);
Expand Down Expand Up @@ -150,6 +145,15 @@ public String genericSignature(Object type) {
return s.append("}").toString();
}

private String intersectionSignature(Object type) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
}

@Override
public String parameterizedSignature(Object type) {
StringBuilder s = new StringBuilder(classSignature(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ private String signature(@Nullable Type type) {
if (type == null || type instanceof Type.UnknownType || type instanceof NullType) {
return "{undefined}";
} else if (type instanceof Type.IntersectionClassType) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
return intersectionSignature(type);
} else if (type instanceof Type.ClassType) {
try {
return ((Type.ClassType) type).typarams_field != null && ((Type.ClassType) type).typarams_field.length() > 0 ? parameterizedSignature(type) : classSignature(type);
Expand Down Expand Up @@ -150,6 +145,15 @@ public String genericSignature(Object type) {
return s.append("}").toString();
}

private String intersectionSignature(Object type) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
}

@Override
public String parameterizedSignature(Object type) {
StringBuilder s = new StringBuilder(classSignature(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ private String signature(@Nullable Type type) {
if (type == null || type instanceof Type.UnknownType || type instanceof NullType) {
return "{undefined}";
} else if (type instanceof Type.IntersectionClassType) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
return intersectionSignature(type);
} else if (type instanceof Type.ClassType) {
try {
return ((Type.ClassType) type).typarams_field != null && ((Type.ClassType) type).typarams_field.length() > 0 ? parameterizedSignature(type) : classSignature(type);
Expand Down Expand Up @@ -150,6 +145,15 @@ public String genericSignature(Object type) {
return s.append("}").toString();
}

private String intersectionSignature(Object type) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
}

@Override
public String parameterizedSignature(Object type) {
StringBuilder s = new StringBuilder(classSignature(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ private String signature(@Nullable Type type) {
if (type == null || type instanceof Type.UnknownType || type instanceof NullType) {
return "{undefined}";
} else if (type instanceof Type.IntersectionClassType) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
return intersectionSignature(type);
} else if (type instanceof Type.ClassType) {
try {
return ((Type.ClassType) type).typarams_field != null && ((Type.ClassType) type).typarams_field.length() > 0 ? parameterizedSignature(type) : classSignature(type);
Expand Down Expand Up @@ -149,6 +144,15 @@ public String genericSignature(Object type) {
return s.append("}").toString();
}

private String intersectionSignature(Object type) {
Type.IntersectionClassType intersectionClassType = (Type.IntersectionClassType) type;
StringJoiner joiner = new StringJoiner(" & ");
for (TypeMirror typeArg : intersectionClassType.getBounds()) {
joiner.add(signature(typeArg));
}
return joiner.toString();
}

@Override
public String parameterizedSignature(Object type) {
StringBuilder s = new StringBuilder(classSignature(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.test.RewriteTest;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.tree.TypeUtils.findDeclaredMethod;
import static org.openrewrite.test.RewriteTest.toRecipe;

@SuppressWarnings("ConstantConditions")
Expand Down Expand Up @@ -91,4 +94,33 @@ public class A {
)
);
}

@Test
void intersectionTypeSignature() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
JavaType.Method methodType = method.getMethodType();
Optional<JavaType.Method> ignore = findDeclaredMethod(methodType.getDeclaringType(), methodType.getName(), methodType.getParameterTypes());
return method;
}
})),
java(
"""
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
class A {
void m() {
Set<Class<?>> primitiveTypes = new HashSet<>(32);
Collections.addAll(primitiveTypes, boolean[].class, byte[].class, char[].class,
double[].class, float[].class, int[].class, long[].class, short[].class);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public String signature(@Nullable Object type) {
return genericSignature(type);
} else if (type instanceof JavaType.Primitive) {
return primitiveSignature(type);
} else if(type instanceof JavaType.Method) {
} else if (type instanceof JavaType.Method) {
return methodSignature((JavaType.Method) type);
} else if(type instanceof JavaType.Variable) {
} else if (type instanceof JavaType.Variable) {
return variableSignature((JavaType.Variable) type);
} else if (type instanceof JavaType.Intersection) {
return intersectionSignature(type);
}

throw new UnsupportedOperationException("Unexpected type " + type.getClass().getName());
Expand Down Expand Up @@ -92,7 +94,7 @@ public String genericSignature(Object type) {

StringJoiner bounds = new StringJoiner(" & ");
for (JavaType bound : gtv.getBounds()) {
if(parameterizedStack == null || !parameterizedStack.contains(bound)) {
if (parameterizedStack == null || !parameterizedStack.contains(bound)) {
bounds.add(signature(bound));
}
}
Expand All @@ -103,11 +105,20 @@ public String genericSignature(Object type) {
return s.toString();
}

private String intersectionSignature(Object type) {
JavaType.Intersection it = (JavaType.Intersection) type;
StringJoiner bounds = new StringJoiner(" & ");
for (JavaType bound : it.getBounds()) {
bounds.add(signature(bound));
}
return bounds.toString();
}

@Override
public String parameterizedSignature(Object type) {
JavaType.Parameterized pt = (JavaType.Parameterized) type;

if(parameterizedStack == null) {
if (parameterizedStack == null) {
parameterizedStack = Collections.newSetFromMap(new IdentityHashMap<>());
}
parameterizedStack.add(pt);
Expand Down

0 comments on commit d1f9512

Please sign in to comment.