Example usage for java.lang.reflect GenericDeclaration getTypeParameters

List of usage examples for java.lang.reflect GenericDeclaration getTypeParameters

Introduction

In this page you can find the example usage for java.lang.reflect GenericDeclaration getTypeParameters.

Prototype

public TypeVariable<?>[] getTypeParameters();

Source Link

Document

Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.

Usage

From source file:net.firejack.platform.core.utils.Factory.java

public int getParameterTypeDeclarationIndex(TypeVariable typeVariable) {
    GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
    TypeVariable[] typeVariables = genericDeclaration.getTypeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        if (typeVariables[i].equals(typeVariable)) {
            return i;
        }//  w w  w  .  ja  v  a 2  s. c  o m
    }
    throw new IllegalStateException(
            "Argument " + typeVariable.toString() + " is not found in " + genericDeclaration.toString() + ".");
}

From source file:org.openflexo.antar.binding.TypeUtils.java

/**
 * Build instanciated DMType considering supplied type is generic (contains TypeVariable definitions) Returns a clone of DMType where
 * all references to TypeVariable are replaced by values defined in context type. For example, given type=Enumeration<E> and
 * context=Vector<String>, returns Enumeration<String> If supplied type is not generic, return type value (without cloning!)
 * //from  w  w w  . j  av a  2s . c  om
 * @param type
 *            : type to instanciate
 * @param context
 *            : context used to instanciate type
 * @return
 */
public static Type makeInstantiatedType(Type type, Type context) {
    if (type == null) {
        return null;
    }

    if (!isGeneric(type)) {
        return type;
    }

    if (type instanceof ParameterizedType) {
        Type[] actualTypeArguments = new Type[((ParameterizedType) type).getActualTypeArguments().length];
        for (int i = 0; i < ((ParameterizedType) type).getActualTypeArguments().length; i++) {
            actualTypeArguments[i] = makeInstantiatedType(
                    ((ParameterizedType) type).getActualTypeArguments()[i], context);
        }
        return new ParameterizedTypeImpl((Class) ((ParameterizedType) type).getRawType(), actualTypeArguments);
    }

    if (type instanceof GenericArrayType) {
        return new GenericArrayTypeImpl(
                makeInstantiatedType(((GenericArrayType) type).getGenericComponentType(), context));
    }

    if (type instanceof TypeVariable) {
        TypeVariable<GenericDeclaration> tv = (TypeVariable<GenericDeclaration>) type;
        GenericDeclaration gd = tv.getGenericDeclaration();
        // System.out.println("Found type variable "+tv+" name="+tv.getName()+" GD="+tv.getGenericDeclaration());
        if (gd instanceof Class) {
            if (context instanceof ParameterizedType) {
                for (int i = 0; i < gd.getTypeParameters().length; i++) {
                    if (gd.getTypeParameters()[i].equals(tv)) {
                        // Found matching parameterized type
                        if (i < ((ParameterizedType) context).getActualTypeArguments().length) {
                            // logger.info("********* return instantiatedType for "+type+" context="+context+" gd="+gd);
                            if (!((ParameterizedType) context).getRawType().equals(gd)) {
                                return makeInstantiatedType(type, getSuperType(context));
                            }
                            return ((ParameterizedType) context).getActualTypeArguments()[i];
                        } else {
                            logger.warning("Could not retrieve parameterized type " + tv + " with context "
                                    + simpleRepresentation(context));
                            return type;
                        }
                    }
                }
            } else if (context instanceof Class && ((Class) context).getGenericSuperclass() != null) {
                return makeInstantiatedType(type, ((Class) context).getGenericSuperclass());
            }
        } else if (gd instanceof Method) {
            return type;
        }
        logger.warning("Not found type variable " + tv + " in context " + context + " GenericDeclaration="
                + tv.getGenericDeclaration());
        // throw new InvalidKeyValuePropertyException("Not found type variable "+tv+" in context "+context);
        return type;
    }

    if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] upperBounds = new Type[wt.getUpperBounds().length];
        for (int i = 0; i < wt.getUpperBounds().length; i++) {
            upperBounds[i] = makeInstantiatedType(wt.getUpperBounds()[i], context);
        }
        Type[] lowerBounds = new Type[wt.getLowerBounds().length];
        for (int i = 0; i < wt.getLowerBounds().length; i++) {
            lowerBounds[i] = makeInstantiatedType(wt.getLowerBounds()[i], context);
        }
        return new WilcardTypeImpl(upperBounds, lowerBounds);
    }

    logger.warning("Unexpected " + type);
    return type;

}