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 @@ -132,6 +132,54 @@ public abstract class MyMapEntry<K, V> implements Entry<K, V> {
);
}

@Test
void retainImportIfUsedInJavaDoc() {
rewriteRun(
java(
"""
import java.util.Date;
import java.util.List;

/**
* referencing {@link Date} only in doc
*/
class Test {
List list;
}
"""
)
);
}

@Test
void removeImportIfFullyQualifiedInJavaDoc() {
rewriteRun(
java(
"""
import java.util.Date;
import java.util.List;

/**
* referencing {@link java.util.Date} only in doc
*/
class Test2 {
List list;
}
""",
"""
import java.util.List;

/**
* referencing {@link java.util.Date} only in doc
*/
class Test2 {
List list;
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1052")
@Test
void usedInJavadocWithThrows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Javadoc;

import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -100,12 +102,31 @@ public J.Lambda.Parameters visitLambdaParameters(J.Lambda.Parameters parameters,
} else {
usedMethods.add((JavaType.Method) javaType);
}
} else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda)) {
// ignore type representing class declaration itself and inferred lambda types
} else if (!(cursor.getValue() instanceof J.ClassDeclaration) &&
!(cursor.getValue() instanceof J.Lambda) &&
!isFullyQualifiedJavaDocReference(cursor)) {
types.add(javaType);
}
}
return javaType;
}

private boolean isFullyQualifiedJavaDocReference(Cursor cursor) {
// Fully qualified Javadoc references are _using_ those types as much as they are just references;
// TypesInUse entries determines what imports are retained, and for fully qualified these can be dropped
if (cursor.getValue() instanceof J.FieldAccess) {
Iterator<Object> path = cursor.getPath();
while (path.hasNext()) {
Comment on lines +118 to +119
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capturing a note from Slack: some performance concerns here due to the use of cursor.getPath().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed again; consensus was it looks ok now with the early return for blocks.

Object o = path.next();
if (o instanceof Javadoc.Reference) {
return true;
}
if (o instanceof J.Block) {
return false;
}
}
}
return false;
}
}
}