List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.lenskit.util.TypeUtils.java
/** * Extract the element type from a type token representing a list. * @param token The type token.//from www.ja va2 s. co m * @param <T> The list element type. * @return The type token for the list's element type. */ @SuppressWarnings("unchecked") public static <T> TypeToken<T> listElementType(TypeToken<? extends List<T>> token) { Type t = token.getType(); Preconditions.checkArgument(t instanceof ParameterizedType, "list type not resolved"); ParameterizedType pt = (ParameterizedType) t; Type[] args = pt.getActualTypeArguments(); assert args.length == 1; return (TypeToken<T>) TypeToken.of(args[0]); }
From source file:GenericUtils.java
/** * Get the Generic definitions from a class for given class without looking * super classes.//from w w w . ja v a2 s . co m * @param classFrom Implementing class * @param interfaceClz class with generic definition * @return null if not found */ @SuppressWarnings("unchecked") public static Type[] getGenericDefinitonsThis(Class classFrom, Class interfaceClz) { Type[] genericInterfaces = classFrom.getGenericInterfaces(); for (Type type : genericInterfaces) { if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (interfaceClz.isAssignableFrom((Class) pt.getRawType())) { return pt.getActualTypeArguments(); } } } // check if it if available on generic super class Type genericSuperclass = classFrom.getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericSuperclass; if (interfaceClz.equals(pt.getRawType())) { return pt.getActualTypeArguments(); } } return null; }
From source file:com.google.code.guice.repository.spi.TypeUtil.java
public static void getGenericSuperclassActualTypes(Collection<Type> types, Class aClass) { if (aClass != null && types != null) { Type superclass = aClass.getGenericSuperclass(); if (superclass instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) superclass; Type[] interfaces = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(interfaces)); } else if (superclass instanceof Class) { Class sClass = (Class) superclass; getGenericInterfacesActualTypes(types, sClass); getGenericSuperclassActualTypes(types, aClass.getSuperclass()); }/*from w ww. ja v a 2 s .com*/ } }
From source file:com.wavemaker.tools.apidocs.tools.parser.util.TypeUtil.java
protected static TypeInformation getParameterizedTypeTypeInformation(ParameterizedType parameterizedType) { List<Class<?>> typeArguments = new LinkedList<>(); Class<?> actualType = (Class<?>) parameterizedType.getRawType(); for (Type type : parameterizedType.getActualTypeArguments()) { TypeInformation typeInfo = extractTypeInformation(type); typeArguments.add(typeInfo.getActualType()); }/* w w w. j a va 2 s .co m*/ return new TypeInformation(actualType, typeArguments, false, parameterizedType); }
From source file:org.localmatters.serializer.util.ReflectionUtils.java
/** * Returns the return the type arguments of the given type. * @param type The type//from ww w .ja va 2s.co m * @return The list of the return type arguments or null */ public static Type[] getTypeArgumentsForType(Type type) { if (type instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) type; return paramType.getActualTypeArguments(); } if (type instanceof Class<?>) { Class<?> klass = (Class<?>) type; if (klass.isArray()) { return new Type[] { klass.getComponentType() }; } return getTypeArgumentsForType(((Class<?>) type).getGenericSuperclass()); } return null; }
From source file:io.neba.core.util.ReflectionUtil.java
/** * Resolves the generic type of a {@link Collection} from a {@link Field}, e.g. * * <pre>/* w w w .j ava 2 s . c om*/ * private List<MyModel> myModel -> MyModel. * </pre> * * @param field must not be <code>null</code>. * @return never null. */ public static Class<?> getCollectionComponentType(Class<?> definingType, Field field) { if (field == null) { throw new IllegalArgumentException("Method parameter field must not be null."); } // The generic type may contain the generic type declarations, e.g. List<String>. Type type = field.getGenericType(); if (!(type instanceof ParameterizedType)) { throw new IllegalArgumentException("Cannot obtain the component type of " + field + ", it does not declare generic type parameters."); } // Only the ParametrizedType contains reflection information about the actual type. ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); // We expect exactly one argument representing the model type. if (typeArguments.length != 1) { signalUnsupportedNumberOfTypeDeclarations(field); } Type componentType = typeArguments[0]; // Wildcard type <X ... Y> if (componentType instanceof WildcardType) { WildcardType wildcardType = (WildcardType) componentType; Type[] lowerBounds = wildcardType.getLowerBounds(); if (lowerBounds.length == 0) { throw new IllegalArgumentException("Cannot obtain the component type of " + field + ", it has a wildcard declaration with an upper" + " bound (<? extends Y>) and is thus read-only." + " Only simple type parameters (e.g. List<MyType>)" + " or lower bound wildcards (e.g. List<? super MyModel>)" + " are supported."); } componentType = lowerBounds[0]; } return getRawType(componentType, definingType); }
From source file:com.wrmsr.wava.driver.PassType.java
public static PassType newPassType(String name, Class<? extends Pass> cls) { ImmutableList.Builder<TypeLiteral<?>> inputs = ImmutableList.builder(); ImmutableList.Builder<TypeLiteral<?>> outputs = ImmutableList.builder(); InjectionPoint passCtor = InjectionPoint.forConstructorOf(cls); for (Dependency<?> dep : passCtor.getDependencies()) { Key key = dep.getKey();/*from www . j ava 2 s. co m*/ if (key.getAnnotation() instanceof PassInput) { inputs.add(key.getTypeLiteral()); } else if (key.getAnnotation() instanceof PassOutput) { checkState(key.getTypeLiteral().getRawType() == Consumer.class); ParameterizedType parameterized = (ParameterizedType) key.getTypeLiteral().getType(); java.lang.reflect.Type outputType = parameterized.getActualTypeArguments()[0]; outputs.add(TypeLiteral.get(outputType)); } } return new PassType(name, cls, inputs.build(), outputs.build()); }
From source file:com.google.code.guice.repository.spi.TypeUtil.java
public static void getGenericInterfacesActualTypes(Collection<Type> types, Class aClass) { if (aClass != null && types != null) { Type[] interfaces = aClass.getGenericInterfaces(); for (Type anInterface : interfaces) { if (anInterface instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) anInterface; Type[] actualTypes = parameterizedType.getActualTypeArguments(); types.addAll(Arrays.asList(actualTypes)); } else if (anInterface instanceof Class) { Class typeClass = (Class) anInterface; getGenericInterfacesActualTypes(types, typeClass); }/*from w w w . j a va 2s . c om*/ } } }
From source file:org.opendaylight.netvirt.federation.plugin.identifiers.FederationPluginIdentifierRegistry.java
@SuppressWarnings("unchecked") private static Class<? extends DataObject> getSubtreeClass( FederationPluginIdentifier<? extends DataObject, ? extends DataObject, ? extends DataObject> identifier) { for (Type type : identifier.getClass().getGenericInterfaces()) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType().equals(FederationPluginIdentifier.class)) { Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (actualTypeArguments != null && actualTypeArguments.length > 1) { return (Class<? extends DataObject>) actualTypeArguments[2]; }/* w w w. jav a 2s. c o m*/ } } return null; }
From source file:Main.java
private final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> declaringClass, Class<?> inClass) { if (inClass == null) return null; Class<?> superClass = null; Type resolvedType = null;/* w ww .ja va 2 s .co m*/ Type genericSuperClass = null; if (!declaringClass.equals(inClass)) { if (declaringClass.isInterface()) { // the declaringClass is an interface Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length && resolvedType == null; i++) { superClass = interfaces[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericInterfaces()[i]; } } if (resolvedType == null) { superClass = inClass.getSuperclass(); resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericSuperclass(); } } else { resolvedType = type; genericSuperClass = superClass = inClass; } if (resolvedType != null) { // if its another type this means we have finished if (resolvedType instanceof TypeVariable<?>) { type = (TypeVariable<?>) resolvedType; TypeVariable<?>[] parameters = superClass.getTypeParameters(); int positionInClass = 0; for (; positionInClass < parameters.length && !type.equals(parameters[positionInClass]); positionInClass++) { } // we located the position of the typevariable in the superclass if (positionInClass < parameters.length) { // let's look if we have type specialization information in the current class if (genericSuperClass instanceof ParameterizedType) { ParameterizedType pGenericType = (ParameterizedType) genericSuperClass; Type[] args = pGenericType.getActualTypeArguments(); return positionInClass < args.length ? args[positionInClass] : null; } } // we didnt find typevariable specialization in the class, so it's the best we can // do, lets return the resolvedType... } } return resolvedType; }