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:es.logongas.ix3.util.ReflectionUtil.java

public static boolean isFieldParametrizedList(Field field, Class listClass) {
    Type type = field.getGenericType();

    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        if (parameterizedType.getRawType().equals(List.class)) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

            if ((actualTypeArguments == null) || (actualTypeArguments.length != 1)) {
                return false;
            } else {
                if (actualTypeArguments[0].equals(listClass)) {
                    return true;
                } else {
                    return false;
                }//  w w w  .  ja  va  2s.  co m
            }

        } else {
            return false;
        }

    } else {
        return false;
    }
}

From source file:es.logongas.ix3.util.ReflectionUtil.java

public static boolean isFieldParametrizedMap(Field field, Class keyClass, Class valueClass) {
    Type type = field.getGenericType();

    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        if (parameterizedType.getRawType().equals(Map.class)) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

            if ((actualTypeArguments == null) || (actualTypeArguments.length != 2)) {
                return false;
            } else {
                if ((actualTypeArguments[0].equals(keyClass)) && (actualTypeArguments[1].equals(valueClass))) {
                    return true;
                } else {
                    return false;
                }/*ww  w. j av  a  2  s .c  o m*/
            }

        } else {
            return false;
        }

    } else {
        return false;
    }
}

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

public static Class<?> erase(Type type) {
    if (type instanceof GenericArrayType) {
        return Object.class; //TODO - get more info?
    }/*w ww  . j  a v a2 s .  c o m*/
    if (type instanceof WildcardType) {
        throw new UnsupportedOperationException();
    }
    if (type instanceof TypeVariable) {
        throw new UnsupportedOperationException();
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        return (Class) parameterizedType.getRawType();
    }
    return (Class) type;
}

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

/**
 * Gets a ListTypeDefinition from the given type.
 * /*from   w  w w .j a v  a2  s  .  c o m*/
 * @param type
 * @param typeState
 * @param strict
 * @return
 */
public static ListTypeDefinition getListTypeDefinition(Type type, TypeState typeState, boolean strict) {

    ListReflectTypeDefinition lrtd = new ListReflectTypeDefinition();
    if (type instanceof Class) {
        Class<?> klass = (Class<?>) type;

        // we already know about this type; we're done
        if (typeState.isTypeKnown(ReflectTypeUtils.getTypeName(klass))) {
            return (ListTypeDefinition) typeState.getType(ReflectTypeUtils.getTypeName(klass));
        }

        lrtd.setKlass(klass);
        lrtd.setTypeName(ReflectTypeUtils.getTypeName(type));
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;

        if (!(pt.getRawType() instanceof Class)) {
            throw new WMRuntimeException(MessageResource.JSON_RAW_TYPE_NOT_CLASS, pt.getRawType());
        }

        Class<?> klass = (Class<?>) pt.getRawType();
        if (!Collection.class.isAssignableFrom(klass)) {
            throw new WMRuntimeException(MessageResource.JSON_EXPECTED_COLLECTION, klass);
        }

        // we already know about this type; we're done
        if (typeState.getType(ReflectTypeUtils.getTypeName(klass)) != null) {
            return (ListTypeDefinition) typeState.getType(ReflectTypeUtils.getTypeName(klass));
        }

        lrtd.setKlass(klass);
        lrtd.setTypeName(ReflectTypeUtils.getTypeName(type));
    } else {
        throw new WMRuntimeException(MessageResource.JSON_UNKNOWN_TYPE, type);
    }

    // in other methods, this has to happen earlier to deal with cycles.
    // those shouldn't be a problem here, but if you change the logic above,
    // it might get fucked.
    typeState.addType(lrtd);

    return lrtd;
}

From source file:com.frame.base.utils.ReflectionUtils.java

/**
 * Get the actual type arguments a child class has used to extend a generic
 * base class. (Taken from http://www.artima.com/weblogs/viewpost.jsp?thread=208860. Thanks
 * mathieu.grenonville for finding this solution!)
 * //from w ww.  j ava2s . co m
 * @param baseClass
 *            the base class
 * @param childClass
 *            the child class
 * @return a list of the raw classes for the actual type arguments.
 */
@SuppressWarnings("rawtypes")
public static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) {
    Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
    Type type = childClass;
    // start walking up the inheritance hierarchy until we hit baseClass
    while (!getClass(type).equals(baseClass)) {
        if (type instanceof Class) {
            // there is no useful information for us in raw types, so just
            // keep going.
            type = ((Class) type).getGenericSuperclass();
        } else {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Class<?> rawType = (Class) parameterizedType.getRawType();

            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
            for (int i = 0; i < actualTypeArguments.length; i++) {
                resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
            }

            if (!rawType.equals(baseClass)) {
                type = rawType.getGenericSuperclass();
            }
        }
    }

    // finally, for each actual type argument provided to baseClass,
    // determine (if possible)
    // the raw class for that type argument.
    Type[] actualTypeArguments;
    if (type instanceof Class) {
        actualTypeArguments = ((Class) type).getTypeParameters();
    } else {
        actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
    }
    List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>();
    // resolve types by chasing down type variables.
    for (Type baseType : actualTypeArguments) {
        while (resolvedTypes.containsKey(baseType)) {
            baseType = resolvedTypes.get(baseType);
        }
        typeArgumentsAsClasses.add(getClass(baseType));
    }
    return typeArgumentsAsClasses;
}

From source file:de.ifgi.fmt.utils.Utils.java

/**
 * //from   w  w  w .  j av  a2s. c  om
 * @param t
 * @param collClass
 * @param itemClass
 * @return
 */
public static boolean isParameterizedWith(Type t, Class<?> collClass, Class<?> itemClass) {
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        if (collClass.isAssignableFrom((Class<?>) pt.getRawType())) {
            Type argT = pt.getActualTypeArguments()[0];
            Class<?> tV = null;
            if (argT instanceof ParameterizedType) {
                tV = (Class<?>) ((ParameterizedType) argT).getRawType();
            } else if (argT instanceof Class) {
                tV = (Class<?>) argT;
            } else {
                return false;
            }
            return itemClass.isAssignableFrom(tV);
        }
    }
    return false;
}

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

public static boolean isMap(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        return Map.class.isAssignableFrom((Class<?>) parameterizedType.getRawType());
    }/*from   w w w . j a v  a2s.  c  o m*/
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        return Map.class.isAssignableFrom(clazz);
    }
    if (type instanceof GenericArrayType) {
        return false;
    }
    throw new UnsupportedOperationException();
}

From source file:com.holonplatform.core.internal.utils.TypeUtils.java

/**
 * Return the type parameter of a generic type.
 * @param clazz subClass of <code>baseClass</code> to analyze.
 * @param baseClass base class having the type parameter the value of which we need to retrieve
 * @return the parameterized type value/*from  w ww  . j a v  a  2  s . c  o  m*/
 */
@SuppressWarnings("rawtypes")
public static Type getTypeArgument(Class<?> clazz, Class<?> baseClass) {
    Stack<Type> superclasses = new Stack<>();
    Type currentType;
    Class<?> currentClass = clazz;

    if (clazz.getGenericSuperclass() == Object.class) {
        currentType = clazz;
        superclasses.push(currentType);
    } else {

        do {
            currentType = currentClass.getGenericSuperclass();
            superclasses.push(currentType);
            if (currentType instanceof Class) {
                currentClass = (Class) currentType;
            } else if (currentType instanceof ParameterizedType) {
                currentClass = (Class) ((ParameterizedType) currentType).getRawType();
            }
        } while (!currentClass.equals(baseClass));

    }

    // find which one supplies type argument and return it
    TypeVariable tv = baseClass.getTypeParameters()[0];
    while (!superclasses.isEmpty()) {
        currentType = superclasses.pop();

        if (currentType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) currentType;
            Class<?> rawType = (Class) pt.getRawType();
            int argIndex = Arrays.asList(rawType.getTypeParameters()).indexOf(tv);
            if (argIndex > -1) {
                Type typeArg = pt.getActualTypeArguments()[argIndex];
                if (typeArg instanceof TypeVariable) {
                    // type argument is another type variable - look for the value of that
                    // variable in subclasses
                    tv = (TypeVariable) typeArg;
                    continue;
                } else {
                    // found the value - return it
                    return typeArg;
                }
            }
        }

        // needed type argument not supplied - break and throw exception
        break;
    }
    throw new IllegalArgumentException(currentType + " does not specify a type parameter");
}

From source file:io.coala.factory.ClassUtil.java

/**
 * Get the actual type arguments a child class has used to extend a generic
 * base class. See <a//  w w  w . ja  v  a 2 s.c o  m
 * href="http://www.artima.com/weblogs/viewpost.jsp?thread=208860"
 * >description</a>
 * 
 * @param genericAncestorType the base class
 * @param concreteDescendantType the child class
 * @return a list of the raw classes for the actual type arguments.
 */
public static <T> List<Class<?>> getTypeArguments(final Class<T> genericAncestorType,
        final Class<? extends T> concreteDescendantType) {
    // sanity check
    if (genericAncestorType == null)
        throw CoalaExceptionFactory.VALUE_NOT_SET.createRuntime("genericAncestorType");
    if (concreteDescendantType == null)
        throw CoalaExceptionFactory.VALUE_NOT_SET.createRuntime("concreteDescendantType");

    Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
    Type type = concreteDescendantType;
    Class<?> typeClass = getClass(type);

    // start walking up the inheritance hierarchy until we hit parentClass
    while (!genericAncestorType.equals(typeClass)) {
        if (type instanceof Class) {
            // there is no useful information for us in raw types, so just
            // keep going.

            if (genericAncestorType.isInterface()) {
                Type intfType = null;
                for (Type intf : typeClass.getGenericInterfaces()) {
                    if (intf instanceof ParameterizedType
                            && genericAncestorType.equals(((ParameterizedType) intf).getRawType())) {
                        intfType = intf;
                        break;
                    }
                }
                if (intfType == null)
                    type = typeClass.getGenericSuperclass();
                else
                    type = intfType;
            } else
                type = typeClass.getGenericSuperclass();

            if (type == null) {
                if (!typeClass.isInterface()) {
                    LOG.warn("No generic super classes found for child class: " + typeClass + " of parent: "
                            + genericAncestorType);
                    return Collections.emptyList();
                }
                for (Type intf : typeClass.getGenericInterfaces()) {
                    if (intf instanceof ParameterizedType) {
                        type = intf;
                        // TODO try other interfaces if this one fails?
                        break;
                    }
                }
                if (type == null) {
                    LOG.warn("No generic ancestors found for child interface: " + typeClass + " of parent: "
                            + genericAncestorType);
                    return Collections.emptyList();
                }
            }
            // LOG.trace(String.format("Trying generic super of %s: %s",
            // typeClass.getSimpleName(), type));
        } else {
            final ParameterizedType parameterizedType = (ParameterizedType) type;
            final Class<?> rawType = (Class<?>) parameterizedType.getRawType();

            final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            final TypeVariable<?>[] typeParameters = rawType.getTypeParameters();
            for (int i = 0; i < actualTypeArguments.length; i++) {
                resolvedTypes.put(typeParameters[i], actualTypeArguments[i]);
            }

            if (!genericAncestorType.equals(rawType)) {
                type = rawType.getGenericSuperclass();
                // LOG.trace(String.format(
                // "Trying generic super of child %s: %s", rawType,
                // type));
            }
            // else // done climbing the hierarchy
            // LOG.trace("Matched generic " + type + " to ancestor: "
            // + genericAncestorType);
        }
        typeClass = getClass(type);
        // LOG.trace("Trying generic " + typeClass + " from: "
        // + Arrays.asList(typeClass.getGenericInterfaces()));
    }

    // finally, for each actual type argument provided to baseClass,
    // determine (if possible)
    // the raw class for that type argument.
    final Type[] actualTypeArguments;
    if (type instanceof Class) {
        actualTypeArguments = typeClass.getTypeParameters();
    } else {
        actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
    }

    // resolve types by chasing down type variables.
    final List<Class<?>> parentTypeArguments = new ArrayList<Class<?>>();
    for (Type baseType : actualTypeArguments) {
        while (resolvedTypes.containsKey(baseType))
            baseType = resolvedTypes.get(baseType);

        parentTypeArguments.add(getClass(baseType));
    }
    // LOG.trace(String.format(
    // "Got child %s's type arguments for %s: %s",
    // childClass.getName(), parentClass.getSimpleName(),
    // parentTypeArguments));
    return parentTypeArguments;
}

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

public static boolean isCollection(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        return Collection.class.isAssignableFrom((Class<?>) parameterizedType.getRawType());
    }//w w w .j  a  va 2s.  co  m
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        return Collection.class.isAssignableFrom(clazz);
    }
    if (type instanceof GenericArrayType) {
        return false;
    }
    throw new UnsupportedOperationException();
}