diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java index 4355021d5ba..c3045457ce1 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java @@ -715,7 +715,12 @@ public String getFullyQualifiedName() { @Override public FullyQualified withFullyQualifiedName(String fullyQualifiedName) { - return type.withFullyQualifiedName(fullyQualifiedName); + FullyQualified qualified = type.withFullyQualifiedName(fullyQualifiedName); + if (type == qualified) { + return this; + } + + return new Parameterized(managedReference, qualified, typeParameters); } @Override diff --git a/rewrite-java/src/test/java/org/openrewrite/java/internal/template/TypeParameterTest.java b/rewrite-java/src/test/java/org/openrewrite/java/internal/template/TypeParameterTest.java index b74b0f4a07b..a909ba47eeb 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/internal/template/TypeParameterTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/internal/template/TypeParameterTest.java @@ -89,6 +89,25 @@ void parameterized(String name) { assertThat(TypeUtils.toString(type)).isEqualTo(name); } + @ParameterizedTest + @ValueSource(strings = { + "java.util.List", + "java.util.Map", + "java.util.List", + "java.util.List", + "java.util.List>", + }) + void parameterizedWithModifierShouldNeverHideParametrizedType(String name) { + TemplateParameterParser parser = new TemplateParameterParser(new CommonTokenStream(new TemplateParameterLexer( + CharStreams.fromString(name)))); + JavaType type = TypeParameter.toFullyQualifiedName(parser.type()); + + JavaType.Parameterized pType = (JavaType.Parameterized) type; + assertThat(pType.withFullyQualifiedName("test")).isInstanceOf(JavaType.Parameterized.class); + assertThat(pType.withFullyQualifiedName("test")).isNotSameAs(pType); + assertThat(pType.withFullyQualifiedName(pType.getFullyQualifiedName())).isSameAs(pType); + } + @ParameterizedTest @ValueSource(strings = { "java.util.List",