Skip to content

Commit 87ba1d2

Browse files
Pankraz76Vincent Potucektimtebeek
authored
Remove fully qualified JavaDoc references from TypesInUse for RemoveUnusedImports (#5738)
* RemoveUnusedImports * RemoveUnusedImports * RemoveUnusedImports * RemoveUnusedImports * RemoveUnusedImports * Reduce test * Remove fully qualified JavaDoc references from TypesInUse * Apply formatter to text blocks * Also break `isFullyQualifiedJavaDocReference` on J.Block --------- Co-authored-by: Vincent Potucek <vpotucek@me.com> Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 11579e6 commit 87ba1d2

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,54 @@ public abstract class MyMapEntry<K, V> implements Entry<K, V> {
132132
);
133133
}
134134

135+
@Test
136+
void retainImportIfUsedInJavaDoc() {
137+
rewriteRun(
138+
java(
139+
"""
140+
import java.util.Date;
141+
import java.util.List;
142+
143+
/**
144+
* referencing {@link Date} only in doc
145+
*/
146+
class Test {
147+
List list;
148+
}
149+
"""
150+
)
151+
);
152+
}
153+
154+
@Test
155+
void removeImportIfFullyQualifiedInJavaDoc() {
156+
rewriteRun(
157+
java(
158+
"""
159+
import java.util.Date;
160+
import java.util.List;
161+
162+
/**
163+
* referencing {@link java.util.Date} only in doc
164+
*/
165+
class Test2 {
166+
List list;
167+
}
168+
""",
169+
"""
170+
import java.util.List;
171+
172+
/**
173+
* referencing {@link java.util.Date} only in doc
174+
*/
175+
class Test2 {
176+
List list;
177+
}
178+
"""
179+
)
180+
);
181+
}
182+
135183
@Issue("https://github.com/openrewrite/rewrite/issues/1052")
136184
@Test
137185
void usedInJavadocWithThrows() {

rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.openrewrite.java.tree.J;
2525
import org.openrewrite.java.tree.JavaSourceFile;
2626
import org.openrewrite.java.tree.JavaType;
27+
import org.openrewrite.java.tree.Javadoc;
2728

2829
import java.util.IdentityHashMap;
30+
import java.util.Iterator;
2931
import java.util.Objects;
3032
import java.util.Set;
3133

@@ -100,12 +102,31 @@ public J.Lambda.Parameters visitLambdaParameters(J.Lambda.Parameters parameters,
100102
} else {
101103
usedMethods.add((JavaType.Method) javaType);
102104
}
103-
} else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda)) {
104-
// ignore type representing class declaration itself and inferred lambda types
105+
} else if (!(cursor.getValue() instanceof J.ClassDeclaration) &&
106+
!(cursor.getValue() instanceof J.Lambda) &&
107+
!isFullyQualifiedJavaDocReference(cursor)) {
105108
types.add(javaType);
106109
}
107110
}
108111
return javaType;
109112
}
113+
114+
private boolean isFullyQualifiedJavaDocReference(Cursor cursor) {
115+
// Fully qualified Javadoc references are _using_ those types as much as they are just references;
116+
// TypesInUse entries determines what imports are retained, and for fully qualified these can be dropped
117+
if (cursor.getValue() instanceof J.FieldAccess) {
118+
Iterator<Object> path = cursor.getPath();
119+
while (path.hasNext()) {
120+
Object o = path.next();
121+
if (o instanceof Javadoc.Reference) {
122+
return true;
123+
}
124+
if (o instanceof J.Block) {
125+
return false;
126+
}
127+
}
128+
}
129+
return false;
130+
}
110131
}
111132
}

0 commit comments

Comments
 (0)