Example usage for java.lang.reflect ParameterizedType getRawType

List of usage examples for java.lang.reflect ParameterizedType getRawType

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getRawType.

Prototype

Type getRawType();

Source Link

Document

Returns the Type object representing the class or interface that declared this type.

Usage

From source file:net.jodah.typetools.TypeResolver.java

/**
 * Populates the {@code map} with with variable/argument pairs for the given {@code types}.
 */// w  ww. j a v a 2  s  .  c o m
private static void populateSuperTypeArgs(final Type[] types, final Map<TypeVariable<?>, Type> map,
        boolean depthFirst) {
    for (Type type : types) {
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            if (!depthFirst)
                populateTypeArgs(parameterizedType, map, depthFirst);
            Type rawType = parameterizedType.getRawType();
            if (rawType instanceof Class)
                populateSuperTypeArgs(((Class<?>) rawType).getGenericInterfaces(), map, depthFirst);
            if (depthFirst)
                populateTypeArgs(parameterizedType, map, depthFirst);
        } else if (type instanceof Class) {
            populateSuperTypeArgs(((Class<?>) type).getGenericInterfaces(), map, depthFirst);
        }
    }
}

From source file:org.evosuite.utils.generic.GenericAccessibleObject.java

/**
 * Returns the exact return type of the given method in the given type. This
 * may be different from <tt>m.getGenericReturnType()</tt> when the method
 * was declared in a superclass, or <tt>type</tt> has a type parameter that
 * is used in the return type, or <tt>type</tt> is a raw type.
 *//*from   w  w  w.j  a va  2 s. c o  m*/
protected static Type getTypeFromExactReturnType(ParameterizedType returnType, ParameterizedType type) {
    Map<TypeVariable<?>, Type> typeMap = TypeUtils.getTypeArguments(returnType);
    Type[] actualParameters = new Type[type.getActualTypeArguments().length];
    int num = 0;
    for (TypeVariable<?> parameterType : ((Class<?>) type.getRawType()).getTypeParameters()) {
        //for(Type parameterType : type.getActualTypeArguments()) {
        //   if(parameterType instanceof TypeVariable<?>) {
        boolean replaced = false;
        for (TypeVariable<?> var : typeMap.keySet()) {
            // D'oh! Why the heck do we need this?? 
            if (var.getName().equals(parameterType.getName())) {
                //if(typeMap.containsKey(parameterType)) {
                actualParameters[num] = typeMap.get(var);
                replaced = true;
                break;
                //} else {
            }
        }
        if (!replaced) {
            actualParameters[num] = parameterType;
        }
        //}
        //       } else {
        //          LoggingUtils.getEvoLogger().info("Not a type variable "+parameterType);
        //          actualParameters[num] = parameterType;
        //          }
        num++;
    }

    return new ParameterizedTypeImpl((Class<?>) type.getRawType(), actualParameters, null);
}

From source file:org.apache.camel.maven.DocumentGeneratorMojo.java

private static String getCanonicalName(ParameterizedType fieldType) {
    final Type[] typeArguments = fieldType.getActualTypeArguments();

    final int nArguments = typeArguments.length;
    if (nArguments > 0) {
        final StringBuilder result = new StringBuilder(getCanonicalName((Class<?>) fieldType.getRawType()));
        result.append("&lt;");
        int i = 0;
        for (Type typeArg : typeArguments) {
            if (typeArg instanceof ParameterizedType) {
                result.append(getCanonicalName((ParameterizedType) typeArg));
            } else {
                result.append(getCanonicalName((Class<?>) typeArg));
            }//w w w  .  jav a  2s .c om
            if (++i < nArguments) {
                result.append(',');
            }
        }
        result.append("&gt;");
        return result.toString();
    }

    return getCanonicalName((Class<?>) fieldType.getRawType());
}

From source file:org.soybeanMilk.SbmUtils.java

/**
 * ???/*from   www . ja  v  a2  s  .  com*/
 * <pre>
 * class A&lt;T&gt;{}
 * class B extends A&lt;Integer&gt;{}
 * </pre>
 * <code>extractTypeVariablesInType(B.class, map)</code><code>map</code>
 * <pre>
 * T    -------&gt;    Integer
 * </pre>
 * @param source
 * @param container
 * @date 2012-5-14
 */
private static void extractTypeVariablesInType(Type source, Map<TypeVariable<?>, Type> variableTypesMap) {
    if (source == null)
        return;
    else if (source instanceof Class<?>) {
        Class<?> clazz = (Class<?>) source;

        //?
        Type[] genericInterfaces = clazz.getGenericInterfaces();
        if (genericInterfaces != null) {
            for (Type t : genericInterfaces)
                extractTypeVariablesInType(t, variableTypesMap);
        }

        //
        Type genericSuperType = clazz.getGenericSuperclass();
        Class<?> superClass = clazz.getSuperclass();
        while (superClass != null && !Object.class.equals(superClass)) {
            extractTypeVariablesInType(genericSuperType, variableTypesMap);

            genericSuperType = superClass.getGenericSuperclass();
            superClass = superClass.getSuperclass();
        }

        //
        Class<?> outerClass = clazz;
        while (outerClass.isMemberClass()) {
            Type genericOuterType = outerClass.getGenericSuperclass();
            extractTypeVariablesInType(genericOuterType, variableTypesMap);

            outerClass = outerClass.getEnclosingClass();
        }
    } else if (source instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) source;

        if (isClassType(pt.getRawType())) {
            Type[] actualArgTypes = pt.getActualTypeArguments();
            TypeVariable<?>[] typeVariables = narrowToClass(pt.getRawType()).getTypeParameters();

            for (int i = 0; i < actualArgTypes.length; i++) {
                TypeVariable<?> var = typeVariables[i];
                Type value = actualArgTypes[i];

                //????
                if (value instanceof TypeVariable<?>) {
                    Type actual = variableTypesMap.get(value);

                    if (actual != null)
                        value = actual;
                }

                variableTypesMap.put(var, value);
            }
        }

        //??
        extractTypeVariablesInType(pt.getRawType(), variableTypesMap);
    }
}

From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java

/**
 * Returns information about array or collection types.
 * /* w w w  .  java  2 s .  c  om*/
 * @param type The type to introspect.
 * @param typeState The current TypeState.
 * @param strict True indicates that processing should stop on ambiguous entries; false indicates that null should
 *        be entered.
 * @return A Tuple.Two containing:
 *         <ol>
 *         <li>The enclosed nested Type; String[][] would return String.class, while List<Map<String,String>> will
 *         return the Type of Map<String,String>.</li>
 *         <li>The list of all enclosing classes. String[][] will return [String[][].class, String[].class].</li>
 *         </ol>
 */
protected static Tuple.Two<TypeDefinition, List<ListTypeDefinition>> getArrayDimensions(Type type,
        TypeState typeState, boolean strict) {

    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;

        Type[] types = pt.getActualTypeArguments();
        if (1 == types.length) {
            Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(types[0], typeState,
                    strict);
            temp.v2.add(0, getListTypeDefinition(pt.getRawType(), typeState, strict));
            return temp;
        } else {
            return new Tuple.Two<TypeDefinition, List<ListTypeDefinition>>(
                    getTypeDefinition(pt, typeState, strict), new ArrayList<ListTypeDefinition>());
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;

        Class<?> klass;
        try {
            klass = ClassUtils.forName(gat.toString());
        } catch (ClassNotFoundException e) {
            klass = null;
        } catch (LinkageError e) {
            klass = null;
        }
        if (klass == null && gat.getGenericComponentType() instanceof Class) {
            klass = Array.newInstance((Class<?>) gat.getGenericComponentType(), 0).getClass();
        }
        if (klass == null) {
            throw new WMRuntimeException(MessageResource.JSON_FAILED_GENERICARRAYTYPE, gat,
                    gat.getGenericComponentType());
        }

        Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(
                gat.getGenericComponentType(), typeState, strict);
        temp.v2.add(0, getListTypeDefinition(klass, typeState, strict));

        return temp;
    } else if (type instanceof Class && ((Class<?>) type).isArray()) {
        Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(
                ((Class<?>) type).getComponentType(), typeState, strict);
        temp.v2.add(0, getListTypeDefinition(type, typeState, strict));

        return temp;
    } else if (type instanceof Class) {
        return new Tuple.Two<TypeDefinition, List<ListTypeDefinition>>(
                getTypeDefinition(type, typeState, strict), new ArrayList<ListTypeDefinition>());
    } else {
        throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type,
                type != null ? type.getClass() : null);
    }
}

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

public static Type[] getSuperInterfaceTypes(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType myType = (ParameterizedType) type;
        return ((Class<?>) myType.getRawType()).getGenericInterfaces();
    } else if (type instanceof Class) {
        return ((Class<?>) type).getGenericInterfaces();
    }/*from  ww w.  j  a  v  a  2 s .  co m*/
    if (type instanceof CustomType) {
        return getSuperInterfaceTypes(((CustomType) type).getBaseClass());
    }

    return new Type[0];
}

From source file:org.evosuite.utils.generic.GenericUtils.java

public static Type replaceTypeVariablesWithWildcards(Type targetType) {
    if (targetType instanceof TypeVariable) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) targetType;
        return new WildcardTypeImpl(typeVariable.getBounds(), new Type[] {});
    } else if (targetType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) targetType;
        Type owner = null;/* ww w  . ja  v  a 2  s .c o  m*/
        if (parameterizedType.getOwnerType() != null)
            owner = replaceTypeVariablesWithWildcards(parameterizedType.getOwnerType());
        Type[] currentParameters = parameterizedType.getActualTypeArguments();
        Type[] parameters = new Type[currentParameters.length];
        for (int i = 0; i < parameters.length; i++) {
            parameters[i] = replaceTypeVariablesWithWildcards(currentParameters[i]);
        }
        return new ParameterizedTypeImpl((Class<?>) parameterizedType.getRawType(), parameters, owner);
    }
    return targetType;
}

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

public static Type getSuperType(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType myType = (ParameterizedType) type;
        Type superType = ((Class<?>) myType.getRawType()).getGenericSuperclass();
        if (superType instanceof ParameterizedType) {
            Type[] actualTypeArguments = new Type[((ParameterizedType) superType)
                    .getActualTypeArguments().length];
            for (int i = 0; i < ((ParameterizedType) superType).getActualTypeArguments().length; i++) {
                Type tv2 = ((ParameterizedType) superType).getActualTypeArguments()[i];
                actualTypeArguments[i] = makeInstantiatedType(tv2, type);
            }//from ww  w . j  a va2s. com
            return new ParameterizedTypeImpl(
                    ((Class<?>) ((ParameterizedType) type).getRawType()).getSuperclass(), actualTypeArguments);
        }
    } else if (type instanceof Class) {
        return ((Class) type).getGenericSuperclass();
    }
    if (type instanceof CustomType) {
        return getSuperType(((CustomType) type).getBaseClass());
    }

    return null;
}

From source file:org.solmix.datax.support.InvokerObject.java

private static GenericParameterNode buildGenericParameterTree(ParameterizedType type) {
    GenericParameterNode gpn = new GenericParameterNode();
    Type paramTypes[] = type.getActualTypeArguments();
    if (paramTypes.length == 1 && (paramTypes[0] instanceof Class<?>)) {
        gpn.addClass((Class<?>) paramTypes[0]);
        return gpn;
    }//from w w w .java2  s .  com
    gpn.addClass((Class<?>) type.getRawType());
    for (int j = 0; j < paramTypes.length; j++)
        if (paramTypes[j] instanceof Class) {
            if (gpn.getChildNode() == null)
                gpn.setChildNode(new GenericParameterNode((Class<?>) paramTypes[j]));
            else
                gpn.getChildNode().addClass((Class<?>) paramTypes[j]);
        } else {
            gpn.setChildNode(buildGenericParameterTree((ParameterizedType) paramTypes[j]));
        }

    return gpn;
}

From source file:jef.tools.collection.CollectionUtil.java

/**
 * ?/* www  .j  av a  2 s.co m*/
 * 
 * @param type
 * @return ??null,???
 */
public static Type getComponentType(Type type) {
    if (type instanceof GenericArrayType) {
        return ((GenericArrayType) type).getGenericComponentType();
    } else if (type instanceof Class) {
        Class<?> rawType = (Class<?>) type;
        if (rawType.isArray()) {
            return rawType.getComponentType();
        } else if (Collection.class.isAssignableFrom(rawType)) {
            // ??Object
            return Object.class;
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        Type rawType = pType.getRawType();
        if (isCollection(rawType)) {
            return pType.getActualTypeArguments()[0];
        }
    }
    return null;
}