Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

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

Introduction

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

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

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

/**
 * Returns information about array or collection types.
 * /* w w w  . java  2  s .com*/
 * @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:antre.TypeResolver.java

/**
 * Populates the {@code typeVariableMap} with type arguments and parameters for the given
 * {@code type}.//from www . jav a  2s  . c om
 */
private static void buildTypeVariableMap(ParameterizedType type, Map<TypeVariable<?>, Type> typeVariableMap) {
    if (type.getRawType() instanceof Class) {
        TypeVariable<?>[] typeVariables = ((Class<?>) type.getRawType()).getTypeParameters();
        Type[] typeArguments = type.getActualTypeArguments();

        for (int i = 0; i < typeArguments.length; i++) {
            TypeVariable<?> variable = typeVariables[i];
            Type typeArgument = typeArguments[i];

            if (typeArgument instanceof Class) {
                typeVariableMap.put(variable, typeArgument);
            } else if (typeArgument instanceof GenericArrayType) {
                typeVariableMap.put(variable, typeArgument);
            } else if (typeArgument instanceof ParameterizedType) {
                typeVariableMap.put(variable, typeArgument);
            } else if (typeArgument instanceof TypeVariable) {
                TypeVariable<?> typeVariableArgument = (TypeVariable<?>) typeArgument;
                Type resolvedType = typeVariableMap.get(typeVariableArgument);
                if (resolvedType == null)
                    resolvedType = resolveBound(typeVariableArgument);
                typeVariableMap.put(variable, resolvedType);
            }
        }
    }
}

From source file:com.hihframework.core.utils.ReflectUtil.java

/**
 * //from   w ww  . j av a 2  s  .c o  m
 * @param obj
 * @param fieldName
 * @return
 * @author Xiaojf
 * @since 2011-9-9
 */
public static Class<?> getGenericType(Class<?> obj, String fieldName) {
    try {
        Field field = getDeclaredField(obj, fieldName);
        if (field != null) {
            ParameterizedType pt = (ParameterizedType) field.getGenericType();
            if (pt != null && pt.getActualTypeArguments() != null) {
                Type[] types = pt.getActualTypeArguments();
                if (types.length > 0) {
                    return (Class<?>) types[0];
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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.java  2s. 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.jdto.impl.BeanPropertyUtils.java

/**
 * Get the type parameter out of a generic type.
 * @param genericType/*  www.jav a  2  s  . c  o m*/
 * @return the type parameter of Object.class
 */
static Class getGenericTypeParameter(Type genericType) {
    ParameterizedType fieldParamType = null;

    if (genericType instanceof ParameterizedType) {
        fieldParamType = (ParameterizedType) genericType;
    }

    if (fieldParamType == null) {
        return Object.class;
    }

    Class typeParameter = (Class) fieldParamType.getActualTypeArguments()[0];

    return typeParameter;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Type getElementType(Type type) {
    if (isArray(type)) {
        if (type instanceof Class<?>) {
            Class<?> clazz = (Class<?>) type;
            return clazz.getComponentType();
        }/*from w w w.  j a v  a  2s .c o  m*/
        if (type instanceof GenericArrayType) {
            GenericArrayType arrayType = (GenericArrayType) type;
            return arrayType.getGenericComponentType();
        }
        throw new UnsupportedOperationException();
    }
    if (isCollection(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] typeArguments = parameterizedType.getActualTypeArguments();
            if (typeArguments.length != 1) {
                throw new UnsupportedOperationException();
            }
            return typeArguments[0];
        }
        if (type instanceof Class<?>) {
            //this is something like a plain List(), no generic information, so type unknown
            return null;
        }
        throw new UnsupportedOperationException();
    }
    if (isMap(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] typeArguments = parameterizedType.getActualTypeArguments();
            if (typeArguments.length != 2) {
                throw new UnsupportedOperationException();
            }
            return typeArguments[1];
        }
        if (type instanceof Class<?>) {
            //this is something like a plain Map(), no generic information, so type unknown
            return null;
        }
    }
    throw new UnsupportedOperationException();
}

From source file:org.batoo.jpa.common.reflect.ReflectHelper.java

/**
 * Returns the actual generic type of a class's type parameter of the <code>member</code>.
 * <p>/*  ww  w. j  a  v a2  s.  co m*/
 * if the <code>member</code> is a field then field's generic types are checked. Otherwise the <code>member</code> is treated a a method
 * and its return type's is checked.
 * <p>
 * 
 * @param member
 *            the member
 * @param index
 *            the index number of the generic parameter
 * @return the class of generic type
 * 
 * @param <X>
 *            the type of the class
 * 
 * @since $version
 * @author hceylan
 */
@SuppressWarnings("unchecked")
public static <X> Class<X> getGenericType(Member member, int index) {
    Type type;

    if (member instanceof Field) {
        final Field field = (Field) member;
        type = field.getGenericType();

    } else {
        final Method method = (Method) member;
        type = method.getGenericReturnType();
    }

    // if not a parameterized type return null
    if (!(type instanceof ParameterizedType)) {
        return null;
    }

    final ParameterizedType parameterizedType = (ParameterizedType) type;

    final Type[] types = parameterizedType != null ? parameterizedType.getActualTypeArguments() : null;

    return (Class<X>) ((types != null) && (index < types.length) ? types[index] : null);
}

From source file:com.revolsys.util.JavaBeanUtil.java

public static Class<?> getTypeParameterClass(final Method method, final Class<?> expectedRawClass) {
    final Type resultListReturnType = method.getGenericReturnType();
    if (resultListReturnType instanceof ParameterizedType) {
        final ParameterizedType parameterizedType = (ParameterizedType) resultListReturnType;
        final Type rawType = parameterizedType.getRawType();
        if (rawType == expectedRawClass) {
            final Type[] typeArguments = parameterizedType.getActualTypeArguments();
            if (typeArguments.length == 1) {
                final Type resultType = typeArguments[0];
                if (resultType instanceof Class<?>) {
                    final Class<?> resultClass = (Class<?>) resultType;
                    return resultClass;
                } else {
                    throw new IllegalArgumentException(method.getName() + " must return "
                            + expectedRawClass.getName() + " with 1 generic type parameter that is a class");
                }/*from   w  ww  . j  a v  a  2 s .c o  m*/
            }
        }
    }
    throw new IllegalArgumentException(method.getName() + " must return " + expectedRawClass.getName()
            + " with 1 generic class parameter");
}

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 . j  av a2s.c om
            if (++i < nArguments) {
                result.append(',');
            }
        }
        result.append("&gt;");
        return result.toString();
    }

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

From source file:de.bund.bva.pliscommon.serviceapi.core.serviceimpl.MappingHelper.java

/**
 * Mappt eine Map./*from  w  w  w . ja  v  a 2  s . c  o  m*/
 * 
 * @param mapper
 *            der Dozer-Mapper
 * @param source
 *            eine Map
 * @param parDestinationType
 *            der Typ der Ziel-Map
 * @param result
 *            die instanziierte, leere Ziel-Map
 * @return die gemappte Map
 */
static Map<Object, Object> mapMap(Mapper mapper, Object source, ParameterizedType parDestinationType,
        Map<Object, Object> result) {
    if (!(source instanceof Map)) {
        throw new IllegalArgumentException("Ein Objekt vom Typ " + source.getClass() + " kann nicht auf "
                + parDestinationType + " gemappt werden");
    }
    Map<?, ?> sourceMap = (Map<?, ?>) source;
    Type destKeyType = parDestinationType.getActualTypeArguments()[0];
    Type destValueType = parDestinationType.getActualTypeArguments()[1];

    for (Map.Entry<?, ?> entry : sourceMap.entrySet()) {
        Object key = map(mapper, entry.getKey(), destKeyType);
        Object value = map(mapper, entry.getValue(), destValueType);
        result.put(key, value);
    }

    return result;
}