List of usage examples for java.lang.reflect ParameterizedType getRawType
Type getRawType();
From source file:org.apache.hadoop.yarn.webapp.hamlet.HamletGen.java
String getTypeName(Type type) { if (type instanceof Class<?>) { return ((Class<?>) type).getSimpleName(); }//w w w. ja va2s . c om ParameterizedType pt = (ParameterizedType) type; return ((Class<?>) pt.getRawType()).getSimpleName() + "<" + ((Class<?>) pt.getActualTypeArguments()[0]).getSimpleName() + ">"; }
From source file:GenericClass.java
private GenericClass(ParameterizedType type) { myclass = (Class<?>) type.getRawType(); recurse(myclass, myclass, type); coalesceMap(); }
From source file:com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer.java
/** * Returns the class for the given field via generic type check. * * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for. * @param property the property of the Map or Collection for the given parent class * @param element <tt>true</tt> for indexed types and Maps. * @return Class of the specified field. *///from w w w . ja va 2s . c om private Class getClass(Class parentClass, String property, boolean element) { try { Field field = reflectionProvider.getField(parentClass, property); Type genericType = null; // Check fields first if (field != null) { genericType = field.getGenericType(); } // Try to get ParameterType from setter method if (genericType == null || !(genericType instanceof ParameterizedType)) { try { Method setter = reflectionProvider.getSetMethod(parentClass, property); genericType = setter != null ? setter.getGenericParameterTypes()[0] : null; } catch (ReflectionException | IntrospectionException e) { // ignore } } // Try to get ReturnType from getter method if (genericType == null || !(genericType instanceof ParameterizedType)) { try { Method getter = reflectionProvider.getGetMethod(parentClass, property); genericType = getter.getGenericReturnType(); } catch (ReflectionException | IntrospectionException e) { // ignore } } if (genericType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) genericType; int index = (element && type.getRawType().toString().contains(Map.class.getName())) ? 1 : 0; Type resultType = type.getActualTypeArguments()[index]; if (resultType instanceof ParameterizedType) { return (Class) ((ParameterizedType) resultType).getRawType(); } return (Class) resultType; } } catch (Exception e) { LOG.debug("Error while retrieving generic property class for property: {}", property, e); } return null; }
From source file:de.escalon.hypermedia.spring.AffordanceBuilderFactory.java
private boolean containsCollection(Type genericReturnType) { final boolean ret; if (genericReturnType instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) genericReturnType; Type rawType = t.getRawType(); Assert.state(rawType instanceof Class<?>, "raw type is not a Class: " + rawType.toString()); Class<?> cls = (Class<?>) rawType; if (HttpEntity.class.isAssignableFrom(cls)) { Type[] typeArguments = t.getActualTypeArguments(); ret = containsCollection(typeArguments[0]); } else if (Resources.class.isAssignableFrom(cls) || Collection.class.isAssignableFrom(cls)) { ret = true;//w w w .j av a 2 s . co m } else { ret = false; } } else if (genericReturnType instanceof GenericArrayType) { ret = true; } else if (genericReturnType instanceof WildcardType) { WildcardType t = (WildcardType) genericReturnType; ret = containsCollection(getBound(t.getLowerBounds())) || containsCollection(getBound(t.getUpperBounds())); } else if (genericReturnType instanceof TypeVariable) { ret = false; } else if (genericReturnType instanceof Class) { Class<?> cls = (Class<?>) genericReturnType; ret = Resources.class.isAssignableFrom(cls) || Collection.class.isAssignableFrom(cls); } else { ret = false; } return ret; }
From source file:com.google.code.pathlet.web.ognl.impl.InstantiatingNullHandler.java
/** * Returns the class for the given field via generic type check. * * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for. * @param property the property of the Map or Collection for the given parent class * @param element <tt>true</tt> for indexed types and Maps. * @return Class of the specified field. *///from w w w . jav a 2s . c o m private Class getClass(Class parentClass, String property, boolean element) { try { Field field = reflectionProvider.getField(parentClass, property); Type genericType = null; // Check fields first if (field != null) { genericType = field.getGenericType(); } // Try to get ParameterType from setter method if (genericType == null || !(genericType instanceof ParameterizedType)) { try { Method setter = reflectionProvider.getSetMethod(parentClass, property); genericType = setter.getGenericParameterTypes()[0]; } catch (ReflectionException ognle) { ; // ignore } catch (IntrospectionException ie) { ; // ignore } } // Try to get ReturnType from getter method if (genericType == null || !(genericType instanceof ParameterizedType)) { try { Method getter = reflectionProvider.getGetMethod(parentClass, property); genericType = getter.getGenericReturnType(); } catch (ReflectionException ognle) { ; // ignore } catch (IntrospectionException ie) { ; // ignore } } if (genericType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) genericType; int index = (element && type.getRawType().toString().contains(Map.class.getName())) ? 1 : 0; Type resultType = type.getActualTypeArguments()[index]; if (resultType instanceof ParameterizedType) { return (Class) ((ParameterizedType) resultType).getRawType(); } return (Class) resultType; } } catch (Exception e) { throw new ConvertException(e); } return null; }
From source file:com.basistech.rosette.apimodel.ModelTest.java
private boolean isListString(ParameterizedType parameterizedType) { return List.class.equals(parameterizedType.getRawType()) && parameterizedType.getActualTypeArguments().length == 1 && String.class.equals(parameterizedType.getActualTypeArguments()[0]); }
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 ava 2 s . 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; } }
From source file:org.apache.sling.models.impl.injectors.OSGiServiceInjector.java
private Object getValue(Object adaptable, Type type, String filterString, DisposalCallbackRegistry callbackRegistry) { if (type instanceof Class) { Class<?> injectedClass = (Class<?>) type; if (injectedClass.isArray()) { Object[] services = getServices(adaptable, injectedClass.getComponentType(), filterString, callbackRegistry);/*from w ww.j ava 2 s . c om*/ if (services == null) { return null; } Object arr = Array.newInstance(injectedClass.getComponentType(), services.length); for (int i = 0; i < services.length; i++) { Array.set(arr, i, services[i]); } return arr; } else { return getService(adaptable, injectedClass, filterString, callbackRegistry); } } else if (type instanceof ParameterizedType) { 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<?> serviceType = (Class<?>) ptype.getActualTypeArguments()[0]; Object[] services = getServices(adaptable, serviceType, filterString, callbackRegistry); if (services == null) { return null; } return Arrays.asList(services); } else { log.warn("Cannot handle type {}", type); return null; } }
From source file:org.localmatters.serializer.util.ReflectionUtilsTest.java
/** * Test getting the type arguments for a type * @throws Exception When the test fails *///from w ww .jav a 2 s. c om public void testGetTypeArgumentsForType() throws Exception { assertNull(ReflectionUtils.getTypeArgumentsForType(String.class)); assertNull(ReflectionUtils.getTypeArgumentsForType(List.class)); assertNull(ReflectionUtils.getTypeArgumentsForType(Map.class)); Type[] types = ReflectionUtils.getTypeArgumentsForType(ParameterizedObject.class); assertNotNull(types); assertEquals(1, types.length); assertSame(String.class, types[0]); Class<?> cl = ObjectWithGenerics.class; Method method = cl.getMethod("getList", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNull(types); method = cl.getMethod("getListOfString", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(1, types.length); assertSame(String.class, types[0]); method = cl.getMethod("getListOfListOfString", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(1, types.length); assertTrue(types[0] instanceof ParameterizedType); ParameterizedType type = (ParameterizedType) types[0]; assertSame(List.class, type.getRawType()); types = ReflectionUtils.getTypeArgumentsForType(type); assertNotNull(types); assertEquals(1, types.length); assertSame(String.class, types[0]); method = cl.getMethod("getListOfParameterizedObject", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(1, types.length); assertTrue(types[0] instanceof Class<?>); Class<?> klass = (Class<?>) types[0]; types = ReflectionUtils.getTypeArgumentsForType(klass); assertNotNull(types); assertEquals(1, types.length); assertSame(String.class, types[0]); method = cl.getMethod("getListOfMapOfStringAndList", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(1, types.length); assertTrue(types[0] instanceof ParameterizedType); type = (ParameterizedType) types[0]; assertSame(Map.class, type.getRawType()); types = ReflectionUtils.getTypeArgumentsForType(type); assertNotNull(types); assertEquals(2, types.length); assertSame(String.class, types[0]); assertSame(List.class, types[1]); method = cl.getMethod("getMap", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(2, types.length); assertFalse(types[0] instanceof Class<?>); assertFalse(types[0] instanceof ParameterizedType); assertFalse(types[1] instanceof Class<?>); assertFalse(types[1] instanceof ParameterizedType); method = cl.getMethod("getMapOfStringAndDouble", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(2, types.length); assertTrue(types[0] instanceof Class<?>); assertSame(String.class, types[0]); assertTrue(types[1] instanceof Class<?>); assertSame(Double.class, types[1]); method = cl.getMethod("getArray", (Class<?>[]) null); types = ReflectionUtils.getTypeArgumentsForType(method.getGenericReturnType()); assertNotNull(types); assertEquals(1, types.length); assertTrue(types[0] instanceof Class<?>); assertSame(String.class, types[0]); }
From source file:org.evosuite.utils.generic.GenericAccessibleObject.java
/** * Maps type parameters in a type to their values. * /*ww w .ja va2 s .c o m*/ * @param toMapType * Type possibly containing type arguments * @param typeAndParams * must be either ParameterizedType, or (in case there are no * type arguments, or it's a raw type) Class * @return toMapType, but with type parameters from typeAndParams replaced. */ protected Type mapTypeParameters(Type toMapType, Type typeAndParams) { if (isMissingTypeParameters(typeAndParams)) { logger.debug("Is missing type parameters, so erasing types"); return GenericTypeReflector.erase(toMapType); } else { VarMap varMap = new VarMap(); Type handlingTypeAndParams = typeAndParams; while (handlingTypeAndParams instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) handlingTypeAndParams; Class<?> clazz = (Class<?>) pType.getRawType(); // getRawType should always be Class varMap.addAll(clazz.getTypeParameters(), pType.getActualTypeArguments()); handlingTypeAndParams = pType.getOwnerType(); } varMap.addAll(getTypeVariableMap()); return varMap.map(toMapType); } }