All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javers.common.reflection.TypeResolvingContext Maven / Gradle / Ivy

There is a newer version: 7.6.2
Show newest version
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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy