List of usage examples for java.lang Class getGenericInterfaces
public Type[] getGenericInterfaces()
From source file:com.project.framework.util.ReflectionUtils.java
/** * ??, Class??./*from ww w. j a v a 2 s.c o m*/ * , null Class Array. * * @param clazz * @param interfaze * @param index * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Class getInterfaceGenricType(final Class clazz, final Class interfaze, final int index) { if (!interfaze.isInterface()) throw new IllegalArgumentException("interfaze must be a interface."); if (!interfaze.isAssignableFrom(clazz)) throw new IllegalArgumentException("clazz must be implemnet form interfaze"); Type[] genTypes = clazz.getGenericInterfaces(); for (Type genType : genTypes) { if (genType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genType; if (interfaze.equals(pt.getRawType())) { Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index] instanceof Class)) { logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; } } } 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 w w . j a v a 2 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.nabla.wapp.server.general.Util.java
public static Class getGenericDeclaration(Class clazz, int position) { Assert.argumentNotNull(clazz);//ww w . j a v a 2s . c om Assert.state(position >= 0); Type classGenType = clazz.getGenericSuperclass(); // special CGLIB workaround -- get generic superclass of superclass if (clazz.getName().contains("$$EnhancerByCGLIB$$")) { classGenType = clazz.getSuperclass().getGenericSuperclass(); } if (classGenType instanceof ParameterizedType) { Type[] params = ((ParameterizedType) classGenType).getActualTypeArguments(); if ((params != null) && (params.length > position)) { return (Class) params[position]; } } for (Type ifGenType : clazz.getGenericInterfaces()) { if (ifGenType instanceof ParameterizedType) { Type[] params = ((ParameterizedType) ifGenType).getActualTypeArguments(); if ((params != null) && (params.length > position)) { return (Class) params[position]; } } } if (log.isErrorEnabled()) log.error("fail to find class of generic parameter " + position + " of class '" + clazz.getSimpleName() + "'"); 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;//from ww w . ja v a2 s . com 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: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./*w w w. j ava 2s. co m*/ * * @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:de.micromata.genome.util.runtime.ClassUtils.java
/** * Looks for the concreate Class of a generic of given classRequested. * * This implementation looks first in super classes and then in super interfaces. * * @param <T> the generic type//from w ww . jav a 2 s . c om * @param clazz concrete class * @param classRequested class or interface implementing it. * @return null if not found. */ public static <T> Class<T> getGenericTypeArgument(Class<?> clazz, Class<T> classRequested) // NOSONAR "Methods should not be too complex" trivial { Type genericSuperclass = clazz.getGenericSuperclass(); if (genericSuperclass != null) { Class<T> ret = getGenericTypeArgumentFromGenericSuperType(genericSuperclass, classRequested); if (ret != null) { return ret; } } Type[] genericInterfaces = clazz.getGenericInterfaces(); if (genericInterfaces == null) { return null; } for (Type genericInterface : genericInterfaces) { Class<T> ret = getGenericTypeArgumentFromGenericSuperType(genericInterface, classRequested); if (ret != null) { return ret; } } Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null) { return getGenericTypeArgument(superClazz, classRequested); } return null; }
From source file:io.coala.factory.ClassUtil.java
/** * Get the actual type arguments a child class has used to extend a generic * base class. See <a// w w w .j a v a 2s . c o m * href="http://www.artima.com/weblogs/viewpost.jsp?thread=208860" * >description</a> * * @param genericAncestorType the base class * @param concreteDescendantType the child class * @return a list of the raw classes for the actual type arguments. */ public static <T> List<Class<?>> getTypeArguments(final Class<T> genericAncestorType, final Class<? extends T> concreteDescendantType) { // sanity check if (genericAncestorType == null) throw CoalaExceptionFactory.VALUE_NOT_SET.createRuntime("genericAncestorType"); if (concreteDescendantType == null) throw CoalaExceptionFactory.VALUE_NOT_SET.createRuntime("concreteDescendantType"); Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); Type type = concreteDescendantType; Class<?> typeClass = getClass(type); // start walking up the inheritance hierarchy until we hit parentClass while (!genericAncestorType.equals(typeClass)) { if (type instanceof Class) { // there is no useful information for us in raw types, so just // keep going. if (genericAncestorType.isInterface()) { Type intfType = null; for (Type intf : typeClass.getGenericInterfaces()) { if (intf instanceof ParameterizedType && genericAncestorType.equals(((ParameterizedType) intf).getRawType())) { intfType = intf; break; } } if (intfType == null) type = typeClass.getGenericSuperclass(); else type = intfType; } else type = typeClass.getGenericSuperclass(); if (type == null) { if (!typeClass.isInterface()) { LOG.warn("No generic super classes found for child class: " + typeClass + " of parent: " + genericAncestorType); return Collections.emptyList(); } for (Type intf : typeClass.getGenericInterfaces()) { if (intf instanceof ParameterizedType) { type = intf; // TODO try other interfaces if this one fails? break; } } if (type == null) { LOG.warn("No generic ancestors found for child interface: " + typeClass + " of parent: " + genericAncestorType); return Collections.emptyList(); } } // LOG.trace(String.format("Trying generic super of %s: %s", // typeClass.getSimpleName(), type)); } else { final ParameterizedType parameterizedType = (ParameterizedType) type; final Class<?> rawType = (Class<?>) parameterizedType.getRawType(); final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); final TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); for (int i = 0; i < actualTypeArguments.length; i++) { resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); } if (!genericAncestorType.equals(rawType)) { type = rawType.getGenericSuperclass(); // LOG.trace(String.format( // "Trying generic super of child %s: %s", rawType, // type)); } // else // done climbing the hierarchy // LOG.trace("Matched generic " + type + " to ancestor: " // + genericAncestorType); } typeClass = getClass(type); // LOG.trace("Trying generic " + typeClass + " from: " // + Arrays.asList(typeClass.getGenericInterfaces())); } // finally, for each actual type argument provided to baseClass, // determine (if possible) // the raw class for that type argument. final Type[] actualTypeArguments; if (type instanceof Class) { actualTypeArguments = typeClass.getTypeParameters(); } else { actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); } // resolve types by chasing down type variables. final List<Class<?>> parentTypeArguments = new ArrayList<Class<?>>(); for (Type baseType : actualTypeArguments) { while (resolvedTypes.containsKey(baseType)) baseType = resolvedTypes.get(baseType); parentTypeArguments.add(getClass(baseType)); } // LOG.trace(String.format( // "Got child %s's type arguments for %s: %s", // childClass.getName(), parentClass.getSimpleName(), // parentTypeArguments)); return parentTypeArguments; }
From source file:net.jodah.typetools.TypeResolver.java
private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType, Class<?> functionalInterface) { Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType); Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null; if (map == null) { map = new HashMap<TypeVariable<?>, Type>(); // Populate lambdas if (functionalInterface != null) populateLambdaArgs(functionalInterface, targetType, map); // Populate interfaces populateSuperTypeArgs(targetType.getGenericInterfaces(), map, functionalInterface != null); // Populate super classes and interfaces Type genericType = targetType.getGenericSuperclass(); Class<?> type = targetType.getSuperclass(); while (type != null && !Object.class.equals(type)) { if (genericType instanceof ParameterizedType) populateTypeArgs((ParameterizedType) genericType, map, false); populateSuperTypeArgs(type.getGenericInterfaces(), map, false); genericType = type.getGenericSuperclass(); type = type.getSuperclass(); }//from www.java 2 s . com // Populate enclosing classes type = targetType; while (type.isMemberClass()) { genericType = type.getGenericSuperclass(); if (genericType instanceof ParameterizedType) populateTypeArgs((ParameterizedType) genericType, map, functionalInterface != null); type = type.getEnclosingClass(); } if (CACHE_ENABLED) typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map)); } return map; }
From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java
public static Class<?> getGenericClass(Class<?> cls, int i) { try {/*from w w w. j a v a 2 s . c o m*/ ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]); Object genericClass = parameterizedType.getActualTypeArguments()[i]; if (genericClass instanceof ParameterizedType) { // ? return (Class<?>) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { // ? return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType(); } else if (((Class) genericClass).isArray()) { // JDK 7 , Foo<int[]> ?? GenericArrayType return ((Class) genericClass).getComponentType(); } else { return (Class<?>) genericClass; } } catch (Throwable e) { throw new IllegalArgumentException(cls.getName() + " generic type undefined!", e); } }
From source file:com.wantscart.jade.provider.Definition.java
public Definition(Class<?> clazz) { this.clazz = clazz; Type[] genTypes = clazz.getGenericInterfaces(); if (ArrayUtils.isNotEmpty(genTypes)) { Type[] params = ((ParameterizedType) genTypes[0]).getActualTypeArguments(); if (params.length > 0) { this.genericsClazz = (Class<?>) params[0]; } else {//from w w w .j av a 2 s . c o m this.genericsClazz = null; } } else { this.genericsClazz = null; } this.constants = Collections.unmodifiableMap( // NL GenericUtils.getConstantFrom(clazz, true, true)); }