From 77ab3fbf61b3176d8d517a52696ac3a0c5f5b683 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 3 Jan 2024 14:37:56 +0100 Subject: [PATCH] `ShortenFullyQualifiedTypeReferences`: Preserve prefix of annotation Fixes: #3870 --- ...ortenFullyQualifiedTypeReferencesTest.java | 36 +++++++++++++++++++ .../org/openrewrite/java/JavaPrinter.java | 2 +- .../ShortenFullyQualifiedTypeReferences.java | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferencesTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferencesTest.java index d304a673742..2c8a5fe0345 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferencesTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferencesTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.ExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.service.ImportService; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; @@ -568,4 +569,39 @@ class Test { ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/3870") + @Test + void typeFullyQualifiedAnnotatedField() { + rewriteRun( + java( + """ + import java.sql.DatabaseMetaData; + import java.util.List; + import java.lang.annotation.*; + + class TypeAnnotationTest { + protected java.sql.@A DatabaseMetaData metadata; + + @Target({ElementType.FIELD, ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) + private @interface A { + } + } + """, + """ + import java.sql.DatabaseMetaData; + import java.util.List; + import java.lang.annotation.*; + + class TypeAnnotationTest { + protected @A DatabaseMetaData metadata; + + @Target({ElementType.FIELD, ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) + private @interface A { + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java index 4beaf0387e0..f74712242d3 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -641,9 +641,9 @@ public J visitForEachLoop(ForEachLoop forEachLoop, PrintOutputCapture

p) { @Override public J visitIdentifier(Identifier ident, PrintOutputCapture

p) { + visitSpace(Space.EMPTY, Space.Location.ANNOTATIONS, p); visit(ident.getAnnotations(), p); beforeSyntax(ident, Space.Location.IDENTIFIER_PREFIX, p); - visitSpace(Space.EMPTY, Space.Location.ANNOTATIONS, p); p.append(ident.getSimpleName()); afterSyntax(ident, p); return ident; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferences.java b/rewrite-java/src/main/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferences.java index 9ae976b43e4..37b42b728a1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferences.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ShortenFullyQualifiedTypeReferences.java @@ -171,7 +171,7 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) { String simpleName = fieldAccess.getSimpleName(); JavaType usedType = usedTypes.get(simpleName); if (type == usedType || signatureBuilder.signature(type).equals(signatureBuilder.signature(usedType))) { - return fieldAccess.getName().withPrefix(fieldAccess.getPrefix()); + return !fieldAccess.getPrefix().isEmpty() ? fieldAccess.getName().withPrefix(fieldAccess.getPrefix()) : fieldAccess.getName(); } else if (!usedTypes.containsKey(simpleName)) { String fullyQualifiedName = ((JavaType.FullyQualified) type).getFullyQualifiedName(); if (!fullyQualifiedName.startsWith("java.lang.")) {