List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.BaseCrudController.java
/** * @return a Resource for the actual parameterized type of this superclass *//*w w w .j av a2 s .co m*/ @SuppressWarnings("unchecked") protected R getResource() { ParameterizedType t = (ParameterizedType) getClass().getGenericSuperclass(); Class<R> clazz = (Class<R>) t.getActualTypeArguments()[0]; return Context.getService(RestService.class).getResource(clazz); }
From source file:org.openehr.adl.rm.RmBeanReflector.java
private Class<?> extractGenericType(Class<?> beanClass, PropertyDescriptor pd) throws ReflectiveOperationException { checkArgument(List.class.isAssignableFrom(pd.getPropertyType())); Field field = getField(beanClass, pd); ParameterizedType stringListType = (ParameterizedType) field.getGenericType(); return (Class<?>) stringListType.getActualTypeArguments()[0]; }
From source file:org.kuali.student.r2.common.dao.GenericEntityDao.java
private Type getEntityType(Object object) { Type type = object.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) type; Type result = paramType.getActualTypeArguments()[0]; return result; } else {//from w ww. java 2 s . c o m throw new IllegalArgumentException("Could not guess entity type by reflection."); } }
From source file:com.qubole.rubix.core.CachingFileSystem.java
@SuppressWarnings("unchecked") private Class<T> getTypeParameterClass() { Type type = getClass().getGenericSuperclass(); ParameterizedType paramType = (ParameterizedType) type; return (Class<T>) paramType.getActualTypeArguments()[0]; }
From source file:org.jboss.windup.reporting.transformers.MetaResultTransformer.java
protected Class<?> transformerType() { Class<?> result = null; Type type = this.getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] fieldArgTypes = pt.getActualTypeArguments(); result = (Class<?>) fieldArgTypes[0]; }/*from w ww .j av a2 s . c om*/ return result; }
From source file:org.ocelotds.core.services.ArgumentConvertor.java
private JavaType getJavaType(Type type) { Class clazz;// w w w. j av a 2 s.co m logger.debug("Computing type of {} - {}", type.getClass(), type.toString()); if (type instanceof ParameterizedType) { clazz = (Class) ((ParameterizedType) type).getRawType(); } else { clazz = (Class) type; } JavaType javaType; Type actualType; if (Collection.class.isAssignableFrom(clazz)) { ParameterizedType pt = (ParameterizedType) type; actualType = pt.getActualTypeArguments()[0]; JavaType t1 = getJavaType(actualType); javaType = CollectionType.construct(Collection.class, t1); } else if (clazz.isArray()) { Class t = clazz.getComponentType(); JavaType t1 = getJavaType(t); javaType = ArrayType.construct(t1, null, null); } else if (Map.class.isAssignableFrom(clazz)) { ParameterizedType pt = (ParameterizedType) type; actualType = pt.getActualTypeArguments()[0]; JavaType t1 = getJavaType(actualType); actualType = pt.getActualTypeArguments()[1]; JavaType t2 = getJavaType(actualType); javaType = MapType.construct(Map.class, t1, t2); } else { javaType = SimpleType.construct(clazz); } return javaType; }
From source file:com.adobe.cq.wcm.core.components.testing.WCMUsePojoBaseTest.java
/** * Provides a spied object that can be used for further mocking. * * @return the spied object//from w w w . j ava2s . co m * @throws IllegalArgumentException if an object of type {@code T} cannot be instantiated */ protected T getSpiedObject() { ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass(); Class<T> type = (Class<T>) superClass.getActualTypeArguments()[0]; T real; try { real = type.newInstance(); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate object of class " + type.getName(), e); } return spy(real); }
From source file:com.autentia.common.util.ClassWithList.java
@Ignore("No es un test, son pruebas con reflection") @Test/*from ww w . j a v a 2 s . c o m*/ public void testGetGenericFromList() throws SecurityException, NoSuchMethodException { final ClassWithList<String> stringList = new ClassWithList<String>(); for (TypeVariable<?> v : stringList.getClass().getTypeParameters()) { print(v); } final Method method = stringList.getClass().getMethod("getElements"); Type returnType = method.getGenericReturnType(); if (returnType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); for (Type typeArgument : typeArguments) { System.out.println(typeArgument); print(typeArgument); TypeVariable<?> typeVariable = (TypeVariable<?>) typeArgument; Class typeArgClass = (Class) typeArgument; System.out.println("typeArgClass = " + typeArgClass); } } }
From source file:com.mingo.convert.ConverterService.java
private <T> Set<Class<? extends Converter>> getConverters(final Class<T> aClass) { Set<Class<? extends Converter>> suitableConverters = new HashSet<>(); for (Class<? extends Converter> 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); }//from w w w . j av a2 s .c o m } } } return suitableConverters; }
From source file:org.apache.sling.models.impl.injectors.ValueMapInjector.java
public Object getValue(@Nonnull Object adaptable, String name, @Nonnull Type type, @Nonnull AnnotatedElement element, @Nonnull DisposalCallbackRegistry callbackRegistry) { ValueMap map = getValueMap(adaptable); if (map == null) { return null; } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; try {//from w w w . j a v a 2s .c o m return map.get(name, clazz); } catch (ClassCastException e) { // handle case of primitive/wrapper arrays if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); if (componentType.isPrimitive()) { Class<?> wrapper = ClassUtils.primitiveToWrapper(componentType); if (wrapper != componentType) { Object wrapperArray = map.get(name, Array.newInstance(wrapper, 0).getClass()); if (wrapperArray != null) { return unwrapArray(wrapperArray, componentType); } } } else { Class<?> primitiveType = ClassUtils.wrapperToPrimitive(componentType); if (primitiveType != componentType) { Object primitiveArray = map.get(name, Array.newInstance(primitiveType, 0).getClass()); if (primitiveArray != null) { return wrapArray(primitiveArray, componentType); } } } } return null; } } else if (type instanceof ParameterizedType) { // list support ParameterizedType pType = (ParameterizedType) type; if (pType.getActualTypeArguments().length != 1) { return null; } Class<?> collectionType = (Class<?>) pType.getRawType(); if (!(collectionType.equals(Collection.class) || collectionType.equals(List.class))) { return null; } Class<?> itemType = (Class<?>) pType.getActualTypeArguments()[0]; Object array = map.get(name, Array.newInstance(itemType, 0).getClass()); if (array == null) { return null; } return Arrays.asList((Object[]) array); } else { log.debug("ValueMapInjector doesn't support non-class types {}", type); return null; } }