Skip to content

Commit

Permalink
Fix Java reflection mapping of generic typed fields. (#4815)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider authored Dec 25, 2024
1 parent 6dc9d50 commit f4038c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public abstract class JavaTypeGoat<T, S extends PT<S> & C> {

public static final PT<TypeA> parameterizedField = new PT<TypeA>() {
};
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<T, U extends PT<U> & C> extends JavaTypeGoat<T, U> {
public InheritedJavaTypeGoat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions rewrite-java-test/src/main/resources/JavaTypeGoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public abstract class JavaTypeGoat<T, S extends PT<S> & C> {

public static final PT<TypeA> parameterizedField = new PT<TypeA>() {
};
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<T, U extends PT<U> & C> extends JavaTypeGoat<T, U> {
public InheritedJavaTypeGoat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit f4038c2

Please sign in to comment.