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:org.opencb.commons.utils.CommandLineUtils.java

private static String getType(ParameterDescription parameterDescription) {
    String type = "";
    if (parameterDescription.getParameter().arity() == 0) {
        return type;
    } else {/* ww  w.  j a v  a  2  s . co m*/
        if (parameterDescription.isDynamicParameter()) {
            Type genericType = parameterDescription.getParameterized().getGenericType();
            if (genericType instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) genericType;
                Type rawType = parameterizedType.getRawType();
                if (rawType instanceof Class && Map.class.isAssignableFrom((Class) rawType)) {
                    String key = getType(parameterizedType.getActualTypeArguments()[0]);
                    String assignment = parameterDescription.getParameter().getAssignment();
                    String value = getType(parameterizedType.getActualTypeArguments()[1]);
                    type = key + assignment + value;
                }
            } else {
                type = getType(genericType);
            }
        } else {
            Type genericType = parameterDescription.getParameterized().getGenericType();
            type = getType(genericType);
            if (type.equals("BOOLEAN")
                    && parameterDescription.getParameterized().getParameter().arity() == -1) {
                type = "";
            }
        }
    }
    return type;
}

From source file:com.github.rvesse.airline.Accessor.java

private static Class<?> getRawType(Type type) {
    if (type instanceof Class) {
        return (Class<?>) type;
    }/*w w w .j a v a2  s.  c  o  m*/
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        return getRawType(parameterizedType.getRawType());
    }
    return null;
}

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

/**
 * Bildet ein Objekt mithilfe von Dozer auf einen gewnschten Zieltyp ab. Im Gegensatz zu
 * {@link Mapper#map(Object, Class)} knnen als Zieltyp auch generische Collections, String und primitive
 * Typen bergeben werden./*from w  ww.java 2  s.c  om*/
 * 
 * @param mapper
 *            der Dozer-Mapper
 * @param source
 *            das zu mappende Objekt
 * @param destinationType
 *            der Zieltyp
 * @return das gemappte Objekt
 */
@SuppressWarnings("unchecked")
public static Object map(Mapper mapper, Object source, Type destinationType) {
    if (source == null) {
        return null;
    }

    if (destinationType instanceof ParameterizedType) {
        ParameterizedType parDestinationType = (ParameterizedType) destinationType;

        Class<?> rawClass = (Class<?>) parDestinationType.getRawType();
        if (List.class.isAssignableFrom(rawClass)) {
            return mapCollection(mapper, source, parDestinationType, new ArrayList<Object>());
        } else if (SortedSet.class.isAssignableFrom(rawClass)) {
            return mapCollection(mapper, source, parDestinationType, new TreeSet<Object>());
        } else if (Set.class.isAssignableFrom(rawClass)) {
            return mapCollection(mapper, source, parDestinationType, new HashSet<Object>());
        } else if (SortedMap.class.isAssignableFrom(rawClass)) {
            return mapMap(mapper, source, parDestinationType, new TreeMap<Object, Object>());
        } else if (Map.class.isAssignableFrom(rawClass)) {
            return mapMap(mapper, source, parDestinationType, new HashMap<Object, Object>());
        }

        destinationType = parDestinationType.getRawType();
    }

    if (destinationType instanceof GenericArrayType) {
        if (!source.getClass().isArray()) {
            throw new IllegalArgumentException("Ein Mapping auf den Array-Typ " + destinationType
                    + " wird nicht untersttzt, wenn das Quellobjekt kein Array ist. Typ des Quellobjekts: "
                    + source.getClass());
        }

        // wir werden im Array Element pro Element mappen
        Type elementType = ((GenericArrayType) destinationType).getGenericComponentType();
        Object[] sourceArray = (Object[]) source;
        Object[] destinationArray = (Object[]) Array.newInstance((Class<?>) elementType, sourceArray.length);
        for (int i = 0; i < sourceArray.length; i++) {
            destinationArray[i] = MappingHelper.map(mapper, sourceArray[i], elementType);
        }
        return destinationArray;
    } else if ((destinationType instanceof Class<?>) && ((Class<?>) destinationType).isArray()) {
        if (!source.getClass().isArray()) {
            throw new IllegalArgumentException("Ein Mapping auf den Array-Typ " + destinationType
                    + " wird nicht untersttzt, wenn das Quellobjekt kein Array ist. Typ des Quellobjekts: "
                    + source.getClass());
        }
        Class<?> destinationTypeClass = (Class<?>) destinationType;
        // wir werden im Array Element pro Element mappen
        Type elementType = destinationTypeClass.getComponentType();
        Object[] sourceArray = (Object[]) source;
        Object[] destinationArray = (Object[]) Array.newInstance((Class<?>) elementType, sourceArray.length);
        for (int i = 0; i < sourceArray.length; i++) {
            destinationArray[i] = MappingHelper.map(mapper, sourceArray[i], elementType);
        }
        return destinationArray;
    }

    if (!(destinationType instanceof Class<?>)) {
        throw new IllegalArgumentException(
                "Ein Mapping auf Typ " + destinationType + " wird nicht untersttzt");
    }

    Class<?> destinationClass = (Class<?>) destinationType;

    if (ClassUtils.isPrimitiveOrWrapper(destinationClass) || MAPPING_BLACKLIST.contains(destinationClass)) {
        return source;
    } else if (destinationClass.isEnum()) {
        // wir mssen auf dieser Ebene Enums leider manuell mappen
        if (!(source instanceof Enum)) {
            throw new IllegalArgumentException("Ein Mapping auf ein Enum " + destinationClass
                    + " wird nicht untersttzt, da das Quellobjekt kein Enumobjekt ist (Quellobjektstyp: "
                    + source.getClass().toString() + ").");
        }
        return Enum.valueOf((Class<Enum>) destinationClass, ((Enum<?>) source).name());
    } else {
        return mapper.map(source, destinationClass);
    }
}

From source file:antre.TypeResolver.java

/**
 * Populates the {@code typeVariableMap} with type arguments and parameters for the given
 * {@code type}./*w w  w .  j a  va  2  s .  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.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java

public static Class<?> getGenericTypeArgument(Class<?> classToParse, Class<?> genericClassToFind,
        Integer argumentIndex) {//from w  ww. j a v  a2 s.c o  m
    Class<?> result = null;
    ParameterizedType parameterizedType = null;
    Class<?> currentClass = classToParse;
    Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>();
    Type[] previousTypeArguments = null;
    while (parameterizedType == null) {
        final TypeVariable<? extends Class<?>>[] typeParameters = currentClass.getTypeParameters();
        currentParameterValues = getCurrentParameterValues(typeParameters, previousTypeArguments,
                currentParameterValues);
        parameterizedType = findParametrizedType(currentClass, genericClassToFind, currentParameterValues);
        if (parameterizedType == null) {
            // current class doesn't extend/implement searched class directly. Should parse superclass
            final Type genericSuperclassType = currentClass.getGenericSuperclass();
            if (genericSuperclassType instanceof Class<?>) {
                log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName());
                return null;
            }
            final ParameterizedType genericSuperclass = (ParameterizedType) genericSuperclassType;
            previousTypeArguments = genericSuperclass.getActualTypeArguments();
            currentClass = (Class<?>) genericSuperclass.getRawType();
        }
    }
    final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
    if (actualTypeArguments[argumentIndex] instanceof Class<?>) {
        result = (Class<?>) actualTypeArguments[argumentIndex];
    } else if (actualTypeArguments[argumentIndex] instanceof TypeVariable) {
        result = currentParameterValues.get(((TypeVariable<?>) actualTypeArguments[argumentIndex]).getName());
    }
    if (result == null) {
        log.debug("Class " + classToParse.getName() + " has unsupported inheritance structure");
    }
    return result;
}

From source file:com.github.rvesse.airline.Accessor.java

private static Type[] getTypeParameters(Class<?> desiredType, Type type) {
    if (type instanceof Class) {
        Class<?> rawClass = (Class<?>) type;

        // if this is the collection class we're done
        if (desiredType.equals(type)) {
            return null;
        }//w ww.  j  av a 2s  .co  m

        for (Type iface : rawClass.getGenericInterfaces()) {
            Type[] collectionType = getTypeParameters(desiredType, iface);
            if (collectionType != null) {
                return collectionType;
            }
        }

        return getTypeParameters(desiredType, rawClass.getGenericSuperclass());
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;

        Type rawType = parameterizedType.getRawType();
        if (desiredType.equals(rawType)) {
            return parameterizedType.getActualTypeArguments();
        }

        Type[] collectionTypes = getTypeParameters(desiredType, rawType);
        if (collectionTypes != null) {
            for (int i = 0; i < collectionTypes.length; i++) {
                if (collectionTypes[i] instanceof TypeVariable) {
                    TypeVariable<?> typeVariable = (TypeVariable<?>) collectionTypes[i];
                    TypeVariable<?>[] rawTypeParams = ((Class<?>) rawType).getTypeParameters();
                    for (int j = 0; j < rawTypeParams.length; j++) {
                        if (typeVariable.getName().equals(rawTypeParams[j].getName())) {
                            collectionTypes[i] = parameterizedType.getActualTypeArguments()[j];
                        }
                    }
                }
            }
        }
        return collectionTypes;
    }
    return null;
}

From source file:org.apache.axis2.jaxws.utility.ClassUtils.java

/**
 * //from   w ww . j a  v a2  s  .c o m
 */
public static Set<Class> getClasses(Type type, Set<Class> list) {
    if (list == null) {
        list = new HashSet<Class>();
    }
    try {
        if (type instanceof Class) {
            list.add((Class) type);
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            getClasses(pt.getRawType(), list);
            Type types[] = pt.getActualTypeArguments();
            if (types != null) {
                for (int i = 0; i < types.length; i++) {
                    getClasses(types[i], list);
                }
            }
        }
        if (type instanceof GenericArrayType) {
            GenericArrayType gat = (GenericArrayType) type;
            getClasses(gat.getGenericComponentType(), list);
        }
    } catch (Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("Problem occurred in getClasses. Processing continues " + t);
        }
    }
    return list;
}

From source file:io.werval.runtime.util.TypeResolver.java

/**
 * Populates the {@code typeVariableMap} with type arguments and parameters for the given {@code type}.
 *//*from w  ww.ja  v a2 s  . co  m*/
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.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  w  w.  ja va 2s  . c om
            }
        }
    }
    throw new IllegalArgumentException(method.getName() + " must return " + expectedRawClass.getName()
            + " with 1 generic class parameter");
}

From source file:antre.TypeResolver.java

/**
 * Populates the {@code map} with with variable/argument pairs for the given {@code types}.
 *//*  w w w .  ja  v a 2s  .co m*/
static void buildTypeVariableMap(final Type[] types, final Map<TypeVariable<?>, Type> map) {
    for (Type type : types) {
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            buildTypeVariableMap(parameterizedType, map);
            Type rawType = parameterizedType.getRawType();
            if (rawType instanceof Class)
                buildTypeVariableMap(((Class<?>) rawType).getGenericInterfaces(), map);
        } else if (type instanceof Class) {
            buildTypeVariableMap(((Class<?>) type).getGenericInterfaces(), map);
        }
    }
}