Skip to content

Commit

Permalink
Only extract annotations with template comment (#4442)
Browse files Browse the repository at this point in the history
- Fixes #4441
  • Loading branch information
timtebeek authored Aug 24, 2024
1 parent 1777317 commit 959838c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ public List<J.Annotation> listAnnotations(JavaSourceFile cu) {

@Override
public J.Annotation visitAnnotation(J.Annotation annotation, Integer integer) {
J.Annotation withoutTemplateComment = annotation.withComments(
ListUtils.concatAll(
ListUtils.map(getCursor().getParentOrThrow().<J>getValue().getComments(), this::filterTemplateComment),
ListUtils.map(annotation.getComments(), this::filterTemplateComment)
));
annotations.add(withoutTemplateComment);
List<Comment> combinedComments = ListUtils.concatAll(
getCursor().getParentOrThrow().<J>getValue().getComments(),
annotation.getComments());
List<Comment> filteredComments = ListUtils.map(combinedComments, this::filterTemplateComment);
if (combinedComments != filteredComments) {
annotations.add(annotation.withComments(filteredComments));
}
return annotation;
}
}.visit(cu, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
Expand All @@ -39,7 +40,7 @@ class A {
String testMethod() {}
}
""",
"""
"""
import lombok.NonNull;
class A {
Expand All @@ -65,7 +66,7 @@ class A {
String testMethod() {}
}
""",
"""
"""
import lombok.NonNull;
class A {
Expand Down Expand Up @@ -101,6 +102,38 @@ String testMethod() {}
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4441")
void methodWithAnnotatedParameter() {
rewriteRun(
spec -> spec.recipe(new ReplaceAnnotation("@org.jetbrains.annotations.NotNull", "@lombok.NonNull", null)),
java(
"""
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class A {
void methodName(
@Nullable final boolean valueVar) {
@NotNull final String nullableVar = "test";
}
}
""",
"""
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;
class A {
void methodName(
@Nullable final boolean valueVar) {
@NonNull final String nullableVar = "test";
}
}
"""
)
);
}
}

@Nested
Expand Down

0 comments on commit 959838c

Please sign in to comment.