List of usage examples for java.lang.reflect TypeVariable getGenericDeclaration
D getGenericDeclaration();
From source file:GenericsUtil.java
/** * Returns all of the {@link TypeVariables}s implemented * by the given class. If none are implemented then an array * of zero length is returned./* w w w . java 2 s. com*/ * @param clazz the class * @param genericClazz the generic class or interface to return * the TypeVariables from * @return an array of TypeVariable */ public static TypeVariable<?>[] getGenericTypeParameters(Class<?> clazz, Type genericClazz) { List<TypeVariable<?>> vars = new ArrayList<TypeVariable<?>>(); // add superclass for (TypeVariable<?> var : clazz.getSuperclass().getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } // add interfaces for (Class<?> iface : clazz.getInterfaces()) { for (TypeVariable<?> var : iface.getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } } // return list return vars.toArray(new TypeVariable<?>[0]); }
From source file:Main.java
/** * Searches for the typevariable definition in the inClass hierarchy. * /* w ww . ja va 2 s . co m*/ * @param type * @param inClass * @return the resolved type or type if unable to resolve it. */ public final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass) { return resolveTypeVariable(type, genericDeclarationToClass(type.getGenericDeclaration()), inClass); }
From source file:com.autentia.common.util.ClassWithList.java
private static void print(TypeVariable v) { System.out.println("Type variable"); System.out.println("Name: " + v.getName()); System.out.println("Declaration: " + v.getGenericDeclaration()); System.out.println("Bounds:"); for (Type t : v.getBounds()) { print(t);/* w ww . j a v a2 s. com*/ } }
From source file:Main.java
final static Type expand(Type type, Class<?> inClass) { Type expandedType = null;/*w ww. j a v a 2s . c o m*/ if (type instanceof TypeVariable) { @SuppressWarnings("unchecked") // for the moment we assume it is a class, we can later handle ctr and methods TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type; if (inClass == null) inClass = genericDeclarationToClass(tvType.getGenericDeclaration()); expandedType = resolveTypeVariable(tvType, inClass); if (type.equals(expandedType)) expandedType = tvType.getBounds()[0]; } else if (type instanceof WildcardType) { WildcardType wType = (WildcardType) type; expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass) : Object.class; } else return type; return expandedType == null || type.equals(expandedType) ? Object.class : expandedType; }
From source file:eu.stratosphere.api.java.typeutils.TypeExtractor.java
private static TypeInformation<?> findCorrespondingInfo(TypeVariable<?> typeVar, Type type, TypeInformation<?> corrInfo) { if (type instanceof TypeVariable) { TypeVariable<?> variable = (TypeVariable<?>) type; if (variable.getName().equals(typeVar.getName()) && variable.getGenericDeclaration().equals(typeVar.getGenericDeclaration())) { return corrInfo; }/*w ww. ja va 2s . com*/ } else if (type instanceof ParameterizedType && Tuple.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())) { ParameterizedType tuple = (ParameterizedType) type; Type[] args = tuple.getActualTypeArguments(); for (int i = 0; i < args.length; i++) { TypeInformation<?> info = findCorrespondingInfo(typeVar, args[i], ((TupleTypeInfo<?>) corrInfo).getTypeAt(i)); if (info != null) { return info; } } } return null; }
From source file:jp.terasoluna.fw.util.GenericsUtil.java
/** * ??<code>Type</code>??// ww w . j a v a 2 s. com * @param type ????<code>Type</code> * @param ancestorTypeList <code>type</code>??? ??????<code>ParameterizedType</code>? * @return ? * @throws IllegalStateException <code>type</code>? <code>Class</code>???? <code>TypeVariable</code>????? * <code>type</code>?? ????????? <code>type</code>???<code>Class</code>???? * (??)? */ protected static Class<? extends Object> resolveTypeVariable(Type type, List<ParameterizedType> ancestorTypeList) throws IllegalStateException { if (isNotTypeVariable(type)) { return getRawClass(type); } // TypeVariable:? TypeVariable<?> targetType = (TypeVariable<?>) type; Type actualType = null; for (int i = ancestorTypeList.size() - 1; i >= 0; i--) { ParameterizedType ancestorType = ancestorTypeList.get(i); // ????? GenericDeclaration declaration = targetType.getGenericDeclaration(); if (!(declaration instanceof Class)) { throw new IllegalStateException("TypeVariable(" + targetType.getName() + " is not declared at Class " + "(ie. is declared at Method or Constructor)"); } // ?Generics???????? Class<?> declaredClass = (Class<?>) declaration; if (declaredClass != ancestorType.getRawType()) { continue; } // ???????? // ConcreteAbstractBLogic<R,P> extends AbstractBLogic<P,R> // ????????type???? Type[] parameterTypes = declaredClass.getTypeParameters(); int index = ArrayUtils.indexOf(parameterTypes, targetType); if (index == -1) { // ??????????????? // ?????????? // ???Generics?API????????????? // ????????? throw new IllegalStateException("Class(" + declaredClass.getName() + ") does not declare TypeValidable(" + targetType.getName() + ")"); } actualType = ancestorType.getActualTypeArguments()[index]; if (log.isDebugEnabled()) { log.debug("resolved " + targetType.getName() + " -> " + actualType); } if (isNotTypeVariable(actualType)) { return getRawClass(actualType); } targetType = (TypeVariable<?>) actualType; } throw new IllegalStateException( "Concrete type of Type(" + type + ") was not found in ancestorList(" + ancestorTypeList + ")"); }
From source file:eu.stratosphere.api.java.typeutils.TypeExtractor.java
private static Type materializeTypeVariable(ArrayList<Type> typeHierarchy, TypeVariable<?> typeVar) { TypeVariable<?> inTypeTypeVar = typeVar; // iterate thru hierarchy from top to bottom until type variable gets a class assigned for (int i = typeHierarchy.size() - 1; i >= 0; i--) { Type curT = typeHierarchy.get(i); // parameterized type if (curT instanceof ParameterizedType) { Class<?> rawType = ((Class<?>) ((ParameterizedType) curT).getRawType()); for (int paramIndex = 0; paramIndex < rawType.getTypeParameters().length; paramIndex++) { TypeVariable<?> curVarOfCurT = rawType.getTypeParameters()[paramIndex]; // check if variable names match if (curVarOfCurT.getName().equals(inTypeTypeVar.getName()) && curVarOfCurT.getGenericDeclaration().equals(inTypeTypeVar.getGenericDeclaration())) { Type curVarType = ((ParameterizedType) curT).getActualTypeArguments()[paramIndex]; // another type variable level if (curVarType instanceof TypeVariable<?>) { inTypeTypeVar = (TypeVariable<?>) curVarType; }// w w w. j a v a 2s . c o m // class else { return curVarType; } } } } } // can not be materialized, most likely due to type erasure return null; }
From source file:com.autentia.common.util.ClassWithList.java
private <T> void printInfo(List<T> list) throws SecurityException, NoSuchMethodException { final TypeVariable<?>[] types = list.getClass().getTypeParameters(); for (TypeVariable<?> clazz : types) { print(clazz);//from w w w . j a v a 2s . c om // ParameterizedType pt = (ParameterizedType)clazz; // log.debug(clazz.getName() + " | " + clazz.getGenericDeclaration().getClass().getName()); // for (TypeVariable<?> type : clazz.getGenericDeclaration().getTypeParameters()) { ParameterizedType pt = (ParameterizedType) type; log.debug(type.getClass().getName()); } // // for (Type type : clazz.getBounds()) { // ParameterizedType pt = (ParameterizedType)type; // print(pt); // log.debug(pt.getOwnerType()); // log.debug(pt.getRawType()); // log.debug(pt.getActualTypeArguments()); // } } }
From source file:net.firejack.platform.core.utils.Factory.java
public int getParameterTypeDeclarationIndex(TypeVariable typeVariable) { GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration(); TypeVariable[] typeVariables = genericDeclaration.getTypeParameters(); for (int i = 0; i < typeVariables.length; i++) { if (typeVariables[i].equals(typeVariable)) { return i; }// ww w. ja v a 2 s . co m } throw new IllegalStateException( "Argument " + typeVariable.toString() + " is not found in " + genericDeclaration.toString() + "."); }
From source file:net.firejack.platform.core.utils.Factory.java
private FieldInfo getField(Class<?> clazz, Field field) { FieldInfo info = null;/*from ww w .j a va2s .com*/ if (field != null) { info = new FieldInfo(field); if (field.getGenericType() instanceof TypeVariable) { TypeVariable genericType = (TypeVariable) field.getGenericType(); TypeVariable<?>[] parameters = genericType.getGenericDeclaration().getTypeParameters(); for (int i = 0; i < parameters.length; i++) { if (parameters[i].getName().equals(genericType.getName())) { Class parameterClass = getGenericParameterClass(clazz, (Class) genericType.getGenericDeclaration(), i); info.setType(parameterClass); break; } } } if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType genericType = (ParameterizedType) field.getGenericType(); Class<?> arrayType = null; if (genericType.getActualTypeArguments()[0] instanceof TypeVariable) { TypeVariable genericType1 = (TypeVariable) genericType.getActualTypeArguments()[0]; TypeVariable<?>[] parameters = genericType1.getGenericDeclaration().getTypeParameters(); for (int i = 0; i < parameters.length; i++) { if (parameters[i].getName().equals(genericType1.getName())) { arrayType = getGenericParameterClass(clazz, (Class) genericType1.getGenericDeclaration(), i); break; } } } else { arrayType = (Class<?>) genericType.getActualTypeArguments()[0]; } info.setGenericType(arrayType); } } return info; }