From f4038c23c528303df0ea9be52622539beac36cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Schn=C3=A9ider?= Date: Wed, 25 Dec 2024 16:44:39 +0100 Subject: [PATCH] Fix Java reflection mapping of generic typed fields. (#4815) --- .../java/org/openrewrite/java/JavaTypeGoat.java | 5 +++-- .../openrewrite/java/JavaTypeMappingTest.java | 16 +++++++++++++--- .../src/main/resources/JavaTypeGoat.java | 5 +++-- .../java/internal/JavaReflectionTypeMapping.java | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeGoat.java b/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeGoat.java index 8e8abd6fe37..64a1d90452f 100644 --- a/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeGoat.java +++ b/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeGoat.java @@ -26,8 +26,9 @@ public abstract class JavaTypeGoat & C> { public static final PT parameterizedField = new PT() { }; - public static final Double PI = 3.14; - public static final double PI_PRIMITIVE = 3.14; + + public static Double PI; + public static double PI_PRIMITIVE; public static abstract class InheritedJavaTypeGoat & C> extends JavaTypeGoat { public InheritedJavaTypeGoat() { diff --git a/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeMappingTest.java b/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeMappingTest.java index 2097273e1a2..1c010267764 100644 --- a/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeMappingTest.java +++ b/rewrite-java-test/src/main/java/org/openrewrite/java/JavaTypeMappingTest.java @@ -25,9 +25,7 @@ import static java.util.Objects.requireNonNull; import static org.assertj.core.api.Assertions.assertThat; -import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.CONTRAVARIANT; -import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.COVARIANT; -import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.INVARIANT; +import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.*; /** * Based on type attribution mappings of [JavaTypeGoat]. @@ -53,6 +51,18 @@ default JavaType firstMethodParameter(String methodName) { return methodType(methodName).getParameterTypes().get(0); } + @Test + default void declaredFields() { + JavaType.Parameterized goat = goatType(); + assertThat(goat.getMembers().stream().filter(m -> m.getName().equals("parameterizedField"))).anySatisfy(field -> + assertThat(field).isInstanceOfSatisfying(JavaType.Variable.class, variable -> + assertThat(variable.getType()).isInstanceOfSatisfying(JavaType.Parameterized.class, param -> + assertThat(param.getTypeParameters()).hasOnlyElementsOfType(JavaType.FullyQualified.class) + ) + ) + ); + } + @Test default void declaringTypeRefersToParameterizedClass() { JavaType.FullyQualified declaringType = methodType("nameShadow").getDeclaringType(); diff --git a/rewrite-java-test/src/main/resources/JavaTypeGoat.java b/rewrite-java-test/src/main/resources/JavaTypeGoat.java index 40e1696a15a..047fa6ada5e 100644 --- a/rewrite-java-test/src/main/resources/JavaTypeGoat.java +++ b/rewrite-java-test/src/main/resources/JavaTypeGoat.java @@ -26,8 +26,9 @@ public abstract class JavaTypeGoat & C> { public static final PT parameterizedField = new PT() { }; - public static final Double PI = 3.14; - public static final double PI_PRIMITIVE = 3.14; + + public static Double PI; + public static double PI_PRIMITIVE; public static abstract class InheritedJavaTypeGoat & C> extends JavaTypeGoat { public InheritedJavaTypeGoat() { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaReflectionTypeMapping.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaReflectionTypeMapping.java index 860d259af24..e242c73e335 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaReflectionTypeMapping.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaReflectionTypeMapping.java @@ -302,7 +302,7 @@ private JavaType.Variable field(Field field) { } } - mappedVariable.unsafeSet(type(field.getDeclaringClass()), type(field.getType()), annotations); + mappedVariable.unsafeSet(type(field.getDeclaringClass()), type(field.getGenericType()), annotations); return mappedVariable; }