Skip to content

Commit e59af63

Browse files
authored
Fix UnnecessaryExplicitTypeArguments within lambda return statements (#858)
* Fix UnnecessaryExplicitTypeArguments within lambda return statements (#670) * Extract findMethodIfUnambiguous and getLambdaReturnType methods
1 parent 5845516 commit e59af63

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/main/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArguments.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.openrewrite.java.tree.*;
2222
import org.openrewrite.staticanalysis.java.JavaFileChecker;
2323

24+
import org.jspecify.annotations.Nullable;
25+
2426
import java.util.ArrayList;
2527
import java.util.List;
2628

@@ -90,7 +92,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
9092
inferredType = methodDeclaration.getReturnTypeExpression().getType();
9193
}
9294
} else if (e instanceof J.Lambda) {
93-
inferredType = ((J.Lambda) e).getType();
95+
inferredType = getLambdaReturnType(((J.Lambda) e).getType());
9496
}
9597
}
9698

@@ -101,6 +103,46 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
101103
return m;
102104
}
103105

106+
private JavaType.@Nullable Method findMethodIfUnambiguous(JavaType.FullyQualified type) {
107+
JavaType.Method sam = null;
108+
for (JavaType.Method candidate : type.getMethods()) {
109+
if (candidate.hasFlags(Flag.Default) || candidate.hasFlags(Flag.Static)) {
110+
continue;
111+
}
112+
if (sam != null) {
113+
return null;
114+
}
115+
sam = candidate;
116+
}
117+
return sam;
118+
}
119+
120+
private @Nullable JavaType getLambdaReturnType(@Nullable JavaType lambdaType) {
121+
JavaType.Parameterized parameterized = TypeUtils.asParameterized(lambdaType);
122+
if (parameterized == null) {
123+
return null;
124+
}
125+
JavaType.Method sam = findMethodIfUnambiguous(parameterized);
126+
if (sam == null) {
127+
return null;
128+
}
129+
JavaType samReturn = sam.getReturnType();
130+
if (samReturn instanceof JavaType.GenericTypeVariable) {
131+
String name = ((JavaType.GenericTypeVariable) samReturn).getName();
132+
List<JavaType> formalParams = parameterized.getType().getTypeParameters();
133+
List<JavaType> actualParams = parameterized.getTypeParameters();
134+
for (int i = 0; i < formalParams.size() && i < actualParams.size(); i++) {
135+
JavaType formal = formalParams.get(i);
136+
if (formal instanceof JavaType.GenericTypeVariable &&
137+
name.equals(((JavaType.GenericTypeVariable) formal).getName())) {
138+
return actualParams.get(i);
139+
}
140+
}
141+
return null;
142+
}
143+
return samReturn;
144+
}
145+
104146
private boolean shouldRetainOnStaticMethod(JavaType.Method methodType) {
105147
if (!methodType.hasFlags(Flag.Static)) {
106148
return false;

src/test/java/org/openrewrite/staticanalysis/UnnecessaryExplicitTypeArgumentsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Object o() {
7171
);
7272
}
7373

74-
@ExpectedToFail("Not implemented yet")
74+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/670")
7575
@Test
7676
void withinLambda() {
7777
rewriteRun(

0 commit comments

Comments
 (0)