List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.blocks4j.reconf.client.constructors.CollectionConstructor.java
public Object construct(MethodData data) throws Throwable { Class<?> returnClass;/*from ww w. jav a 2 s . c om*/ Type innerClass = null; if (data.getReturnType() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) data.getReturnType(); returnClass = (Class<?>) parameterized.getRawType(); if (parameterized.getActualTypeArguments()[0] instanceof ParameterizedType) { innerClass = parameterized.getActualTypeArguments()[0]; } else if (parameterized.getActualTypeArguments()[0] instanceof Class<?>) { innerClass = parameterized.getActualTypeArguments()[0]; } } 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 != 1) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } else { innerClass = parameterized.getActualTypeArguments()[0]; } } else { innerClass = 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); Collection<Object> collectionInstance = (Collection<Object>) constructor .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY); if (null == data.getValue()) { return collectionInstance; } for (String s : new StringParser(data).getTokens()) { Object o = ObjectConstructorFactory.get(innerClass) .construct(new MethodData(data.getMethod(), innerClass, s)); if (o != null) { collectionInstance.add(o); } } return collectionInstance; }
From source file:br.com.pfood.mb.imp.GenericMBImp.java
@Override public void init(GenericBO bo) { try {//from ww w . j a va 2s.co m ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); classe = (Class) parameterizedType.getActualTypeArguments()[0]; obj = classe.newInstance(); lista = new ArrayList<T>(); this.genericBO = bo; this.beanManager = new CdiUtils().getBeanManager(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } }
From source file:com.solace.data.jpa.GenericDao.java
public GenericDao() { try {//from w w w . ja v a 2 s. com ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); this.m_persistentClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1]; } catch (Exception e) { } }
From source file:me.ronghai.sa.dao.impl.AbstractModelDAOWithEMImpl.java
@SuppressWarnings("unchecked") public AbstractModelDAOWithEMImpl() { ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass(); this.entityClass = (Class<E>) type.getActualTypeArguments()[0]; }
From source file:org.easyj.rest.controller.AbstractGenericEntityController.java
protected Class<E> getEntityClass() { if (entityClass == null) { ParameterizedType pt = (ParameterizedType) getClass().getGenericSuperclass(); entityClass = (Class<E>) pt.getActualTypeArguments()[0]; }//from www . j ava2 s.co m return entityClass; }
From source file:org.datalorax.populace.core.util.TypeResolver.java
private Type resolveParameterisedType(final ParameterizedType type) { final TypeToken<?> typeToken = TypeToken.of(type); final Type[] resolvedArgs = Arrays.stream(type.getActualTypeArguments()).map(t -> resolve(t, typeToken)) .toArray(Type[]::new); return TypeUtils.parameterise(typeToken.getRawType(), resolvedArgs); }
From source file:com.googlecode.jsonschema2pojo.integration.AdditionalPropertiesIT.java
@Test public void additionalPropertiesOfStringArrayTypeOnly() throws SecurityException, NoSuchMethodException, ClassNotFoundException { ClassLoader resultsClassLoader = generateAndCompile( "/schema/additionalProperties/additionalPropertiesArraysOfStrings.json", "com.example"); Class<?> classWithNoAdditionalProperties = resultsClassLoader .loadClass("com.example.AdditionalPropertiesArraysOfStrings"); Method getter = classWithNoAdditionalProperties.getMethod("getAdditionalProperties"); ParameterizedType listType = (ParameterizedType) ((ParameterizedType) getter.getGenericReturnType()) .getActualTypeArguments()[1]; assertThat(listType.getActualTypeArguments()[0], is(equalTo((Type) String.class))); // setter with these types should exist: classWithNoAdditionalProperties.getMethod("setAdditionalProperties", String.class, List.class); }
From source file:org.jmingo.mapping.convert.ConverterService.java
private Set<Class<?>> getConverters(final Class<?> aClass) { Set<Class<?>> suitableConverters = new HashSet<>(); for (Class<?> converterClass : convertersClasses) { if (converterClass.getGenericInterfaces().length > 0 && converterClass.getGenericInterfaces()[0] instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) converterClass.getGenericInterfaces()[0]; if (pt.getActualTypeArguments().length > 0 && pt.getActualTypeArguments()[0] instanceof Class) { Class<?> at = (Class<?>) pt.getActualTypeArguments()[0]; if (aClass.equals(at)) { suitableConverters.add(converterClass); }// w ww.j a v a2 s . com } } } return suitableConverters; }
From source file:com.ewcms.common.query.mongo.PropertyConvert.java
/** * ?{@link RuntimeException}// w w w . j a va 2 s . c o m * * @param name ???{@literal null} * @return {@value Class<?>} */ public Class<?> getPropertyType(String propertyName) { if (!StringUtils.hasText(propertyName)) { throw new IllegalArgumentException("Property's name must not null or empty!"); } String[] names = StringUtils.tokenizeToStringArray(propertyName, NESTED); Class<?> type = beanClass; PropertyDescriptor pd = null; for (String name : names) { pd = BeanUtils.getPropertyDescriptor(type, name); if (pd == null) { logger.error("\"{}\" property isn't exist.", propertyName); throw new RuntimeException(propertyName + " property isn't exist."); } type = pd.getPropertyType(); } if (type.isArray()) { return type.getComponentType(); } if (Collection.class.isAssignableFrom(type)) { Method method = pd.getReadMethod(); if (method == null) { logger.error("\"{}\" property is not read method.", propertyName); throw new RuntimeException(propertyName + " property is not read method."); } ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); if (returnType.getActualTypeArguments().length > 0) { return (Class<?>) returnType.getActualTypeArguments()[0]; } logger.error("\"{}\" property is collection,but it's not generic.", propertyName); throw new RuntimeException(propertyName + " property is collection,but it's not generic."); } return type; }
From source file:org.openmobster.core.mobileObject.TestBeanSyntax.java
private Object initializeIndexedProperty(Object parentObject, String property, PropertyDescriptor propertyMetaData) throws Exception { Object element = null;/*from w w w. j av a2 s . c om*/ //Find the Class of the elementType Class elementType = null; if (!propertyMetaData.getPropertyType().isArray()) { ParameterizedType returnType = (ParameterizedType) propertyMetaData.getReadMethod() .getGenericReturnType(); Type[] actualTypes = returnType.getActualTypeArguments(); for (Type actualType : actualTypes) { elementType = (Class) actualType; } } else { elementType = propertyMetaData.getPropertyType().getComponentType(); } //An IndexedProperty Object indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); //Initialize the IndexedProperty (An Array or Collection) if (indexedProperty == null) { if (propertyMetaData.getPropertyType().isArray()) { //TODO: Remove hardcoded array size PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), Array.newInstance(elementType, 1)); } else { //Handle Collection Construction PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), new ArrayList()); } indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); } //Check to see if the index specified by the field requires creation of new //element try { element = PropertyUtils.getIndexedProperty(parentObject, property); } catch (IndexOutOfBoundsException iae) { Object newlyInitialized = elementType.newInstance(); if (!propertyMetaData.getPropertyType().isArray()) { ((Collection) indexedProperty).add(newlyInitialized); } else { //TODO: Remove hardcoded array index Array.set(indexedProperty, 0, newlyInitialized); } element = newlyInitialized; } return element; }