Skip to content

Commit

Permalink
GH-492 - Overwrite generic fields in FieldInfo of concrete classes.
Browse files Browse the repository at this point in the history
Take all declared fields of the hierarchy into consideration when building a FieldsInfo for concrete classes. Resolve type of generics fields in subtype.
  • Loading branch information
michael-simons committed Dec 11, 2018
1 parent 1732ac3 commit 34b351c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/org/neo4j/ogm/metadata/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.neo4j.ogm.metadata;

import static org.neo4j.ogm.metadata.reflect.GenericUtils.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -98,8 +100,8 @@ public class FieldInfo {
public FieldInfo(ClassInfo classInfo, Field field, String typeParameterDescriptor, ObjectAnnotations annotations, boolean isSupportedNativeType) {
this.containingClassInfo = classInfo;
this.field = field;
this.fieldType = field.getType();
this.isArray = field.getType().isArray();
this.fieldType = isGenericField(field) ? findFieldType(field, classInfo.getUnderlyingClass()) : field.getType();
this.isArray = fieldType.isArray();
this.name = field.getName();
this.descriptor = field.getType().getTypeName();
this.typeParameterDescriptor = typeParameterDescriptor;
Expand Down
28 changes: 26 additions & 2 deletions core/src/main/java/org/neo4j/ogm/metadata/FieldsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.Transient;
import org.neo4j.ogm.driver.TypeSystem;
import org.neo4j.ogm.metadata.reflect.GenericUtils;

/**
* @author Vince Bickers
Expand All @@ -42,10 +45,17 @@ public class FieldsInfo {
this.fields = new HashMap<>();
}

public FieldsInfo(ClassInfo classInfo, Class<?> cls, TypeSystem typeSystem) {
FieldsInfo(ClassInfo classInfo, Class<?> clazz, TypeSystem typeSystem) {
this.fields = new HashMap<>();

for (Field field : cls.getDeclaredFields()) {
// Fields influenced by this class are all all declared fields plus
// all generics fields of possible superclasses that resolve to concrete
// types through this class.
List<Field> allFieldsInfluencedByThisClass = new ArrayList<>();
allFieldsInfluencedByThisClass.addAll(getGenericFieldsInHierarchyOf(clazz));
allFieldsInfluencedByThisClass.addAll(Arrays.asList(clazz.getDeclaredFields()));

for (Field field : allFieldsInfluencedByThisClass) {
final int modifiers = field.getModifiers();
if (!Modifier.isTransient(modifiers) && !Modifier.isFinal(modifiers) && !Modifier.isStatic(modifiers)) {
ObjectAnnotations objectAnnotations = ObjectAnnotations.of(field.getDeclaredAnnotations());
Expand Down Expand Up @@ -89,6 +99,20 @@ public FieldsInfo(ClassInfo classInfo, Class<?> cls, TypeSystem typeSystem) {
}
}

private static List<Field> getGenericFieldsInHierarchyOf(Class<?> clazz) {

List<Field> genericFieldsInHierarchy = new ArrayList<>();
Class<?> currentClass = clazz.getSuperclass();
while(currentClass != null) {
Stream.of(currentClass.getDeclaredFields())
.filter(GenericUtils::isGenericField)
.forEach(genericFieldsInHierarchy::add);
currentClass = currentClass.getSuperclass();
}

return genericFieldsInHierarchy;
}

public Collection<FieldInfo> fields() {
return fields.values();
}
Expand Down

0 comments on commit 34b351c

Please sign in to comment.