Example usage for java.lang.reflect TypeVariable getName

List of usage examples for java.lang.reflect TypeVariable getName

Introduction

In this page you can find the example usage for java.lang.reflect TypeVariable getName.

Prototype

String getName();

Source Link

Document

Returns the name of this type variable, as it occurs in the source code.

Usage

From source file:org.openmainframe.ade.impl.PropertyAnnotation.java

/**
 * Find out what are the concrete classes used in an offspring class for the generic placeholders of a base class/interface
 * //from w  ww. j  a  v  a  2 s  . co m
 * @param <T> base class type
 * @param offspring class or interface subclassing or extending the base class
 * @param base class with generic arguments
 * @param actualArgs the actual type arguments passed to the offspring class (omit unless useful)
 * @return actual generic type arguments, must match the type parameters of the offspring class. If omitted, the
 * type parameters will be used instead.
 */
@SuppressWarnings("unchecked")
public static <T> Type[] resolveActualTypeArgs(Class<? extends T> offspring, Class<T> base,
        Type... actualArgs) {

    //  If actual types are omitted, the type parameters will be used instead.
    if (actualArgs.length == 0) {
        actualArgs = offspring.getTypeParameters();
    }
    // map generic parameters into the actual types
    final Map<String, Type> genericVariables = new TreeMap<String, Type>();
    for (int i = 0; i < actualArgs.length; i++) {
        final TypeVariable<?> typeVariable = (TypeVariable<?>) offspring.getTypeParameters()[i];
        genericVariables.put(typeVariable.getName(), actualArgs[i]);
    }

    // Find direct ancestors (superclass, interfaces)
    final List<Type> ancestors = new LinkedList<Type>();
    if (offspring.getGenericSuperclass() != null) {
        ancestors.add(offspring.getGenericSuperclass());
    }
    for (Type t : offspring.getGenericInterfaces()) {
        ancestors.add(t);
    }

    // Recurse into ancestors (superclass, interfaces)
    for (Type type : ancestors) {
        if (type instanceof Class<?>) {
            // ancestor is non-parameterized. Recurse only if it matches the base class.
            final Class<?> ancestorClass = (Class<?>) type;
            if (base.isAssignableFrom(ancestorClass)) {
                final Type[] result = resolveActualTypeArgs((Class<? extends T>) ancestorClass, base);
                if (result != null) {
                    return result;
                }
            }
        }
        if (type instanceof ParameterizedType) {
            // ancestor is parameterized. Recurse only if the raw type matches the base class.
            final ParameterizedType parameterizedType = (ParameterizedType) type;
            final Type rawType = parameterizedType.getRawType();
            if (rawType instanceof Class<?>) {
                final Class<?> rawTypeClass = (Class<?>) rawType;
                if (base.isAssignableFrom(rawTypeClass)) {

                    // loop through all type arguments and replace type variables with the actually known types
                    final List<Type> resolvedTypes = new LinkedList<Type>();
                    for (Type t : parameterizedType.getActualTypeArguments()) {
                        if (t instanceof TypeVariable<?>) {
                            final Type resolvedType = genericVariables.get(((TypeVariable<?>) t).getName());
                            resolvedTypes.add(resolvedType != null ? resolvedType : t);
                        } else if (t instanceof ParameterizedType) {
                            final ParameterizedType pType = (ParameterizedType) t;
                            final Type resolvedPType = new ResolvedParameterizedType(pType, genericVariables);
                            resolvedTypes.add(resolvedPType);
                        } else {
                            resolvedTypes.add(t);
                        }
                    }

                    final Type[] result = resolveActualTypeArgs((Class<? extends T>) rawTypeClass, base,
                            resolvedTypes.toArray(new Type[] {}));
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
    }

    // we have a result if we reached the base class.
    return offspring.equals(base) ? actualArgs : null;
}

From source file:org.romaframework.core.schema.SchemaHelper.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Class<?> resolveClassFromType(Type type, ParameterizedType params) {
    if (type instanceof Class<?>)
        return (Class<?>) type;
    if (type instanceof ParameterizedType) {
        return resolveClassFromType(((ParameterizedType) type).getRawType(), (ParameterizedType) type);
    }/*from ww w  .ja  v  a  2 s  .  c  o m*/
    if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Class<?> arrItemp = resolveClassFromType(gat.getGenericComponentType(), null);
        return Array.newInstance(arrItemp, 0).getClass();
    }
    if (type instanceof TypeVariable<?>) {
        TypeVariable<?> t = (TypeVariable<?>) type;
        if (params != null) {
            Class<?> cl = resolveClassFromType(params.getRawType(), null);
            if (cl != null) {

                TypeVariable<Class<?>>[] var = ((Class) cl).getTypeParameters();
                int i = 0;
                for (; i < var.length; i++) {
                    if (var[i].getName().equals(t.getName())) {
                        return resolveClassFromType(params.getActualTypeArguments()[i],
                                resolveParameterizedType(params.getOwnerType()));
                    }
                }
            }
        }
        Type[] bounds = t.getBounds();
        if (bounds.length == 1)
            return resolveClassFromType(bounds[0], params);
    }
    if (type instanceof WildcardType) {
        // TODO:
    }
    return null;
}

From source file:org.vulpe.commons.util.VulpeReflectUtil.java

/**
 * Returns position of TypeVariable in list of getTypeParameters on class.
 *
 * @param typeVariable//  www.  j  a  va 2 s  .  c  o  m
 * @return
 */
private static int getIndexTypeVariable(final TypeVariable<?> typeVariable) {
    int index = -1;
    for (final TypeVariable<?> typeVariable2 : typeVariable.getGenericDeclaration().getTypeParameters()) {
        ++index;
        if (typeVariable.getName().equals(typeVariable2.getName())) {
            break;
        }
    }
    return index;
}

From source file:org.wrml.runtime.syntax.SyntaxHandler.java

public static Class<?> getSyntaxType(final Class<?> syntaxHandlerClass) {

    final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(syntaxHandlerClass,
            SyntaxHandler.class);

    for (final TypeVariable<?> typeVar : typeArguments.keySet()) {

        final String typeVarName = typeVar.getName();
        if (SyntaxHandler.TYPE_VARIABLE_NAME.equals(typeVarName)) {
            return (Class<?>) typeArguments.get(typeVar);
        } else {/* w  w w.  j  av a2  s.  c  o m*/
            throw new RuntimeException("Unexpected type variable name  \"" + typeVarName
                    + "\" in SyntaxHandler class (" + syntaxHandlerClass + ")");
        }

    }

    return null;

}