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:org.jbpm.formModeler.core.model.PojoDataHolder.java

private Set<DataFieldHolder> calculatePropertyNames() throws Exception {

    Class clazz = getHolderClass();

    if (clazz == null) {
        return null;
    }/*  w  w  w  .j a  va 2 s .c  o m*/

    Set<DataFieldHolder> dataFieldHolders = new TreeSet<DataFieldHolder>();

    for (Field field : clazz.getDeclaredFields()) {

        if (isValidType(field.getType().getName())) {
            String capitalizedName = capitalize(field.getName());
            try {
                Method setter = clazz.getDeclaredMethod("set" + capitalizedName, field.getType());

                if (!setter.getReturnType().getName().equals("void")
                        && !Modifier.isPublic(setter.getModifiers()))
                    continue;

                Method getter;

                if (field.getType().equals(boolean.class))
                    getter = clazz.getDeclaredMethod("is" + capitalizedName);
                else
                    getter = clazz.getDeclaredMethod("get" + capitalizedName);

                if (!getter.getReturnType().equals(field.getType())
                        && !Modifier.isPublic(getter.getModifiers()))
                    continue;

                Type type = field.getGenericType();

                DataFieldHolder fieldHolder;

                if (type instanceof ParameterizedType) {
                    ParameterizedType generictype = (ParameterizedType) type;
                    Type[] arguments = generictype.getActualTypeArguments();
                    if (arguments == null || arguments.length > 1)
                        fieldHolder = new DataFieldHolder(this, field.getName(), field.getType().getName());
                    else
                        fieldHolder = new DataFieldHolder(this, field.getName(), field.getType().getName(),
                                ((Class<?>) arguments[0]).getName());
                } else {
                    fieldHolder = new DataFieldHolder(this, field.getName(), field.getType().getName());
                }

                dataFieldHolders.add(fieldHolder);

            } catch (Exception e) {
                getLogger().debug("Unable to generate field holder for '{}': {}", field.getName(), e);
            }
        }
    }

    return dataFieldHolders;
}

From source file:org.raml.emitter.RamlEmitter.java

private void dumpSequenceField(StringBuilder dump, int depth, Field field, Object pojo) {
    if (!List.class.isAssignableFrom(field.getType())) {
        throw new RuntimeException("Only List can be sequence.");
    }/*from  w w  w. j ava  2  s .  c  o m*/

    List seq = (List) getFieldValue(field, pojo);
    if (seq == null || seq.size() == 0) {
        return;
    }

    Type type = field.getGenericType();
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        Type itemType = pType.getActualTypeArguments()[0];
        dump.append(indent(depth)).append(alias(field)).append(YAML_MAP_SEP);
        dumpSequenceItems(dump, depth, seq, itemType);
    }
}

From source file:org.jnosql.artemis.reflection.Reflections.java

/**
 * return the key and value of field./*from   w ww  .  j  av a  2s.  c  o m*/
 *
 * @param field the field
 * @return the types of the type
 */
public KeyValueClass getGenericKeyValue(Field field) {
    ParameterizedType genericType = (ParameterizedType) field.getGenericType();
    KeyValueClass keyValueClass = new KeyValueClass();
    keyValueClass.keyClass = (Class<?>) genericType.getActualTypeArguments()[0];
    keyValueClass.valueClass = (Class<?>) genericType.getActualTypeArguments()[1];
    return keyValueClass;
}

From source file:com.grepcurl.random.ObjectGenerator.java

protected Type _getParameterType(ParameterizedType type) {
    return type.getActualTypeArguments()[0];
}

From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.ResultSetConverterBlockServiceImpl.java

/**
 * Gets List&lt;Entity&gt; class
 * @param daoMethod method/*from  w ww .  j  a va 2s .  c  o m*/
 * @return entity class
 */
private Class getEntityClass(Method daoMethod) {
    Type type = daoMethod.getGenericReturnType();
    ParameterizedType parameterizedType = (ParameterizedType) type;
    return (Class) parameterizedType.getActualTypeArguments()[0];
}

From source file:com.frank.search.solr.repository.support.SimpleSolrRepository.java

@SuppressWarnings("unchecked")
private Class<T> resolveReturnedClassFromGernericType() {
    ParameterizedType parameterizedType = resolveReturnedClassFromGernericType(getClass());
    return (Class<T>) parameterizedType.getActualTypeArguments()[0];
}

From source file:cn.afterturn.easypoi.excel.imports.base.ImportBaseService.java

/**
 * ??//from   w  w  w  .  j a  v a 2  s. c o  m
 * @param targetId
 * @param fields
 * @param excelParams
 * @param excelCollection
 * @param pojoClass
 * @param getMethods
 * @throws Exception
 */
public void getAllExcelField(String targetId, Field[] fields, Map<String, ExcelImportEntity> excelParams,
        List<ExcelCollectionParams> excelCollection, Class<?> pojoClass, List<Method> getMethods,
        ExcelEntity excelEntityAnn) throws Exception {
    ExcelImportEntity excelEntity = null;
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        if (PoiPublicUtil.isNotUserExcelUserThis(null, field, targetId)) {
            continue;
        }
        if (PoiPublicUtil.isCollection(field.getType())) {
            // ?
            ExcelCollection excel = field.getAnnotation(ExcelCollection.class);
            ExcelCollectionParams collection = new ExcelCollectionParams();
            collection.setName(field.getName());
            Map<String, ExcelImportEntity> temp = new HashMap<String, ExcelImportEntity>();
            ParameterizedType pt = (ParameterizedType) field.getGenericType();
            Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0];
            collection.setType(clz);
            getExcelFieldList(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
                    PoiPublicUtil.getClassFields(clz), clz, temp, null);
            collection.setExcelParams(temp);
            collection.setExcelName(PoiPublicUtil
                    .getValueByTargetId(field.getAnnotation(ExcelCollection.class).name(), targetId, null));
            additionalCollectionName(collection);
            excelCollection.add(collection);
        } else if (PoiPublicUtil.isJavaClass(field) || field.getType().isEnum()) {
            addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams, excelEntityAnn);
        } else {
            List<Method> newMethods = new ArrayList<Method>();
            if (getMethods != null) {
                newMethods.addAll(getMethods);
            }
            //
            newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
            ExcelEntity excel = field.getAnnotation(ExcelEntity.class);
            if (excel.show() && StringUtils.isEmpty(excel.name())) {
                throw new ExcelImportException("if use ExcelEntity ,name mus has value ,data: "
                        + ReflectionToStringBuilder.toString(excel), ExcelImportEnum.PARAMETER_ERROR);
            }
            getAllExcelField(StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId,
                    PoiPublicUtil.getClassFields(field.getType()), excelParams, excelCollection,
                    field.getType(), newMethods, excel);
        }
    }
}

From source file:com.azaptree.services.security.impl.SecurityCredentialsServiceImpl.java

@Override
public Map<String, Class<?>> getSupportedCredentials() throws SecurityServiceException {
    final Map<String, Class<?>> supportedCredentials = new HashMap<>();
    for (Map.Entry<String, CredentialToByteSourceConverter<?>> entry : credentialToByteSourceConverters
            .entrySet()) {//from ww  w. j ava 2 s  . c  om
        final CredentialToByteSourceConverter<?> converter = entry.getValue();
        final Class<?> converterParamType;
        for (Type genericInterface : converter.getClass().getGenericInterfaces()) {
            if (genericInterface instanceof ParameterizedType) {
                final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
                if (((Class<?>) parameterizedType.getRawType())
                        .isAssignableFrom(CredentialToByteSourceConverter.class)) {
                    final Type type = parameterizedType.getActualTypeArguments()[0];
                    converterParamType = type instanceof Class<?> ? (Class<?>) type
                            : (Class<?>) ((ParameterizedType) type).getRawType();
                    supportedCredentials.put(entry.getKey(), converterParamType);
                    break;
                }
            }
        }
    }

    return supportedCredentials;
}

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 . jav  a  2  s  .  c o  m*/
    // 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;
}

From source file:org.romaframework.core.schema.SchemaHelper.java

/**
 * Return the generic type if iType uses Java5+ Generics.
 * //from  w ww. java2 s . com
 * @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;
}