Fixed type definition

This commit is contained in:
Luca Frosini 2019-11-08 18:15:42 +01:00
parent a17e5f7b4b
commit 22abc6ebfc
2 changed files with 29 additions and 4 deletions

View File

@ -91,7 +91,7 @@ public class TypeDefinitionImpl extends ERImpl implements TypeDefinition {
return properties;
}
protected static Class<?> getGenericClass(java.lang.reflect.Type type){
protected Class<?> getGenericClass(java.lang.reflect.Type type){
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
java.lang.reflect.Type[] bounds = typeVariable.getBounds();
java.lang.reflect.Type t = bounds[0];

View File

@ -1,6 +1,7 @@
package org.gcube.informationsystem.types.impl.relations;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
@ -30,20 +31,23 @@ public final class RelationTypeDefinitionImpl
super();
}
@SuppressWarnings("unchecked")
public RelationTypeDefinitionImpl(Class<? extends BaseRelation<?, ?>> clz) {
super(clz);
if(IsRelatedTo.class.isAssignableFrom(clz)){
@SuppressWarnings("unchecked")
Class<? extends IsRelatedTo<? extends Resource, ? extends Resource>> c = (Class<? extends IsRelatedTo<? extends Resource, ? extends Resource>>) clz;
this.superClasses = retrieveSuperClasses(c, IsRelatedTo.class, Relation.NAME);
} else if(ConsistsOf.class.isAssignableFrom(clz)) {
@SuppressWarnings("unchecked")
Class<? extends ConsistsOf<? extends Resource, ? extends Facet>> c = (Class<? extends ConsistsOf<? extends Resource, ? extends Facet>>) clz;
this.superClasses = retrieveSuperClasses(c, ConsistsOf.class, Relation.NAME);
} else if(RelationTypeDefinition.class.isAssignableFrom(clz)){
@SuppressWarnings("unchecked")
Class<RelationTypeDefinition> c = (Class<RelationTypeDefinition>) clz;
this.superClasses = retrieveSuperClasses(c, RelationTypeDefinition.class, BaseRelation.NAME);
} else if(IsParentOf.class.isAssignableFrom(clz)){
@SuppressWarnings("unchecked")
Class<? extends IsParentOf> c = (Class<? extends IsParentOf>) clz;
this.superClasses = retrieveSuperClasses(c, IsParentOf.class, BaseRelation.NAME);
} else if(BaseRelation.class.isAssignableFrom(clz)){
@ -52,11 +56,32 @@ public final class RelationTypeDefinitionImpl
throw new RuntimeException("Type Hierachy Error");
}
java.lang.reflect.Type[] typeParameters = clz.getTypeParameters();
discoverSourceAndTarget(clz);
}
@SuppressWarnings("unchecked")
protected Type[] getParametersFromSuperClasses(Class<? extends BaseRelation<?, ?>> clz) {
for(Type t : clz.getGenericInterfaces()){
if(t instanceof ParameterizedType) {
if(BaseRelation.class.isAssignableFrom((Class<?>) ((ParameterizedType) t).getRawType())) {
return ((ParameterizedType) t).getActualTypeArguments();
}
}else if(BaseRelation.class.isAssignableFrom((Class<?>) t)){
return getParametersFromSuperClasses((Class<? extends BaseRelation<?, ?>>) t);
}
}
throw new RuntimeException("Unable to find Generic Parameters From SuperClasses to set source and target");
}
@SuppressWarnings("unchecked")
private void discoverSourceAndTarget(Class<? extends BaseRelation<?, ?>> clz) {
Type[] typeParameters = clz.getTypeParameters();
Class<? extends BaseEntity> sourceClass;
Class<? extends BaseEntity> targetClass;
if(typeParameters.length==0) {
typeParameters = ((ParameterizedType) clz.getGenericInterfaces()[0]).getActualTypeArguments();
typeParameters = getParametersFromSuperClasses(clz);
sourceClass = (Class<? extends BaseEntity>) typeParameters[0];
targetClass = (Class<? extends BaseEntity>) typeParameters[1];
}else {