Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getActualTypeArguments.

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

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  ww w . j a  v a2 s  . com
    Class<?> collectionType = (Class<?>) pType.getRawType();
    if (!isCollectionTypeSupported(collectionType)) {
        return null;
    }
    if (isSetType(collectionType)) {
        return handleSetType(pType);
    } else {
        return handleListType(pType);
    }

}

From source file:com.mmj.app.lucene.solr.service.BaseSearch.java

@SuppressWarnings("unchecked")
public BaseSearch() {
    try {// w w w .j  ava2  s .com
        Type genericSuperclass = getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) genericSuperclass;
            Type[] actualTypeArguments = type.getActualTypeArguments();
            filedType = (Class<T>) actualTypeArguments[0];
        } else {
            throw new RuntimeException(
                    String.format("?%s??T", getClass().getSimpleName()));
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:de.openknowledge.domain.SimpleValueObjectBuilder.java

private SimpleValueObjectBuilder(Class<V> valueObjectClass) {
    ParameterizedType valueObjectSuperclass = (ParameterizedType) valueObjectClass.getGenericSuperclass();
    Class<?> simpleClass = (Class<?>) valueObjectSuperclass.getActualTypeArguments()[0];
    try {//ww  w. j  a  v a  2 s . co m
        valueObjectConstructor = valueObjectClass.getConstructor(simpleClass);
    } catch (NoSuchMethodException ex) {
        if (ClassUtils.isPrimitiveWrapper(simpleClass)) {
            try {
                valueObjectConstructor = valueObjectClass
                        .getConstructor(ClassUtils.wrapperToPrimitive(simpleClass));
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires "
                        + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter");
            }
        } else {
            throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires "
                    + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter");
        }
    }
    if (simpleClass.isPrimitive()) {
        simpleClass = ClassUtils.primitiveToWrapper(simpleClass);
    }
    try {
        simpleValueConstructor = simpleClass.getConstructor(String.class);
    } catch (NoSuchMethodException ex) {
        throw new IllegalStateException("Value Object simple type " + simpleClass.getName()
                + " requires String-Constructor to be used with JSF Converter");
    }
}

From source file:iing.uabc.edu.mx.persistencia.util.BeanManager.java

public Class getCollectionElementType(String propertyName) {
    Class elementType = null;//  ww  w.j a va 2s  . c  om

    try {
        Field collectionField = bean.getBean().getClass().getDeclaredField(propertyName);

        ParameterizedType collectionType = (ParameterizedType) collectionField.getGenericType();

        elementType = (Class) collectionType.getActualTypeArguments()[0];

    } catch (NoSuchFieldException | SecurityException ex) {
        Logger.getLogger(BeanManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return elementType;
}

From source file:org.openmrs.module.reportingrest.web.resource.BaseEvaluatedResourceTest.java

/**
 * Instantiates EvaluatedResource./*from   w  w w  .java 2  s  .  c o  m*/
 *
 * @return the new resource
 */
public R newResource() {
    ParameterizedType t = (ParameterizedType) getClass().getGenericSuperclass();
    @SuppressWarnings("unchecked")
    Class<T> clazz = (Class<T>) t.getActualTypeArguments()[1];
    return (R) Context.getService(RestService.class).getResourceBySupportedClass(clazz);
}

From source file:cn.nekocode.itempool.ItemPool.java

public void addType(@NonNull Class<? extends Item> itemClass) {
    ParameterizedType parameterizedType = (ParameterizedType) itemClass.getGenericSuperclass();
    Class dataClass = (Class) parameterizedType.getActualTypeArguments()[0];

    ItemType type = new ItemType(itemClass);
    mapOfType.put(dataClass, type);//  ww  w  .  j  a v  a 2  s.  c  o  m
    mapOfItemClass.put(type.TYPE_ID, itemClass);
}

From source file:gDao.genericDao.GDaoImpl.java

/**
 * If SimpleDao annotation is used, persistent class will be initialized through constructor by using
 * ParameterizedType class/*from   w  ww . ja va  2s.  c o  m*/
 */
public GDaoImpl() {
    try {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        persistentClass = (Class<T>) type.getActualTypeArguments()[0];
    } catch (Exception e) {
        logger.debug("Persistent class will be initialized manually by user");
    }
}

From source file:org.apache.camel.bam.processor.BamProcessorSupport.java

@SuppressWarnings("unchecked")
protected BamProcessorSupport(TransactionTemplate transactionTemplate, Expression correlationKeyExpression) {
    this.transactionTemplate = transactionTemplate;
    this.correlationKeyExpression = correlationKeyExpression;

    Type type = getClass().getGenericSuperclass();
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] arguments = parameterizedType.getActualTypeArguments();
        if (arguments.length > 0) {
            Type argumentType = arguments[0];
            if (argumentType instanceof Class) {
                this.entityType = (Class<T>) argumentType;
            }/*  w w w .  j a  va2  s.co  m*/
        }
    }
    if (entityType == null) {
        throw new IllegalArgumentException("Could not infer the entity type!");
    }
}

From source file:com.cloudera.livy.client.common.TestHttpMessages.java

private Class<?> getGenericArgType(Type type) {
    assertNotNull("FIX ME: null type argument.", type);

    ParameterizedType ptype = (ParameterizedType) type;
    assertEquals("FIX ME: no support for multiple type arguments.", 1, ptype.getActualTypeArguments().length);

    Type argType = ptype.getActualTypeArguments()[0];
    assertTrue("FIX ME: type argument is not a class.", argType instanceof Class);

    return (Class<?>) argType;
}

From source file:com.netflix.hystrix.contrib.javanica.utils.FallbackMethod.java

private Type getFirstParametrizedType(Method m) {
    Type gtype = m.getGenericReturnType();
    if (gtype instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) gtype;
        return pType.getActualTypeArguments()[0];
    }//from   ww w  .jav  a 2 s.  c o m
    return null;
}