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.romaframework.core.schema.SchemaHelper.java

/**
 * Return the generic type if iType uses Java5+ Generics.
 * // w w w  .j  a v  a2s. c  o m
 * @param iType
 *          Type with generics
 * @return Generic Class<?> if any
 */
public static Class<?> getGenericClass(Type iType) {
    // CHECK IF IT'S A PARAMETIZERED TYPE
    if (!(iType instanceof ParameterizedType)) {
        return null;
    }

    Class<?> returnClass = null;

    // list the raw type information
    ParameterizedType ptype = (ParameterizedType) iType;
    Type rtype = ptype.getRawType();

    if (!(rtype instanceof Class<?>)) {
        // NO CLASS: RETURN NULL
        return null;
    }

    Type[] targs = ptype.getActualTypeArguments();

    if (targs == null || targs.length == 0) {
        return null;
    }

    Class<?> classType = (Class<?>) rtype;

    try {
        if (classType.isArray()) {
            returnClass = (Class<?>) targs[0];
        } else if (java.util.Collection.class.isAssignableFrom((Class<?>) rtype)) {
            returnClass = resolveClassFromType(targs[0], null);
            if (returnClass == null)
                returnClass = Object.class;

        } else if (java.util.Map.class.isAssignableFrom((Class<?>) rtype)) {
            returnClass = classType.getDeclaredClasses()[0];
        } else if (targs[0] instanceof Class<?>) {
            return (Class<?>) targs[0];
        }
    } catch (ClassCastException e) {
        throw new ConfigurationException(
                "Cannot determine embedded type for " + iType + ". Embedded class not found", e);
    }

    return returnClass;
}

From source file:ductive.console.commands.register.spring.ArgumentParserBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
    ReflectionUtils.doWithMethods(bean.getClass(), new MethodCallback() {
        @Override/*from   w w  w  . ja  v  a2s  .c om*/
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            ArgParser argParser = AnnotationUtils.findAnnotation(method, ArgParser.class);
            if (argParser == null)
                return;

            String qualifier = StringUtils.defaultIfEmpty(argParser.value(), null);

            if (!void.class.equals(argParser.bind())) {
                argParserRegistry.register(argParser.bind(), qualifier, bean, method);
                return;
            }

            Type genericReturnType = method.getGenericReturnType();
            Validate.isTrue(ParameterizedType.class.isInstance(genericReturnType),
                    String.format(
                            "method %s: return type of parser generator method must be a subclass of %s<?>",
                            method, Parser.class.getCanonicalName()));

            ParameterizedType pt = ParameterizedType.class.cast(genericReturnType);
            Validate.isTrue(Parser.class.isAssignableFrom((Class<?>) pt.getRawType()),
                    String.format(
                            "method %s: return type of parser generator method must be a subclass of %s<?>",
                            method, Parser.class.getCanonicalName()));

            Type[] genericParams = pt.getActualTypeArguments();
            Validate.isTrue(genericParams.length == 1);

            argParserRegistry.register((Class<?>) genericParams[0], qualifier, bean, method);
        }

    });
    return bean;
}

From source file:org.apache.servicecomb.swagger.invocation.converter.ConverterMgr.java

protected Converter findCollectionToArray(Type src, Type target) {
    if (ParameterizedType.class.isAssignableFrom(src.getClass()) && target.getClass().equals(Class.class)) {
        ParameterizedType srcType = (ParameterizedType) src;
        Class<?> srcCls = (Class<?>) srcType.getRawType();
        Class<?> targetCls = (Class<?>) target;

        if (Collection.class.isAssignableFrom(srcCls) && targetCls.isArray()
                && srcType.getActualTypeArguments()[0].equals(targetCls.getComponentType())) {
            Converter converter = collectionToArrayMap.computeIfAbsent(target,
                    k -> new SameElementCollectionToArray(targetCls.getComponentType()));
            return converter;
        }//from  ww  w  .  ja va 2s. co m
    }

    return null;
}

From source file:io.servicecomb.swagger.invocation.converter.ConverterMgr.java

protected Converter findCollectionToArray(Type src, Type target) {
    if (ParameterizedType.class.isAssignableFrom(src.getClass()) && target.getClass().equals(Class.class)) {
        ParameterizedType srcType = (ParameterizedType) src;
        Class<?> srcCls = (Class<?>) srcType.getRawType();
        Class<?> targetCls = (Class<?>) target;

        if (Collection.class.isAssignableFrom(srcCls) && targetCls.isArray()
                && srcType.getActualTypeArguments()[0].equals(targetCls.getComponentType())) {
            Converter converter = collectionToArrayMap.get(target);
            if (converter == null) {
                converter = new SameElementCollectionToArray(targetCls.getComponentType());
                collectionToArrayMap.put(target, converter);
            }//  w w  w .j  av a  2 s.  c om
            return converter;
        }
    }

    return null;
}

From source file:io.servicecomb.swagger.invocation.converter.ConverterMgr.java

protected Converter findArrayToCollection(Type src, Type target) {
    if (src.getClass().equals(Class.class) && ParameterizedType.class.isAssignableFrom(target.getClass())) {
        Class<?> srcCls = (Class<?>) src;
        ParameterizedType targetType = (ParameterizedType) target;
        Class<?> targetCls = (Class<?>) targetType.getRawType();

        if (srcCls.isArray() && srcCls.getComponentType().equals(targetType.getActualTypeArguments()[0])) {
            if (List.class.isAssignableFrom(targetCls)) {
                return arrayToList;
            }//from   w ww  .  j a v  a2 s.com
            if (Set.class.isAssignableFrom(targetCls)) {
                return arrayToSet;
            }
        }
    }

    return null;
}

From source file:com.nesscomputing.jersey.exceptions.GuiceProvisionExceptionMapper.java

private Class<?> getType(Class<?> c) {
    Class<?> _c = c;//from   w w w  .  jav  a 2s  . c om
    while (_c != Object.class) {
        Type[] ts = _c.getGenericInterfaces();
        for (Type t : ts) {
            if (t instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) t;
                if (pt.getRawType() == ExceptionMapper.class) {
                    return getResolvedType(pt.getActualTypeArguments()[0], c, _c);
                }
            }
        }

        _c = _c.getSuperclass();
    }

    // This statement will never be reached
    return null;
}

From source file:reconf.client.constructors.CollectionConstructor.java

public Object construct(MethodData data) throws Throwable {

    if (data.hasAdapter()) {
        return data.getAdapter().adapt(data.getValue());
    }//from  www. j a  v  a  2s . c  o  m

    Class<?> returnClass = null;
    Type innerClass = null;

    if (data.getReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterized = (ParameterizedType) data.getReturnType();
        returnClass = (Class<?>) parameterized.getRawType();

        if (parameterized.getActualTypeArguments()[0] instanceof ParameterizedType) {
            innerClass = parameterized.getActualTypeArguments()[0];

        } else if (parameterized.getActualTypeArguments()[0] instanceof Class<?>) {
            innerClass = parameterized.getActualTypeArguments()[0];
        }
    } else if (data.getReturnType() instanceof Class) {
        returnClass = (Class<?>) data.getReturnType();

        if (returnClass.getGenericSuperclass() != null
                && returnClass.getGenericSuperclass() instanceof ParameterizedType) {
            ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass();
            if (parameterized.getActualTypeArguments().length != 1) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            } else {
                innerClass = parameterized.getActualTypeArguments()[0];
            }

        } else {
            innerClass = Object.class;
        }

    } else {
        throw new IllegalArgumentException(msg.format("error.return", data.getMethod()));
    }

    if (returnClass.isInterface()) {
        returnClass = getDefaultImplementation(data, returnClass);
    }

    Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
    Collection<Object> collectionInstance = (Collection<Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue()) {
        return collectionInstance;
    }

    for (String s : new StringParser(data).getTokens()) {
        Object o = ObjectConstructorFactory.get(innerClass)
                .construct(new MethodData(data.getMethod(), innerClass, s));
        if (o != null) {
            collectionInstance.add(o);
        }
    }

    return collectionInstance;
}

From source file:com.nesscomputing.jersey.exceptions.GuiceProvisionExceptionMapper.java

private Class<?> getResolvedType(Type t, Class<?> c, Class<?> dc) {
    if (t instanceof Class) {
        return (Class<?>) t;
    } else if (t instanceof TypeVariable) {
        ReflectionHelper.ClassTypePair ct = ReflectionHelper.resolveTypeVariable(c, dc, (TypeVariable<?>) t);
        if (ct != null) {
            return ct.c;
        } else {/*from w w w  .ja  va  2s .  co  m*/
            return null;
        }
    } else if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        return (Class<?>) pt.getRawType();
    } else {
        // TODO log
        return null;
    }
}

From source file:org.blocks4j.reconf.client.constructors.CollectionConstructor.java

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass;/*from   w w w .j  a v  a 2 s .  c om*/
    Type innerClass = null;

    if (data.getReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterized = (ParameterizedType) data.getReturnType();
        returnClass = (Class<?>) parameterized.getRawType();

        if (parameterized.getActualTypeArguments()[0] instanceof ParameterizedType) {
            innerClass = parameterized.getActualTypeArguments()[0];

        } else if (parameterized.getActualTypeArguments()[0] instanceof Class<?>) {
            innerClass = parameterized.getActualTypeArguments()[0];
        }
    } else if (data.getReturnType() instanceof Class) {
        returnClass = (Class<?>) data.getReturnType();

        if (returnClass.getGenericSuperclass() != null
                && returnClass.getGenericSuperclass() instanceof ParameterizedType) {
            ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass();
            if (parameterized.getActualTypeArguments().length != 1) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            } else {
                innerClass = parameterized.getActualTypeArguments()[0];
            }

        } else {
            innerClass = Object.class;
        }

    } else {
        throw new IllegalArgumentException(msg.format("error.return", data.getMethod()));
    }

    if (returnClass.isInterface()) {
        returnClass = getDefaultImplementation(data, returnClass);
    }

    Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
    Collection<Object> collectionInstance = (Collection<Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue()) {
        return collectionInstance;
    }

    for (String s : new StringParser(data).getTokens()) {
        Object o = ObjectConstructorFactory.get(innerClass)
                .construct(new MethodData(data.getMethod(), innerClass, s));
        if (o != null) {
            collectionInstance.add(o);
        }
    }

    return collectionInstance;
}

From source file:net.nelz.simplesm.aop.CacheBase.java

protected boolean verifyTypeIsList(final Class clazz) {
    if (List.class.equals(clazz)) {
        return true;
    }/*from w w  w . j  a v  a2 s .co  m*/
    final Type[] types = clazz.getGenericInterfaces();
    if (types != null) {
        for (final Type type : types) {
            if (type != null) {
                if (type instanceof ParameterizedType) {
                    final ParameterizedType ptype = (ParameterizedType) type;
                    if (List.class.equals(ptype.getRawType())) {
                        return true;
                    }
                } else {
                    if (List.class.equals(type)) {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}