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:es.jffa.tsc.sip04.dao.hbn.AbstractHbnDao.java

/**
 *
 * @return//w ww .  j  a  v  a2  s.  c om
 */
private Class<T> _getDomainClass() {
    if (domainClass == null) {
        ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();

        this.domainClass = (Class<T>) thisType.getActualTypeArguments()[0];
    }

    return domainClass;
}

From source file:com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchemas.java

private static Class<?> unwrapGenericSetParam(Type setType) {
    if (!(setType instanceof ParameterizedType)) {
        LOGGER.warn("Set type " + setType + " is not a " + "ParameterizedType, using default marshaler and "
                + "unmarshaler!");
        return Object.class;
    }/*from   w  w w  . j  a v a2  s .  c  o  m*/

    ParameterizedType ptype = (ParameterizedType) setType;
    Type[] arguments = ptype.getActualTypeArguments();

    if (arguments.length != 1) {
        LOGGER.warn("Set type " + setType + " does not have exactly one "
                + "type argument, using default marshaler and " + "unmarshaler!");
        return Object.class;
    }

    if (arguments[0].toString().equals("byte[]")) {
        return byte[].class;
    } else {
        return (Class<?>) arguments[0];
    }
}

From source file:es.caib.sgtsic.ejb3.AbstractFacade.java

public boolean borrable(Object id) {

    T item = this.find(id);

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

    if (item == null) {
        return false;
    }/*ww w . ja  v a  2 s.  c o  m*/

    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:org.apache.airavata.db.AbstractThriftDeserializer.java

/**
 * Generates a {@link JavaType} that matches the target Thrift field represented by the provided
 * {@code <E>} enumerated value.  If the field's type includes generics, the generics will
 * be added to the generated {@link JavaType} to support proper conversion.
 * @param thriftInstance The Thrift-generated class instance that will be converted to/from JSON.
 * @param field A {@code <E>} enumerated value that represents a field in a Thrift-based entity.
 * @return The {@link JavaType} representation of the type associated with the field.
 * @throws NoSuchFieldException if unable to determine the field's type.
 * @throws SecurityException if unable to determine the field's type.
 *///  ww  w .j  av  a2  s .c om
protected JavaType generateValueType(final T thriftInstance, final E field)
        throws NoSuchFieldException, SecurityException {
    final TypeFactory typeFactory = TypeFactory.defaultInstance();

    final Field declaredField = thriftInstance.getClass().getDeclaredField(field.getFieldName());
    if (declaredField.getType().equals(declaredField.getGenericType())) {
        log.debug("Generating JavaType for type '{}'.", declaredField.getType());
        return typeFactory.constructType(declaredField.getType());
    } else {
        final ParameterizedType type = (ParameterizedType) declaredField.getGenericType();
        final Class<?>[] parameterizedTypes = new Class<?>[type.getActualTypeArguments().length];
        for (int i = 0; i < type.getActualTypeArguments().length; i++) {
            parameterizedTypes[i] = (Class<?>) type.getActualTypeArguments()[i];
        }
        log.debug("Generating JavaType for type '{}' with generics '{}'", declaredField.getType(),
                parameterizedTypes);
        return typeFactory.constructParametricType(declaredField.getType(), parameterizedTypes);
    }
}

From source file:com.alfresco.orm.repository.AlfrescoRespositoryProxyFactoryBean.java

public Class getTypeClass(Class clasz) {
    Class retClass = null;//from w w w. jav a  2 s  .com
    for (Type type : clasz.getGenericInterfaces()) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            // System.out.print("Return type is " + pt.getRawType() +
            // " with the following type arguments: ");
            for (Type t : pt.getActualTypeArguments()) {
                // System.out.println(t + " ");
                retClass = (Class) t;
            }
        } else {
            // System.out.println("Return type is " + type);
        }
    }
    return retClass;
}

From source file:com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java

@Override
public void deserialize(Object o, Field field, String value) throws SerializerException {
    try {/* w w  w  .j  av a2 s  .  c  om*/
        /*
         * get the parameterized type
         */
        final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
        final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
        /*
         * get cBean
         */
        final CBean<?> cBean = CBeanServer.getInstance().getCBean(containedType);
        /*
         * get the array object
         */
        @SuppressWarnings("unchecked")
        final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName());
        /*
         * iterate
         */
        final JSONArray jsonArray = new JSONArray(value);
        for (int i = 0; i < jsonArray.length(); i++) {
            final String key = jsonArray.getString(i);
            final Object oo = cBean.load(new CBeanKey(key));
            /*
             * oo could be null. if that happens, it can be because the underlying object has been deleted. this is ok. We add the null into the the list so that we preserve the list LENGTH
             */
            list.add(oo);
        }
    } catch (final Exception e) {
        throw new SerializerException(e);
    }
}

From source file:com.opensymphony.able.service.JpaCrudService.java

protected JpaCrudService() {
    ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
    Type[] typeArguments = genericSuperclass.getActualTypeArguments();
    this.entityClass = (Class<E>) typeArguments[0];
}

From source file:com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java

@Override
public void delete(Object o, Field field) throws SerializerException {
    final Property property = field.getAnnotation(Property.class);
    if ((null != property) && (property.cascadeDelete() == true)) {
        try {//from   ww  w  .j av  a2s  . com
            /*
             * get the parameterized type
             */
            final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
            final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0];
            /*
             * get cBean
             */
            final CBean<Object> cBean = CBeanServer.getInstance().getCBean(containedType);
            /*
             * get the array object
             */
            @SuppressWarnings("unchecked")
            final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName());
            /*
             * iterate
             */
            for (final Object obj : list) {
                final String key = cBean.getId(obj);
                cBean.delete(new CBeanKey(key));
            }
        } catch (final Exception e) {
            throw new SerializerException(e);
        }
    }
}

From source file:com.netflix.governator.lifecycle.ConfigurationProcessor.java

void assignConfiguration(Object obj, Field field, Map<String, String> contextOverrides) throws Exception {
    Configuration configuration = field.getAnnotation(Configuration.class);
    String configurationName = configuration.value();
    ConfigurationKey key = new ConfigurationKey(configurationName,
            KeyParser.parse(configurationName, contextOverrides));

    Object value = null;// w ww. j  a va  2  s.com

    boolean has = configurationProvider.has(key);
    if (has) {
        try {
            if (Supplier.class.isAssignableFrom(field.getType())) {
                ParameterizedType type = (ParameterizedType) field.getGenericType();
                Class<?> actualType = (Class<?>) type.getActualTypeArguments()[0];
                Supplier<?> current = (Supplier<?>) field.get(obj);
                value = getConfigurationSupplier(field, key, actualType, current);
                if (value == null) {
                    log.error("Field type not supported: " + actualType + " (" + field.getName() + ")");
                    field = null;
                }
            } else {
                Supplier<?> supplier = getConfigurationSupplier(field, key, field.getType(),
                        Suppliers.ofInstance(field.get(obj)));
                if (supplier == null) {
                    log.error("Field type not supported: " + field.getType() + " (" + field.getName() + ")");
                    field = null;
                } else {
                    value = supplier.get();
                }
            }
        } catch (IllegalArgumentException e) {
            ignoreTypeMismtachIfConfigured(configuration, configurationName, e);
            field = null;
        } catch (ConversionException e) {
            ignoreTypeMismtachIfConfigured(configuration, configurationName, e);
            field = null;
        }
    }

    if (field != null) {
        String defaultValue;
        if (Supplier.class.isAssignableFrom(field.getType())) {
            defaultValue = String.valueOf(((Supplier<?>) field.get(obj)).get());
        } else {
            defaultValue = String.valueOf(field.get(obj));
        }

        String documentationValue;
        if (has) {
            field.set(obj, value);

            documentationValue = String.valueOf(value);
            if (Supplier.class.isAssignableFrom(field.getType())) {
                documentationValue = String.valueOf(((Supplier<?>) value).get());
            } else {
                documentationValue = String.valueOf(documentationValue);
            }
        } else {
            documentationValue = "";
        }
        configurationDocumentation.registerConfiguration(field, configurationName, has, defaultValue,
                documentationValue, configuration.documentation());
    }
}

From source file:cf.nats.DefaultCfNats.java

@Override
public <R extends MessageBody<Void>> Request request(MessageBody<R> message, long timeout, TimeUnit unit,
        final RequestResponseHandler<R> handler) {
    final String subject = getPublishSubject(message);
    final String encoding = encode(message);
    final Type[] genericInterfaces = message.getClass().getGenericInterfaces();
    final ParameterizedType replyType = (ParameterizedType) genericInterfaces[0];
    final Class<R> messageReplyClass = (Class<R>) replyType.getActualTypeArguments()[0];

    return nats.request(subject, encoding, timeout, unit,
            createMessageHandler(messageReplyClass, new PublicationHandler<R, Void>() {
                @Override//from  www.  j av a 2  s . c om
                public void onMessage(Publication<R, Void> publication) {
                    handler.onResponse(publication);
                }
            }));
}