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

InstanceOfPatternMatch: Generic type without parameters #298

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.VariableNameUtils;
Expand All @@ -33,6 +34,7 @@
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -224,6 +226,25 @@ public J.InstanceOf processInstanceOf(J.InstanceOf instanceOf, Cursor cursor) {
name,
type,
null));
JavaType.FullyQualified fqType = TypeUtils.asFullyQualified(type);
if (fqType != null && !fqType.getTypeParameters().isEmpty() && !(instanceOf.getClazz() instanceof J.ParameterizedType)) {
TypedTree oldTypeTree = (TypedTree) instanceOf.getClazz();

// Each type parameter is turned into a wildcard, i.e. `List` -> `List<?>` or `Map.Entry` -> `Map.Entry<?,?>`
List<Expression> wildcardsList = IntStream.range(0, fqType.getTypeParameters().size())
.mapToObj(i -> new J.Wildcard(randomId(), Space.EMPTY, Markers.EMPTY, null, null))
.collect(Collectors.toList());

J.ParameterizedType newTypeTree = new J.ParameterizedType(
randomId(),
oldTypeTree.getPrefix(),
Markers.EMPTY,
oldTypeTree.withPrefix(Space.EMPTY),
null,
oldTypeTree.getType()
).withTypeParameters(wildcardsList);
result = result.withClazz(newTypeTree);
}

// update entry in replacements to share the pattern variable name
for (Map.Entry<J.TypeCast, J.InstanceOf> entry : replacements.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,64 @@ void test(Object o) {
);
}

@Test
void genericsWithoutParameters() {
rewriteRun(
//language=java
java(
"""
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class A {
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> applyRoutesType(Object routes) {
if (routes instanceof List) {
List<Object> routesList = (List<Object>) routes;
if (routesList.isEmpty()) {
return Collections.emptyList();
}
if (routesList.stream()
.anyMatch(route -> !(route instanceof Map))) {
return Collections.emptyList();
}
return routesList.stream()
.map(route -> (Map<String, Object>) route)
.collect(Collectors.toList());
}
return Collections.emptyList();
}
}
""",
"""
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class A {
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> applyRoutesType(Object routes) {
if (routes instanceof List<?> routesList) {
if (routesList.isEmpty()) {
return Collections.emptyList();
}
if (routesList.stream()
.anyMatch(route -> !(route instanceof Map))) {
return Collections.emptyList();
}
return routesList.stream()
.map(route -> (Map<String, Object>) route)
.collect(Collectors.toList());
}
return Collections.emptyList();
}
}
"""
)
);
}

@Test
void primitiveArray() {
rewriteRun(
Expand Down Expand Up @@ -245,7 +303,7 @@ void test(Object o) {
public class A {
void test(Object o) {
Map.Entry entry = null;
if (o instanceof Map.Entry entry1) {
if (o instanceof Map.Entry<?,?> entry1) {
entry = entry1;
}
System.out.println(entry);
Expand Down Expand Up @@ -700,7 +758,7 @@ Object test(Object o) {
import java.util.List;
public class A {
Object test(Object o) {
return o instanceof List l ? l.get(0) : o.toString();
return o instanceof List<?> l ? l.get(0) : o.toString();
}
}
"""
Expand All @@ -725,7 +783,7 @@ Object test(Object o) {
import java.util.List;
public class A {
Object test(Object o) {
return o instanceof List l ? l.get(0) : o.toString();
return o instanceof List<?> l ? l.get(0) : o.toString();
}
}
"""
Expand Down