org.javers.common.reflection.TypeResolvingContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.common.reflection;
import org.javers.common.validation.Validate;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;
/**
* @author bartosz walacik
*/
class TypeResolvingContext {
private Map substitutions = new HashMap<>();
Type getSubstitution(Type type) {
if (type instanceof TypeVariable){
Type resolved = substitutions.get(type);
if (resolved instanceof TypeVariable) {
return getSubstitution(resolved);
}
else {
return resolved;
}
}
return null;
}
void addTypeSubstitutions(Class clazz){
Validate.argumentIsNotNull(clazz);
if (clazz == Object.class){
return;
}
Type t = clazz.getGenericSuperclass();
if ( !(t instanceof ParameterizedType)){
return;
}
ParameterizedType genericSuperclass = (ParameterizedType)t;
Class superclass = clazz.getSuperclass();
TypeVariable[] typeParameters = superclass.getTypeParameters();
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
if (typeParameters.length == 0 || actualTypeArguments.length == 0){
return;
}
//both arrays should have the same length, hopefully
for (int i=0; i