List of usage examples for java.lang Class getGenericSuperclass
public Type getGenericSuperclass()
From source file:org.springframework.core.GenericTypeResolver.java
private static Class[] doResolveTypeArguments(Class ownerClass, Class classToIntrospect, Class genericIfc) { while (classToIntrospect != null) { if (genericIfc.isInterface()) { Type[] ifcs = classToIntrospect.getGenericInterfaces(); for (Type ifc : ifcs) { Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc); if (result != null) { return result; }/* w w w. j a v a 2 s .co m*/ } } else { Class[] result = doResolveTypeArguments(ownerClass, classToIntrospect.getGenericSuperclass(), genericIfc); if (result != null) { return result; } } classToIntrospect = classToIntrospect.getSuperclass(); } return null; }
From source file:Main.java
/** * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned, * otherwise null. For example :/*from w w w . j av a2 s . c o m*/ * * <pre> * abstract class MyClass implements Serializer<Number> { * * } * * // type value will be the parameterized type Serializer<Number> * Type type = lookupGenericType(Serializer.class, MyClass.class); * </pre> */ public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { // lets look if the interface is directly implemented by fromClass Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { // do they match? if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } // ok it's not one of the directly implemented interfaces, lets try extended class Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); }
From source file:org.soybeanMilk.SbmUtils.java
/** * ???/* www . j ava 2s. c om*/ * <pre> * class A<T>{} * class B extends A<Integer>{} * </pre> * <code>extractTypeVariablesInType(B.class, map)</code><code>map</code> * <pre> * T -------> Integer * </pre> * @param source * @param container * @date 2012-5-14 */ private static void extractTypeVariablesInType(Type source, Map<TypeVariable<?>, Type> variableTypesMap) { if (source == null) return; else if (source instanceof Class<?>) { Class<?> clazz = (Class<?>) source; //? Type[] genericInterfaces = clazz.getGenericInterfaces(); if (genericInterfaces != null) { for (Type t : genericInterfaces) extractTypeVariablesInType(t, variableTypesMap); } // Type genericSuperType = clazz.getGenericSuperclass(); Class<?> superClass = clazz.getSuperclass(); while (superClass != null && !Object.class.equals(superClass)) { extractTypeVariablesInType(genericSuperType, variableTypesMap); genericSuperType = superClass.getGenericSuperclass(); superClass = superClass.getSuperclass(); } // Class<?> outerClass = clazz; while (outerClass.isMemberClass()) { Type genericOuterType = outerClass.getGenericSuperclass(); extractTypeVariablesInType(genericOuterType, variableTypesMap); outerClass = outerClass.getEnclosingClass(); } } else if (source instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) source; if (isClassType(pt.getRawType())) { Type[] actualArgTypes = pt.getActualTypeArguments(); TypeVariable<?>[] typeVariables = narrowToClass(pt.getRawType()).getTypeParameters(); for (int i = 0; i < actualArgTypes.length; i++) { TypeVariable<?> var = typeVariables[i]; Type value = actualArgTypes[i]; //???? if (value instanceof TypeVariable<?>) { Type actual = variableTypesMap.get(value); if (actual != null) value = actual; } variableTypesMap.put(var, value); } } //?? extractTypeVariablesInType(pt.getRawType(), variableTypesMap); } }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
public static Class<?> determineGenericsType(Class<?> clazz, Method method, boolean isReadMethod) { Class<?> result = null; if (isReadMethod) { Type parameterType = method.getGenericReturnType(); result = determineGenericsType(parameterType); } else {//from w ww. j a v a 2s .c om Type[] parameterTypes = method.getGenericParameterTypes(); if (parameterTypes != null) { result = determineGenericsType(parameterTypes[0]); } } if (result == null) { // Have a look at generic superclass Type genericSuperclass = clazz.getGenericSuperclass(); if (genericSuperclass != null) { result = determineGenericsType(genericSuperclass); } } return result; }
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;//from ww w . j av a 2 s . c o 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; }
From source file:com.appleframework.core.utils.ClassUtility.java
/** * Returns the generic type with the given index from the given * {@link Class}. Scan all base classes until finding a generic type. * /*from w ww . j a v a2 s .co m*/ * @param clazz * Class where seeking the generic type * @param index * @return the generic type */ public static Class<?> getGenericType(Class<?> clazz, int index) { Type genericSuperclass = clazz.getGenericSuperclass(); if (genericSuperclass == null) { return null; } Class<?> effectiveClass = clazz; while (!(genericSuperclass instanceof ParameterizedType)) { effectiveClass = effectiveClass.getSuperclass(); genericSuperclass = effectiveClass.getGenericSuperclass(); if (effectiveClass.equals(Object.class)) { return null; } } return (Class<?>) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[index]; }
From source file:net.jodah.typetools.TypeResolver.java
/** * Returns the generic {@code type} using type variable information from the {@code subType} else {@code null} if the * generic type cannot be resolved./*from w w w . ja va2s. c om*/ * * @param type to resolve generic type for * @param subType to extract type variable information from * @return generic {@code type} else {@code null} if it cannot be resolved */ public static Type resolveGenericType(Class<?> type, Type subType) { Class<?> rawType; if (subType instanceof ParameterizedType) rawType = (Class<?>) ((ParameterizedType) subType).getRawType(); else rawType = (Class<?>) subType; if (type.equals(rawType)) return subType; Type result; if (type.isInterface()) { for (Type superInterface : rawType.getGenericInterfaces()) if (superInterface != null && !superInterface.equals(Object.class)) if ((result = resolveGenericType(type, superInterface)) != null) return result; } Type superClass = rawType.getGenericSuperclass(); if (superClass != null && !superClass.equals(Object.class)) if ((result = resolveGenericType(type, superClass)) != null) return result; return null; }
From source file:io.werval.runtime.util.TypeResolver.java
/** * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from the * {@code initialType}.//from w ww . ja v a2 s . c o m * * @param initialType Initial type * @param targetType Target type * * @return Generic type */ public static Type resolveGenericType(Type initialType, Class<?> targetType) { Class<?> rawType; if (initialType instanceof ParameterizedType) { rawType = (Class<?>) ((ParameterizedType) initialType).getRawType(); } else { rawType = (Class<?>) initialType; } if (targetType.equals(rawType)) { return initialType; } Type result; if (targetType.isInterface()) { for (Type superInterface : rawType.getGenericInterfaces()) { if (superInterface != null && !superInterface.equals(Object.class)) { result = resolveGenericType(superInterface, targetType); if (result != null) { return result; } } } } Type superType = rawType.getGenericSuperclass(); if (superType != null && !superType.equals(Object.class)) { result = resolveGenericType(superType, targetType); if (result != null) { return result; } } return null; }
From source file:com.senacor.core.manager.BaseManagerImpl.java
private ParameterizedType findParameterizedSuperclass() { Class c = this.getClass(); Type t;/* w ww. j av a 2s. com*/ do { t = c.getGenericSuperclass(); c = c.getSuperclass(); } while ((t == null) || !(t instanceof ParameterizedType)); return (ParameterizedType) t; }
From source file:de.openknowledge.domain.SimpleValueObjectBuilder.java
private SimpleValueObjectBuilder(Class<V> valueObjectClass) { ParameterizedType valueObjectSuperclass = (ParameterizedType) valueObjectClass.getGenericSuperclass(); Class<?> simpleClass = (Class<?>) valueObjectSuperclass.getActualTypeArguments()[0]; try {/* w w w . j a v a2 s . co m*/ valueObjectConstructor = valueObjectClass.getConstructor(simpleClass); } catch (NoSuchMethodException ex) { if (ClassUtils.isPrimitiveWrapper(simpleClass)) { try { valueObjectConstructor = valueObjectClass .getConstructor(ClassUtils.wrapperToPrimitive(simpleClass)); } catch (NoSuchMethodException e) { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } else { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } if (simpleClass.isPrimitive()) { simpleClass = ClassUtils.primitiveToWrapper(simpleClass); } try { simpleValueConstructor = simpleClass.getConstructor(String.class); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Value Object simple type " + simpleClass.getName() + " requires String-Constructor to be used with JSF Converter"); } }