Skip to content
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 @@ -117,7 +117,7 @@ private J.MethodInvocation makeNestedGenericsExplicit(J.MethodInvocation mi, J.V
}

// Add explicit type parameters when the method is generic and the return type's type parameter matches a type parameter from the declaring class
if (mi.getTypeParameters() == null && mi.getMethodType() != null && containsGenericTypeVariable(mi.getMethodType().getReturnType())) {
if (mi.getTypeParameters() == null && mi.getMethodType() != null && containsGenericTypeVariable(mi.getMethodType().getReturnType()) && !containsWildcard(leftTypeParams)) {
// Create JRightPadded list from leftTypeParams
List<JRightPadded<Expression>> typeParamsList = new ArrayList<>();
for (Expression typeParam : leftTypeParams) {
Expand All @@ -141,6 +141,15 @@ private J.MethodInvocation makeNestedGenericsExplicit(J.MethodInvocation mi, J.V
}));
}

private static boolean containsWildcard(List<Expression> typeParams) {
for (Expression typeParam : typeParams) {
if (typeParam instanceof J.Wildcard) {
return true;
}
}
return false;
}

private boolean containsGenericTypeVariable(JavaType type) {
if (type instanceof JavaType.GenericTypeVariable) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,32 @@ void m() {
);
}

@Test
void wildcardTypeParameterNoExplicitTypeArgument() {
//language=java
rewriteRun(
version(
java(
"""
class A {
void m(ClassLoader cl) throws Exception {
Class<?> recipeClass = cl.loadClass("java.lang.String");
}
}
""",
"""
class A {
void m(ClassLoader cl) throws Exception {
var recipeClass = cl.loadClass("java.lang.String");
}
}
"""
),
10
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/868")
@Test
void genericsCollectorsRegression() {
Expand Down
Loading