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:reconf.client.constructors.MapConstructor.java

public Object construct(MethodData data) throws Throwable {

    if (data.hasAdapter()) {
        return data.getAdapter().adapt(data.getValue());
    }/*from  ww w  .j  a v  a  2  s .co  m*/

    Class<?> returnClass = null;
    Type keyType = null;
    Type valueType = null;

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

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

                } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) {
                    valueType = first;
                    keyType = parameterized.getActualTypeArguments()[0];

                } else {
                    throw new IllegalArgumentException(
                            msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
                }
            }

        } else {
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];
        }

    } 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 != 2) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];

        } else {
            keyType = Object.class;
            valueType = 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);
    Map<Object, Object> mapInstance = (Map<Object, Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) {
        return mapInstance;
    }

    if ((!(keyType instanceof Class))
            || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) {
        throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod()));
    }

    StringParser parser = new StringParser(data);
    for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) {
        Object value = ObjectConstructorFactory.get(valueType)
                .construct(new MethodData(data.getMethod(), valueType, each.getValue()));
        mapInstance.put(ObjectConstructorFactory.get(keyType)
                .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value);
    }

    return mapInstance;
}

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

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass;//from   ww  w .  ja  va  2  s.  c o m
    Type keyType = null;
    Type valueType = null;

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

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

                } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) {
                    valueType = first;
                    keyType = parameterized.getActualTypeArguments()[0];

                } else {
                    throw new IllegalArgumentException(
                            msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
                }
            }

        } else {
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];
        }

    } 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 != 2) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];

        } else {
            keyType = Object.class;
            valueType = 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);
    Map<Object, Object> mapInstance = (Map<Object, Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) {
        return mapInstance;
    }

    if ((!(keyType instanceof Class))
            || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) {
        throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod()));
    }

    StringParser parser = new StringParser(data);
    for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) {
        Object value = ObjectConstructorFactory.get(valueType)
                .construct(new MethodData(data.getMethod(), valueType, each.getValue()));
        mapInstance.put(ObjectConstructorFactory.get(keyType)
                .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value);
    }

    return mapInstance;
}

From source file:org.apache.axis2.jaxws.marshaller.impl.alt.DocLitWrappedMinimalMethodMarshaller.java

/**
 * Return ComponentType, might need to look at the GenericType
 * @param pd ParameterDesc or null if return
 * @param operationDesc OperationDescription
 * @param msrd MarshalServiceRuntimeDescription
 * @return//w  w w .ja v a 2s  .co  m
 */
private static Class getComponentType(ParameterDescription pd, OperationDescription operationDesc,
        MarshalServiceRuntimeDescription msrd) {
    Class componentType = null;
    if (log.isDebugEnabled()) {
        log.debug("start getComponentType");
        log.debug(" ParameterDescription=" + pd);
    }

    // Determine if array, list, or other
    Class cls = null;
    if (pd == null) {
        cls = operationDesc.getResultActualType();
    } else {
        cls = pd.getParameterActualType();
    }

    if (cls != null) {
        if (cls.isArray()) {
            componentType = cls.getComponentType();
        } else if (List.class.isAssignableFrom(cls)) {
            if (log.isDebugEnabled()) {
                log.debug("Parameter is a List: " + cls);
            }
            Method method = msrd.getMethod(operationDesc);
            if (log.isDebugEnabled()) {
                log.debug("Method is: " + method);
            }
            Type genericType = null;
            if (pd == null) {
                genericType = method.getGenericReturnType();
            } else {
                ParameterDescription[] pds = operationDesc.getParameterDescriptions();
                for (int i = 0; i < pds.length; i++) {
                    if (pds[i] == pd) {
                        genericType = method.getGenericParameterTypes()[i];
                    }
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("genericType is: " + genericType.getClass() + " " + genericType);
            }
            if (genericType instanceof Class) {
                if (log.isDebugEnabled()) {
                    log.debug(" genericType instanceof Class");
                }
                componentType = String.class;
            } else if (genericType instanceof ParameterizedType) {
                if (log.isDebugEnabled()) {
                    log.debug(" genericType instanceof ParameterizedType");
                }
                ParameterizedType pt = (ParameterizedType) genericType;
                if (pt.getRawType() == Holder.class) {
                    if (log.isDebugEnabled()) {
                        log.debug(" strip off holder");
                    }
                    genericType = pt.getActualTypeArguments()[0];
                    if (genericType instanceof Class) {
                        componentType = String.class;
                    } else if (genericType instanceof ParameterizedType) {
                        pt = (ParameterizedType) genericType;
                    }
                }
                if (componentType == null) {
                    Type comp = pt.getActualTypeArguments()[0];
                    if (log.isDebugEnabled()) {
                        log.debug(" comp =" + comp.getClass() + " " + comp);
                    }
                    if (comp instanceof Class) {
                        componentType = (Class) comp;
                    } else if (comp instanceof ParameterizedType) {
                        componentType = (Class) ((ParameterizedType) comp).getRawType();
                    }
                }
            }

        }
    }

    if (log.isDebugEnabled()) {
        log.debug("end getComponentType=" + componentType);
    }
    return componentType;
}

From source file:org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.java

private boolean eligibleParameter(MethodParameter methodParameter) {
    Type parameterType = methodParameter.getGenericParameterType();
    if (parameterType.equals(Acknowledgment.class) || parameterType.equals(ConsumerRecord.class)) {
        return false;
    }//from   ww w  .ja  va  2  s.c o  m
    if (parameterType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) parameterType;
        if (parameterizedType.getRawType().equals(Message.class)) {
            return !(parameterizedType.getActualTypeArguments()[0] instanceof WildcardType);
        }
    }
    return !parameterType.equals(Message.class); // could be Message without a generic type
}

From source file:com.medallia.tiny.ObjectProvider.java

/**
 * Register a factory object; if this ObjectProvider is asked to
 * provide the type of object the factory produces it will
 * call {@link ObjectFactory#make()} to obtain the object.
 *///  www . java 2 s.  co  m
public <X> ObjectProvider registerFactory(ObjectFactory<X> of) {
    for (Type t : of.getClass().getGenericInterfaces()) {
        if (t instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) t;
            if (pt.getRawType().equals(ObjectFactory.class)) {
                @SuppressWarnings("unchecked")
                Class<X> x = (Class<X>) pt.getActualTypeArguments()[0];
                registerFactory(x, of);
                break;
            }
        }
    }
    return this;
}

From source file:dstrelec.nats.listener.adapter.MessagingMessageListenerAdapter.java

private boolean eligibleParameter(MethodParameter methodParameter) {
    Type parameterType = methodParameter.getGenericParameterType();
    if (parameterType.equals(io.nats.client.Message.class)) {
        return false;
    }/*from   w  w w.jav a2s  .  c  o m*/
    if (parameterType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) parameterType;
        if (parameterizedType.getRawType().equals(Message.class)) {
            return !(parameterizedType.getActualTypeArguments()[0] instanceof WildcardType);
        }
    }
    return !parameterType.equals(Message.class); // could be Message without a generic type
}

From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java

private Type resolveTypeParameter(ParameterizedType type, Type beanType, TypeVariable<?> typeVariable) {
    // step1. raw type
    Class<?> actualType = (Class<?>) type.getRawType();
    TypeVariable<?>[] typeVariables = actualType.getTypeParameters();
    Type[] actualTypes = type.getActualTypeArguments();
    for (int i = 0; i < typeVariables.length; i++) {
        if (typeVariables[i].equals(typeVariable) && !actualTypes[i].equals(typeVariable)) {
            return resolveType(this.type, beanType, actualTypes[i]);
        }/*from w w w  .j a v  a2 s  .  co  m*/
    }

    // step2. generic super class
    Type genericSuperType = actualType.getGenericSuperclass();
    Type resolvedGenericSuperType = resolveType(genericSuperType, beanType, typeVariable);
    if (!(resolvedGenericSuperType instanceof TypeVariable<?>)) {
        return resolvedGenericSuperType;
    }

    // step3. generic interfaces
    if (beanType instanceof ParameterizedType) {
        for (Type interfaceType : ((Class<?>) ((ParameterizedType) beanType).getRawType())
                .getGenericInterfaces()) {
            Type resolvedType = resolveType(interfaceType, interfaceType, typeVariable);
            if (!(resolvedType instanceof TypeVariable<?>)) {
                return resolvedType;
            }
        }
    }

    // don't resolve type variable
    return typeVariable;
}

From source file:org.netxilia.spi.impl.formula.function.MethodWrapper.java

public MethodWrapper(Object instance, Method method) {
    this.instance = instance;
    this.method = method;
    Class<?>[] paramTypes = method.getParameterTypes();
    paramConverters = new IParamConverter[paramTypes.length];

    Type[] genericParameterTypes = method.getGenericParameterTypes();

    for (int i = 0; i < genericParameterTypes.length; ++i) {
        Type genericParameterType = genericParameterTypes[i];
        if (genericParameterType instanceof ParameterizedType) {
            ParameterizedType aType = (ParameterizedType) genericParameterType;

            if (Iterator.class.isAssignableFrom((Class<?>) aType.getRawType())) {
                Class<?> componentType = (Class<?>) aType.getActualTypeArguments()[0];
                paramConverters[i] = new IteratorConverter(componentType);
            }/*from  w ww . j  a va 2  s. co  m*/
        } else {
            Class<?> paramType = (Class<?>) genericParameterType;
            if (IGenericValue.class == paramType) {
                paramConverters[i] = new LazyParamConverter();
            } else if (RichValue.class == paramType) {
                paramConverters[i] = new RichValueParamConverter();
            } else {
                paramConverters[i] = new BasicTypeConverter(paramType);
            }
        }
    }

    Function annotation = this.method.getAnnotation(Function.class);
    cacheable = annotation == null || annotation.cacheable();
}

From source file:com.adobe.acs.commons.util.impl.ValueMapTypeConverter.java

private Object handleCollectionTypes(ParameterizedType pType) {
    // list support
    if (pType.getActualTypeArguments().length != 1) {
        return null;
    }//from w w w . ja  v  a2s  .  co  m
    Class<?> collectionType = (Class<?>) pType.getRawType();
    if (!isCollectionTypeSupported(collectionType)) {
        return null;
    }
    if (isSetType(collectionType)) {
        return handleSetType(pType);
    } else {
        return handleListType(pType);
    }

}

From source file:org.apache.hadoop.yarn.api.TestPBImplRecords.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static Object genTypeValue(Type type) {
    Object ret = typeValueCache.get(type);
    if (ret != null) {
        return ret;
    }//from w w  w  . ja  va  2 s  .  c  om
    // only use positive primitive values
    if (type.equals(boolean.class)) {
        return rand.nextBoolean();
    } else if (type.equals(byte.class)) {
        return bytes[rand.nextInt(4)];
    } else if (type.equals(int.class)) {
        return rand.nextInt(1000000);
    } else if (type.equals(long.class)) {
        return Long.valueOf(rand.nextInt(1000000));
    } else if (type.equals(float.class)) {
        return rand.nextFloat();
    } else if (type.equals(double.class)) {
        return rand.nextDouble();
    } else if (type.equals(String.class)) {
        return String.format("%c%c%c", 'a' + rand.nextInt(26), 'a' + rand.nextInt(26), 'a' + rand.nextInt(26));
    } else if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            Class compClass = clazz.getComponentType();
            if (compClass != null) {
                ret = Array.newInstance(compClass, 2);
                Array.set(ret, 0, genTypeValue(compClass));
                Array.set(ret, 1, genTypeValue(compClass));
            }
        } else if (clazz.isEnum()) {
            Object[] values = clazz.getEnumConstants();
            ret = values[rand.nextInt(values.length)];
        } else if (clazz.equals(ByteBuffer.class)) {
            // return new ByteBuffer every time
            // to prevent potential side effects
            ByteBuffer buff = ByteBuffer.allocate(4);
            rand.nextBytes(buff.array());
            return buff;
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type rawType = pt.getRawType();
        Type[] params = pt.getActualTypeArguments();
        // only support EnumSet<T>, List<T>, Set<T>, Map<K,V>
        if (rawType.equals(EnumSet.class)) {
            if (params[0] instanceof Class) {
                Class c = (Class) (params[0]);
                return EnumSet.allOf(c);
            }
        }
        if (rawType.equals(List.class)) {
            ret = Lists.newArrayList(genTypeValue(params[0]));
        } else if (rawType.equals(Set.class)) {
            ret = Sets.newHashSet(genTypeValue(params[0]));
        } else if (rawType.equals(Map.class)) {
            Map<Object, Object> map = Maps.newHashMap();
            map.put(genTypeValue(params[0]), genTypeValue(params[1]));
            ret = map;
        }
    }
    if (ret == null) {
        throw new IllegalArgumentException("type " + type + " is not supported");
    }
    typeValueCache.put(type, ret);
    return ret;
}