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:com.puppetlabs.puppetdb.javaclient.test.MockConnector.java

private static boolean isCollectionType(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType ptype = (ParameterizedType) type;
        Type rawType = ptype.getRawType();
        return rawType instanceof Class && Collection.class.isAssignableFrom((Class<?>) rawType);
    }//www.  j  a v  a2 s  .  c  o  m
    return false;
}

From source file:Main.java

/**
 * Returns the generic class of multi-value objects.
 * //  www  .  j  ava  2 s  .c o m
 * @param p
 *          Field to examine
 * @return The Class<?> of generic type if any, otherwise null
 */
public static Class<?> getGenericMultivalueType(final Field p) {
    if (p.getType() instanceof Class<?>) {
        final Type genericType = p.getGenericType();
        if (genericType != null && genericType instanceof ParameterizedType) {
            final ParameterizedType pt = (ParameterizedType) genericType;
            if (pt.getActualTypeArguments() != null && pt.getActualTypeArguments().length > 0) {
                if (((Class<?>) pt.getRawType()).isAssignableFrom(Map.class)) {
                    if (pt.getActualTypeArguments()[1] instanceof Class<?>) {
                        return (Class<?>) pt.getActualTypeArguments()[1];
                    } else if (pt.getActualTypeArguments()[1] instanceof ParameterizedType)
                        return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[1]).getRawType();
                } else if (pt.getActualTypeArguments()[0] instanceof Class<?>) {
                    return (Class<?>) pt.getActualTypeArguments()[0];
                } else if (pt.getActualTypeArguments()[0] instanceof ParameterizedType)
                    return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[0]).getRawType();
            }
        } else if (p.getType().isArray())
            return p.getType().getComponentType();
    }
    return null;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static Class<?> getGenericCollectionTypeOfField(Field next) {
    Class<?> type;/*from   w  w w.j  ava  2 s . co m*/
    ParameterizedType collectionType = (ParameterizedType) next.getGenericType();
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java

private static void _addOptionalType(Builder builder, Method method) {
    java.lang.reflect.Type type = _getGenericType(method);

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

        if (innerParametrizedType.getRawType() == List.class) {
            _addListType(builder, method, (Class<?>) innerParametrizedType.getActualTypeArguments()[0]);
        }/*from ww w .  j a v  a 2  s .  c  o m*/
    } else {
        Class<?> returnTypeClass = (Class<?>) type;

        _addFields(builder, method, returnTypeClass);
    }
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static Class<?> getGenericCollectionTypeOfMethodParameter(Method theMethod, int theParamIndex) {
    Class<?> type;/*from  w ww  . j  a va 2s  . c  o  m*/
    Type genericParameterType = theMethod.getGenericParameterTypes()[theParamIndex];
    if (Class.class.equals(genericParameterType)) {
        return null;
    }
    ParameterizedType collectionType = (ParameterizedType) genericParameterType;
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:com.jeroensteenbeeke.hyperion.events.DefaultEventDispatcher.java

@SuppressWarnings("unchecked")
static Class<? extends Event<?>> getEventClass(Class<?> handlerClass) {

    for (Class<?> i : handlerClass.getInterfaces()) {
        if (EventHandler.class.equals(i)) {
            for (Type t : handlerClass.getGenericInterfaces()) {
                if (t instanceof ParameterizedType) {
                    ParameterizedType pt = (ParameterizedType) t;
                    if (EventHandler.class.equals(pt.getRawType())) {

                        return (Class<? extends Event<?>>) pt.getActualTypeArguments()[0];

                    }/*from w ww.  j a v a2 s . c om*/
                }
            }
        } else if (EventHandler.class.isAssignableFrom(i)) {
            return getEventClass((Class<? extends EventHandler<?>>) i);
        }
    }

    if (EventHandler.class.isAssignableFrom(handlerClass.getSuperclass())) {
        return getEventClass((Class<?>) handlerClass.getSuperclass());
    }

    return null;

}

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static String getSimpleName(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType genericType = (ParameterizedType) type;
        return getSimpleName(genericType.getRawType()) + getParameterizedTypeName(genericType);
    }//from  w w  w  . j  a v  a 2  s .c  o m
    if (type instanceof Class) {
        return _getSimpleName((Class) type);
    }
    return type.getTypeName();
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

@SuppressWarnings({ "rawtypes" })
public static Class<?> getGenericCollectionTypeOfMethodReturnType(Method theMethod) {
    Class<?> type;/*from www .j  a v  a 2  s. co  m*/
    Type genericReturnType = theMethod.getGenericReturnType();
    if (!(genericReturnType instanceof ParameterizedType)) {
        return null;
    }
    ParameterizedType collectionType = (ParameterizedType) genericReturnType;
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else if (firstArg instanceof TypeVariable<?>) {
        Type decl = ((TypeVariable) firstArg).getBounds()[0];
        return (Class<?>) decl;
    } else if (firstArg instanceof WildcardType) {
        Type decl = ((WildcardType) firstArg).getUpperBounds()[0];
        return (Class<?>) decl;
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java

/**
 * Obtain the concrete type used by an implementation of an interface that
 * uses a generic type.//w  w  w  .java2s. c  o m
 *
 * @param type  The interface that defines a generic type
 * @param clazz The class that implements the interface with a concrete type
 * @param <T>   The interface type
 *
 * @return concrete type used by the implementation
 */
private static <T> Object getGenericType(Class<T> type, Class<? extends T> clazz) {

    // Look to see if this class implements the generic interface

    // Get all the interfaces
    Type[] interfaces = clazz.getGenericInterfaces();
    for (Type iface : interfaces) {
        // Only need to check interfaces that use generics
        if (iface instanceof ParameterizedType) {
            ParameterizedType pi = (ParameterizedType) iface;
            // Look for the generic interface
            if (pi.getRawType() instanceof Class) {
                if (type.isAssignableFrom((Class<?>) pi.getRawType())) {
                    return getTypeParameter(clazz, pi.getActualTypeArguments()[0]);
                }
            }
        }
    }

    // Interface not found on this class. Look at the superclass.
    @SuppressWarnings("unchecked")
    Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass();

    Object result = getGenericType(type, superClazz);
    if (result instanceof Class<?>) {
        // Superclass implements interface and defines explicit type for
        // generic
        return result;
    } else if (result instanceof Integer) {
        // Superclass implements interface and defines unknown type for
        // generic
        // Map that unknown type to the generic types defined in this class
        ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass();
        return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]);
    } else {
        // Error will be logged further up the call stack
        return null;
    }
}

From source file:GenericsUtil.java

private static void gatherTypeVariables(final Type type, final Map<TypeVariable<?>, Type> map) {
    if (ParameterizedType.class.isInstance(type)) {
        final ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
        final TypeVariable<?>[] typeVariables = GenericDeclaration.class.cast(parameterizedType.getRawType())
                .getTypeParameters();//ww  w  .j a  v a2 s.  co m
        final Type[] actualTypes = parameterizedType.getActualTypeArguments();
        for (int i = 0; i < actualTypes.length; ++i) {
            map.put(typeVariables[i], actualTypes[i]);
        }
    }
}