List of usage examples for java.lang.reflect ParameterizedType getOwnerType
Type getOwnerType();
From source file:Main.java
public static void main(String args[]) throws Exception { Type type = StringList.class.getGenericSuperclass(); System.out.println(type);/*from ww w . j a v a 2 s . c o m*/ ParameterizedType pt = (ParameterizedType) type; Type ownerType = pt.getOwnerType(); System.out.println(ownerType); }
From source file:com.autentia.common.util.ClassWithList.java
private static void print(ParameterizedType pt) { System.out.println("Parameterized type"); System.out.println("Owner: " + pt.getOwnerType()); System.out.println("Raw type: " + pt.getRawType()); for (Type actualType : pt.getActualTypeArguments()) { print(actualType);//from w w w .j a va2s . co m } }
From source file:net.jodah.typetools.TypeResolver.java
/** * Populates the {@code map} with variable/argument pairs for the given {@code type}. */// ww w .jav a 2 s.co m private static void populateTypeArgs(ParameterizedType type, Map<TypeVariable<?>, Type> map, boolean depthFirst) { if (type.getRawType() instanceof Class) { TypeVariable<?>[] typeVariables = ((Class<?>) type.getRawType()).getTypeParameters(); Type[] typeArguments = type.getActualTypeArguments(); if (type.getOwnerType() != null) { Type owner = type.getOwnerType(); if (owner instanceof ParameterizedType) populateTypeArgs((ParameterizedType) owner, map, depthFirst); } for (int i = 0; i < typeArguments.length; i++) { TypeVariable<?> variable = typeVariables[i]; Type typeArgument = typeArguments[i]; if (typeArgument instanceof Class) { map.put(variable, typeArgument); } else if (typeArgument instanceof GenericArrayType) { map.put(variable, typeArgument); } else if (typeArgument instanceof ParameterizedType) { map.put(variable, typeArgument); } else if (typeArgument instanceof TypeVariable) { TypeVariable<?> typeVariableArgument = (TypeVariable<?>) typeArgument; if (depthFirst) { Type existingType = map.get(variable); if (existingType != null) { map.put(typeVariableArgument, existingType); continue; } } Type resolvedType = map.get(typeVariableArgument); if (resolvedType == null) resolvedType = resolveBound(typeVariableArgument); map.put(variable, resolvedType); } } } }
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Initializes a TypeDefinition from a given class. The first entry in the return list is the TypeDefinition for the * parameter class; any entries after that (if any) are TypeDefinitions for any other types that were required as * fields for that root TypeDefinition.// w w w . ja va 2 s .c o m * * @param klass The Class object to describe. * @param typeState The TypeState for the current operation. * @param strict True indicates that processing should stop on ambiguous entries; false indicates that null should * be entered. * @return A list of TypeDefinitions; the first entry is the root (corresponding with the klass parameter), any * other entries in the list were required to describe the root TypeDefinition's fields. The return may also * be null, if sufficient information was not provided to determine the type. */ public static TypeDefinition getTypeDefinition(Type type, TypeState typeState, boolean strict) { Class<?> klass; // we already know about this type; we're done if (typeState.isTypeKnown(ReflectTypeUtils.getTypeName(type))) { return typeState.getType(ReflectTypeUtils.getTypeName(type)); } // if the type is Object, return null, we can't figure out anything more if (type instanceof Class && Object.class.equals(type)) { return null; } // if we don't have enough information, return null if (!strict) { if (type instanceof Class && Map.class.isAssignableFrom((Class<?>) type) && !Properties.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } else if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) { if (!JSON.class.isAssignableFrom((Class<?>) type)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(type)); } return null; } } TypeDefinition ret; if (type instanceof Class && Properties.class.isAssignableFrom((Class<?>) type)) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); klass = (Class<?>) type; mtdret.setKlass(klass); TypeDefinition stringType = getTypeDefinition(String.class, typeState, false); mtdret.setKeyFieldDefinition(new GenericFieldDefinition(stringType)); mtdret.setValueFieldDefinition(new GenericFieldDefinition(stringType)); ret = mtdret; } else if (type instanceof Class && JSONUtils.isPrimitive((Class<?>) type)) { PrimitiveReflectTypeDefinition ptret; if (((Class<?>) type).isEnum()) { ptret = new EnumPrimitiveReflectTypeDefinition(); } else { ptret = new PrimitiveReflectTypeDefinition(); } ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); klass = (Class<?>) type; ptret.setKlass(klass); ret = ptret; } else if (type instanceof Class) { klass = (Class<?>) type; if (Collection.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (klass.isArray()) { throw new WMRuntimeException(MessageResource.JSON_USE_FIELD_FOR_ARRAY, klass); } else if (Map.class.isAssignableFrom(klass)) { throw new WMRuntimeException(MessageResource.JSON_TYPE_NOGENERICS, klass); } else if (ClassUtils.isPrimitiveOrWrapper(klass) || CharSequence.class.isAssignableFrom(klass)) { PrimitiveReflectTypeDefinition ptret = new PrimitiveReflectTypeDefinition(); ptret.setTypeName(ReflectTypeUtils.getTypeName(type)); ptret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(ptret); ptret.setKlass(klass); ret = ptret; } else { ObjectReflectTypeDefinition otret = new ObjectReflectTypeDefinition(); otret.setTypeName(ReflectTypeUtils.getTypeName(type)); otret.setShortName(ReflectTypeUtils.getShortName(type)); otret.setKlass(klass); typeState.addType(otret); PropertyUtilsBean pub = ((ReflectTypeState) typeState).getPropertyUtilsBean(); PropertyDescriptor[] pds = pub.getPropertyDescriptors(klass); otret.setFields(new LinkedHashMap<String, FieldDefinition>(pds.length)); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } Type paramType; if (pd.getReadMethod() != null) { paramType = pd.getReadMethod().getGenericReturnType(); } else if (pd.getWriteMethod() != null) { paramType = pd.getWriteMethod().getGenericParameterTypes()[0]; } else { logger.warn("No getter in type " + pd.getName()); continue; } otret.getFields().put(pd.getName(), getFieldDefinition(paramType, typeState, strict, pd.getName())); } ret = otret; } } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (pt.getRawType() instanceof Class && Map.class.isAssignableFrom((Class<?>) pt.getRawType())) { MapReflectTypeDefinition mtdret = new MapReflectTypeDefinition(); mtdret.setTypeName(ReflectTypeUtils.getTypeName(type)); mtdret.setShortName(ReflectTypeUtils.getShortName(type)); typeState.addType(mtdret); Type[] types = pt.getActualTypeArguments(); mtdret.setKeyFieldDefinition(getFieldDefinition(types[0], typeState, strict, null)); mtdret.setValueFieldDefinition(getFieldDefinition(types[1], typeState, strict, null)); mtdret.setKlass((Class<?>) pt.getRawType()); ret = mtdret; } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE, pt.getOwnerType(), pt); } } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type, type != null ? type.getClass() : null); } return ret; }
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Returns the FieldDefinition for a field of the specified type. * /*from w w w. j a va 2 s . c om*/ * @param type * @param typeState * @param strict True if strict mode is on; not enough information will result in exceptions instead of warnings. * @param The name of this field (if known) * @return The corresponding fieldDefinition to the type. */ public static FieldDefinition getFieldDefinition(Type type, TypeState typeState, boolean strict, String name) { GenericFieldDefinition ret = new GenericFieldDefinition(); ret.setName(name); if (type == null) { // do nothing, it's null, but do return a FieldDefinition } else if (type instanceof Class) { Class<?> returnTypeClass = (Class<?>) type; if (returnTypeClass.isArray()) { Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions( returnTypeClass, typeState, strict); ret.setTypeDefinition(dimAndClass.v1); ret.setArrayTypes(dimAndClass.v2); } else if (!strict && Collection.class.isAssignableFrom(returnTypeClass)) { if (!JSON.class.isAssignableFrom(returnTypeClass)) { logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(returnTypeClass)); } ret.setArrayTypes(new ArrayList<ListTypeDefinition>(1)); ret.getArrayTypes().add(getListTypeDefinition(returnTypeClass, typeState, strict)); } else if (ClassUtils.isPrimitiveOrWrapper(returnTypeClass)) { TypeDefinition td = getTypeDefinition(returnTypeClass, typeState, strict); ret.setTypeDefinition(td); } else { TypeDefinition td = getTypeDefinition(returnTypeClass, typeState, strict); ret.setTypeDefinition(td); } } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (Class.class == pt.getRawType()) { TypeDefinition td = getTypeDefinition(Class.class, typeState, strict); ret.setTypeDefinition(td); } else if (pt.getRawType() instanceof Class && Collection.class.isAssignableFrom((Class<?>) pt.getRawType())) { Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions(pt, typeState, strict); ret.setTypeDefinition(dimAndClass.v1); ret.setArrayTypes(dimAndClass.v2); } else if (pt.getRawType() instanceof Class && Map.class.isAssignableFrom((Class<?>) pt.getRawType())) { TypeDefinition td = getTypeDefinition(pt, typeState, strict); ret.setTypeDefinition(td); } else { if (strict) { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE, pt.getOwnerType(), pt); } else { logger.warn(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE.getMessage(pt.getOwnerType(), pt)); } } } else if (type instanceof GenericArrayType) { Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions(type, typeState, strict); ret.setTypeDefinition(dimAndClass.v1); ret.setArrayTypes(dimAndClass.v2); } else { throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type, type != null ? type.getClass() : null); } return ret; }
From source file:com.clark.func.Functions.java
/** * <p>//from ww w .j a v a2s. co m * </p> * * @param cls * @param parameterizedType * @param typeVarAssigns */ private static <T> void mapTypeVariablesToArguments(Class<T> cls, ParameterizedType parameterizedType, Map<TypeVariable<?>, Type> typeVarAssigns) { // capture the type variables from the owner type that have assignments Type ownerType = parameterizedType.getOwnerType(); if (ownerType instanceof ParameterizedType) { // recursion to make sure the owner's owner type gets processed mapTypeVariablesToArguments(cls, (ParameterizedType) ownerType, typeVarAssigns); } // parameterizedType is a generic interface/class (or it's in the owner // hierarchy of said interface/class) implemented/extended by the class // cls. Find out which type variables of cls are type arguments of // parameterizedType: Type[] typeArgs = parameterizedType.getActualTypeArguments(); // of the cls's type variables that are arguments of parameterizedType, // find out which ones can be determined from the super type's arguments TypeVariable<?>[] typeVars = getRawType(parameterizedType).getTypeParameters(); // use List view of type parameters of cls so the contains() method can // be used: List<TypeVariable<Class<T>>> typeVarList = Arrays.asList(cls.getTypeParameters()); for (int i = 0; i < typeArgs.length; i++) { TypeVariable<?> typeVar = typeVars[i]; Type typeArg = typeArgs[i]; // argument of parameterizedType is a type variable of cls if (typeVarList.contains(typeArg) // type variable of parameterizedType has an assignment in // the super type. && typeVarAssigns.containsKey(typeVar)) { // map the assignment to the cls's type variable typeVarAssigns.put((TypeVariable<?>) typeArg, typeVarAssigns.get(typeVar)); } } }
From source file:com.clark.func.Functions.java
/** * <p>//from w ww. j ava 2s.c o m * </p> * * @param parameterizedType * @param toClass * @param subtypeVarAssigns * @return */ private static Map<TypeVariable<?>, Type> getTypeArguments(ParameterizedType parameterizedType, Class<?> toClass, Map<TypeVariable<?>, Type> subtypeVarAssigns) { Class<?> cls = getRawType(parameterizedType); // make sure they're assignable if (!isAssignable(cls, toClass)) { return null; } Type ownerType = parameterizedType.getOwnerType(); Map<TypeVariable<?>, Type> typeVarAssigns; if (ownerType instanceof ParameterizedType) { // get the owner type arguments first ParameterizedType parameterizedOwnerType = (ParameterizedType) ownerType; typeVarAssigns = getTypeArguments(parameterizedOwnerType, getRawType(parameterizedOwnerType), subtypeVarAssigns); } else { // no owner, prep the type variable assignments map typeVarAssigns = subtypeVarAssigns == null ? new HashMap<TypeVariable<?>, Type>() : new HashMap<TypeVariable<?>, Type>(subtypeVarAssigns); } // get the subject parameterized type's arguments Type[] typeArgs = parameterizedType.getActualTypeArguments(); // and get the corresponding type variables from the raw class TypeVariable<?>[] typeParams = cls.getTypeParameters(); // map the arguments to their respective type variables for (int i = 0; i < typeParams.length; i++) { Type typeArg = typeArgs[i]; typeVarAssigns.put(typeParams[i], typeVarAssigns.containsKey(typeArg) ? typeVarAssigns.get(typeArg) : typeArg); } if (toClass.equals(cls)) { // target class has been reached. Done. return typeVarAssigns; } // walk the inheritance hierarchy until the target class is reached return getTypeArguments(getClosestParentType(cls, toClass), toClass, typeVarAssigns); }
From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java
private Type resolveParameterizedType(ParameterizedType beanType, ParameterizedType parameterizedType) { Type rawType = parameterizedType.getRawType(); Type[] actualTypes = parameterizedType.getActualTypeArguments(); Type resolvedRawType = resolveType(beanType, beanType, rawType); Type[] resolvedActualTypes = new Type[actualTypes.length]; for (int i = 0; i < actualTypes.length; i++) { resolvedActualTypes[i] = resolveType(beanType, beanType, actualTypes[i]); }//from www.j a v a2 s . c om // reconstruct ParameterizedType by types resolved TypeVariable. return TypeUtils.parameterizeWithOwner(parameterizedType.getOwnerType(), resolvedRawType.getClass(), resolvedActualTypes); }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(java.lang.reflect.Type t) { if (t instanceof Class<?>) { return canUse((Class<?>) t); } else if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; for (java.lang.reflect.Type parameterType : pt.getActualTypeArguments()) { if (!canUse(parameterType)) return false; }/*from w w w .ja va2s . c o m*/ if (!canUse(pt.getOwnerType())) { return false; } } // If it's not declared, let's assume it's ok return true; }
From source file:org.evosuite.utils.generic.GenericAccessibleObject.java
/** * Maps type parameters in a type to their values. * /*w w w . j a v a2s . 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); } }