-
Notifications
You must be signed in to change notification settings - Fork 497
Added deeper scanning into RemoveUnusedImports #5208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added deeper scanning into RemoveUnusedImports #5208
Conversation
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java
Outdated
Show resolved
Hide resolved
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java
Outdated
Show resolved
Hide resolved
…sedImportsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
| private static Set<J.Annotation> findAllAnnotations(J.CompilationUnit cu) { | ||
| Set<J.Annotation> annotations = new HashSet<>(); | ||
| cu.getClasses().stream() | ||
| .map(cl -> cl.getLeadingAnnotations()) | ||
| .flatMap(List::stream) | ||
| .forEach(annotations::add); | ||
| cu.getTypesInUse().getTypesInUse().stream().forEach(type -> { | ||
| if (type instanceof JavaType.Class) { | ||
| JavaType.Class aClass = (JavaType.Class) type; | ||
| if (aClass.getKind() == JavaType.FullyQualified.Kind.Annotation) { | ||
| FindAnnotations.find(cu, aClass.getFullyQualifiedName()).forEach(annotations::add); | ||
| } | ||
| } | ||
| }); | ||
| return annotations; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have expected something like the following, instead of all the scan methods below; would that be possible here?
Set<J.Annotation> annotations = new JavaIsoVisitor<Set<J.Annotation> >() {
public J.Annotation visitAnnotations(J.Annotations ann, Set<J.Annotation> set) {
set.add(ann);
return ann;
}
}.reduce(cu, new HashSet<>());|
Fixed a couple issues with the recipe yesterday I've now reran the tests you added here without any of the recipe changes, and that means now only this test fails: @Test
void lombokValInLambda() {
rewriteRun(
java(
"""
import lombok.val;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class Foo {
List<String> names = new ArrayList<>();
public String method() {
return names.stream()
.map(n -> {
val name = "";
return n + name;
})
.collect(Collectors.joining(""));
}
}
"""
)
);
}I don't think we'd need all of the changes you've added here just to support that case, and given the conflicts we see here I'm going to close this PR for now. You're welcome to propose a separate fix for that one failing case still. Thanks for helping out! |
What's changed?
Added deeper scanning into RemoveUnusedImports
What's your motivation?
I’ve forked the class and made some improvements for them and wanted to provide feedback of those improvements.
These improvements were made because the recipe was throwing away a lot of imports that were being used for our use case.
Anything in particular you'd like reviewers to focus on?
I’m fairly new to OpenRewrite so I’m not 100% sure that my changes are in line with the best practices. But I tried. Feel free to improve on my code.
Have you considered any alternatives or workarounds?
Use my own cloned class?
Any additional context
I’ve disabled one unit test because it gave a parser error. The same unit test in my project using an older rewrite version works fine. It may help down track where the error may come from.
Checklist