Skip to content

Commit 11bbf82

Browse files
UseCollectionInterfaces updates methodInvovations type info to enable import removal (#517)
* Fixed issue * Update src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Added logic to keep type information in the method invocation * Added method for methodUpdating * removed unused import * Moved private method * formatted and inlined * Removed whitespace changes --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5a97f80 commit 11bbf82

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
150150
return m;
151151
}
152152

153+
@Override
154+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
155+
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
156+
if (mi.getSelect() != null && mi.getSelect().getType() != null) {
157+
JavaType originalType = mi.getSelect().getType();
158+
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(originalType);
159+
if (fullyQualified != null) {
160+
String fullyQualifiedName = fullyQualified.getFullyQualifiedName();
161+
if (rspecRulesReplaceTypeMap.containsKey(fullyQualifiedName)) {
162+
JavaType.FullyQualified newType = TypeUtils.asFullyQualified(JavaType.buildType(rspecRulesReplaceTypeMap.get(fullyQualifiedName)));
163+
if (newType != null) {
164+
if (originalType instanceof JavaType.Parameterized) {
165+
newType = new JavaType.Parameterized(null, newType, ((JavaType.Parameterized) originalType).getTypeParameters());
166+
}
167+
return updateMethodInvocation(mi, newType);
168+
}
169+
}
170+
}
171+
}
172+
return mi;
173+
}
174+
153175
@Override
154176
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
155177
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);
@@ -201,6 +223,16 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
201223
return mv;
202224
}
203225

226+
private J.MethodInvocation updateMethodInvocation(J.MethodInvocation mi, JavaType.FullyQualified newType) {
227+
if (mi.getSelect() != null) {
228+
mi = mi.withSelect(mi.getSelect().withType(newType));
229+
}
230+
if (mi.getMethodType() != null) {
231+
mi = mi.withMethodType(mi.getMethodType().withDeclaringType(newType));
232+
}
233+
return mi;
234+
}
235+
204236
private TypeTree removeFromParameterizedType(JavaType.FullyQualified newType,
205237
J.ParameterizedType parameterizedType) {
206238
J.Identifier returnType = new J.Identifier(

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

+60
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,64 @@ void groovyDefVariable() {
10011001
)
10021002
);
10031003
}
1004+
1005+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/357")
1006+
@Test
1007+
void danglingGenericReference() {
1008+
rewriteRun(
1009+
//language=java
1010+
java(
1011+
"""
1012+
import java.util.concurrent.ConcurrentLinkedQueue;
1013+
1014+
class B {
1015+
private final ConcurrentLinkedQueue<String> widgetDataList;
1016+
public void foo() {
1017+
widgetDataList.add("TEST");
1018+
}
1019+
}
1020+
""",
1021+
"""
1022+
import java.util.Queue;
1023+
1024+
class B {
1025+
private final Queue<String> widgetDataList;
1026+
public void foo() {
1027+
widgetDataList.add("TEST");
1028+
}
1029+
}
1030+
"""
1031+
)
1032+
);
1033+
}
1034+
1035+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/357")
1036+
@Test
1037+
void danglingReference() {
1038+
rewriteRun(
1039+
//language=java
1040+
java(
1041+
"""
1042+
import java.util.concurrent.ConcurrentLinkedQueue;
1043+
1044+
class B {
1045+
private final ConcurrentLinkedQueue widgetDataList;
1046+
public void foo() {
1047+
widgetDataList.add("TEST");
1048+
}
1049+
}
1050+
""",
1051+
"""
1052+
import java.util.Queue;
1053+
1054+
class B {
1055+
private final Queue widgetDataList;
1056+
public void foo() {
1057+
widgetDataList.add("TEST");
1058+
}
1059+
}
1060+
"""
1061+
)
1062+
);
1063+
}
10041064
}

0 commit comments

Comments
 (0)