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.facebook.stetho.json.ObjectMapper.java

private List<Object> convertArrayToList(Field field, JSONArray array)
        throws IllegalAccessException, JSONException {
    if (List.class.isAssignableFrom(field.getType())) {
        ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
        Type[] types = parameterizedType.getActualTypeArguments();
        if (types.length != 1) {
            throw new IllegalArgumentException(
                    "Only able to handle a single type in a list " + field.getName());
        }/*from   w w w  . ja v  a 2 s.  c om*/
        Class arrayClass = (Class) types[0];
        List<Object> objectList = new ArrayList<Object>();
        for (int i = 0; i < array.length(); ++i) {
            if (arrayClass.isEnum()) {
                objectList.add(getEnumValue(array.getString(i), arrayClass));
            } else if (canDirectlySerializeClass(arrayClass)) {
                objectList.add(array.get(i));
            } else {
                JSONObject jsonObject = array.getJSONObject(i);
                if (jsonObject == null) {
                    objectList.add(null);
                } else {
                    objectList.add(convertValue(jsonObject, arrayClass));
                }
            }
        }
        return objectList;
    } else {
        throw new IllegalArgumentException("only know how to deserialize List<?> on field " + field.getName());
    }
}

From source file:es.caib.sgtsic.utils.ejb.AbstractService.java

public boolean borrable(Object id) {

    T item = this.find(id);

    log.debug("Entramos a borrable " + item);

    if (item == null) {
        return false;
    }/* w  ww . j  a  va 2  s. c  om*/

    log.debug("Entramos a borrable con no false " + item);

    for (Field f : entityClass.getDeclaredFields()) {
        boolean hasToManyAnnotations = (f.isAnnotationPresent(OneToMany.class))
                || (f.isAnnotationPresent(ManyToMany.class));
        if (hasToManyAnnotations) {

            Type type = f.getGenericType();
            ParameterizedType pt = (ParameterizedType) type;

            List<Type> arguments = Arrays.asList(pt.getActualTypeArguments());

            Class childEntityClass = null;

            for (Type argtype : arguments) {
                childEntityClass = (Class) argtype;
                break;
            }

            if (childEntityClass == null) {
                continue;
            }

            if (this.childrenCount(id, childEntityClass, entityClass) > 0) {

                log.debug("Cuenta positiva");

                return false;
            }
        }
    }

    log.debug("Cuenta 0");

    return true;

}

From source file:com.opensymphony.able.introspect.PropertyInfo.java

/**
 * Returns the component type of the property - e.g. ignoring the
 * cardinality (array or List)./*from   www .  j ava 2s .  c  o  m*/
 */
public Class getPropertyComponentType() {
    Class<?> propertyType = descriptor.getPropertyType();

    if (propertyType.isArray()) {
        return propertyType.getComponentType();
    }
    if (Collection.class.isAssignableFrom(propertyType)) {
        if (descriptor.getReadMethod() != null) {
            ParameterizedType genericSuperclass = (ParameterizedType) descriptor.getReadMethod()
                    .getGenericReturnType();
            Type[] typeArguments = genericSuperclass.getActualTypeArguments();
            return (Class) typeArguments[0];
        } else {
            // no idea what the type is
            // TODO do we have an annotation?
            return Object.class;
        }
    }
    return propertyType;
}

From source file:com.springinpractice.ch11.web.controller.AbstractCrudController.java

@SuppressWarnings("unchecked")
public AbstractCrudController() {
    ParameterizedType paramType = (ParameterizedType) getClass().getGenericSuperclass();
    this.ciClass = (Class<T>) paramType.getActualTypeArguments()[0];
}

From source file:org.apache.openejb.util.proxy.QueryProxy.java

private Class<?> getGenericType(final Type type) {
    if (type instanceof ParameterizedType) {
        final ParameterizedType pt = (ParameterizedType) type;
        if (pt.getActualTypeArguments().length == 1) {
            return (Class<?>) pt.getActualTypeArguments()[0];
        }/*from   w ww.j  ava2s  .  c o m*/
    }
    return Class.class.cast(type);
}

From source file:com.github.cherimojava.data.spring.EntityConverter.java

@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
    if (MediaType.APPLICATION_JSON.equals(mediaType)) {
        if (type instanceof Class) {
            // check if this is a simple entity class
            return Entity.class.isAssignableFrom((Class) type);
        }//ww w  .j a v a 2s .c  o  m
        if (type instanceof ParameterizedType) {
            // is this a parameterized type
            ParameterizedType pt = (ParameterizedType) type;
            if (pt.getRawType() instanceof Class
                    && Collection.class.isAssignableFrom((Class) pt.getRawType())) {
                // is this rawtype a class and is this class some collection
                Type generic = pt.getActualTypeArguments()[0];
                if (generic instanceof Class && Entity.class.isAssignableFrom((Class) generic)) {
                    // is this collection generic an entity
                    return true;
                }
            }
        }
    }
    return false;
}

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;
        }// w w w  .j  a v  a  2  s.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);
            }/*from   w  w w  .  j av a 2  s.co m*/
            return converter;
        }
    }

    return null;
}

From source file:org.phenotips.entities.internal.AbstractPrimaryEntityManager.java

/**
 * Gets the concrete class of the managed PrimaryEntity. The base implementation assumes that the class is passed
 * for the first generic parameter./*from   ww w  .  j av a 2 s . c  o m*/
 *
 * @return a class
 * @throws AbstractMethodError if the manager class doesn't properly define the generic parameter
 */
@SuppressWarnings("unchecked")
protected Class<? extends E> getEntityClass() {
    if (this.eclass == null) {
        Type cls = this.getClass().getGenericSuperclass();
        while (cls instanceof Class) {
            cls = ((Class<?>) cls).getGenericSuperclass();
        }
        ParameterizedType pcls = (ParameterizedType) cls;
        if (pcls.getActualTypeArguments().length == 0 || !(pcls.getActualTypeArguments()[0] instanceof Class)) {
            this.logger.error(
                    "Invalid component configuration: {} does not define a real class for the <E> parameter",
                    this.getClass().getName());
            throw new AbstractMethodError("The PrimaryEntityManager class " + this.getClass().getCanonicalName()
                    + " must define a real class for the <E> parameter");
        }
        this.eclass = (Class<E>) pcls.getActualTypeArguments()[0];
    }
    return this.eclass;
}

From source file:org.kuali.rice.krad.uif.util.CopyUtils.java

/**
 * Get a field reference for temporary use while deep cloning.
 * /*from  ww  w.  ja v a2  s.  c  om*/
 * <p>
 * Call {@link #recycle(CopyReference)} when done working with the reference.
 * </p>
 * 
 * @param source The source object.
 * @param target The target object.
 * @param field The field to use as the reference target.
 * 
 * @return A field reference for temporary use while deep cloning.
 */
private static <T> FieldReference<T> getFieldReference(Object source, Object target, Field field,
        CopyReference<T> pref) {
    @SuppressWarnings("unchecked")
    FieldReference<T> ref = RecycleUtils.getRecycledInstance(FieldReference.class);

    if (ref == null) {
        ref = new FieldReference<T>();
    }

    ref.source = source;
    ref.target = target;
    ref.field = field;

    DelayedCopy delayedCopy = field.getAnnotation(DelayedCopy.class);
    ref.delayAvailable = delayedCopy != null && (!delayedCopy.inherit() || pref.isDelayAvailable());

    Map<String, Type> pTypeVars = pref.getTypeVariables();

    if (pTypeVars != null && source != null) {
        Class<?> sourceType = source.getClass();
        Class<?> targetClass = pref.getTargetClass();
        Type targetType = ObjectPropertyUtils.findGenericType(sourceType, targetClass);
        if (targetType instanceof ParameterizedType) {
            ParameterizedType parameterizedTargetType = (ParameterizedType) targetType;
            Type[] params = parameterizedTargetType.getActualTypeArguments();
            for (int j = 0; j < params.length; j++) {
                if (params[j] instanceof TypeVariable<?>) {
                    Type pType = pTypeVars.get(targetClass.getTypeParameters()[j].getName());
                    ref.typeVariables.put(((TypeVariable<?>) params[j]).getName(), pType);
                }
            }
        }
    }

    Class<?> rawType = field.getType();
    Type genericType = field.getGenericType();
    if (genericType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) genericType;
        TypeVariable<?>[] typeParams = rawType.getTypeParameters();
        Type[] params = parameterizedType.getActualTypeArguments();
        assert params.length == typeParams.length;
        for (int i = 0; i < params.length; i++) {
            Type paramType = params[i];
            if (paramType instanceof TypeVariable<?>) {
                Type fType = ref.typeVariables.get(((TypeVariable<?>) paramType).getName());
                if (fType != null) {
                    paramType = fType;
                }
            }
            ref.typeVariables.put(typeParams[i].getName(), paramType);
        }
    }
    return ref;
}