From 61dc0d463593f9f787c776465779e70488c6fcbb Mon Sep 17 00:00:00 2001 From: Michael Vitz Date: Thu, 5 Sep 2024 11:23:46 +0200 Subject: [PATCH 1/2] Don't remove referenced annotations from record parameters --- .../openrewrite/java/RemoveUnusedImportsTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java index a23297e0cb4..2f6d059e581 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java @@ -1814,4 +1814,17 @@ public abstract class Foo2 { ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/4473") + @Test + void doesNotRemoveReferencedAnnotationFromRecordParameter() { + rewriteRun( + java( + """ + import java.beans.BeanProperty; + record A(@BeanProperty String a) {} + """ + ) + ); + } } From 6a11dc0ad33bef9174156f2ddb111c45ccae986e Mon Sep 17 00:00:00 2001 From: Michael Vitz Date: Thu, 5 Sep 2024 23:25:28 +0200 Subject: [PATCH 2/2] Use annotation with correct target to be preserved at record components --- .../java/RemoveUnusedImportsTest.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java index 2f6d059e581..2cca5eb84e0 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java @@ -1821,8 +1821,22 @@ void doesNotRemoveReferencedAnnotationFromRecordParameter() { rewriteRun( java( """ - import java.beans.BeanProperty; - record A(@BeanProperty String a) {} + package a; + + import java.lang.annotation.*; + + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.PARAMETER, ElementType.RECORD_COMPONENT }) + public @interface A {} + """ + ), + java( + """ + package b; + + import a.A; + + record B(@A String a) {} """ ) );