List of usage examples for java.lang.reflect ParameterizedType getOwnerType
Type getOwnerType();
From source file:org.evosuite.utils.generic.GenericClass.java
/** * <p>/*from w ww . j av a 2s .c om*/ * changeClassLoader * </p> * * @param loader * a {@link java.lang.ClassLoader} object. */ public void changeClassLoader(ClassLoader loader) { try { if (rawClass != null) rawClass = getClass(rawClass.getName(), loader); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; // GenericClass rawType = new GenericClass(pt.getRawType()); // rawType.changeClassLoader(loader); GenericClass ownerType = null; if (pt.getOwnerType() != null) { ownerType = new GenericClass(pt.getOwnerType()); // ownerType.type = pt.getOwnerType(); ownerType.changeClassLoader(loader); } List<GenericClass> parameterClasses = new ArrayList<GenericClass>(); for (Type parameterType : pt.getActualTypeArguments()) { GenericClass parameter = new GenericClass(parameterType); // parameter.type = parameterType; parameter.changeClassLoader(loader); parameterClasses.add(parameter); } Type[] parameterTypes = new Type[parameterClasses.size()]; for (int i = 0; i < parameterClasses.size(); i++) parameterTypes[i] = parameterClasses.get(i).getType(); this.type = new ParameterizedTypeImpl(rawClass, parameterTypes, ownerType != null ? ownerType.getType() : null); } else if (type instanceof GenericArrayType) { GenericClass componentClass = getComponentClass(); componentClass.changeClassLoader(loader); this.type = GenericArrayTypeImpl.createArrayType(componentClass.getType()); } else if (type instanceof WildcardType) { Type[] oldUpperBounds = ((WildcardType) type).getUpperBounds(); Type[] oldLowerBounds = ((WildcardType) type).getLowerBounds(); Type[] upperBounds = new Type[oldUpperBounds.length]; Type[] lowerBounds = new Type[oldLowerBounds.length]; for (int i = 0; i < oldUpperBounds.length; i++) { GenericClass bound = new GenericClass(oldUpperBounds[i]); // bound.type = oldUpperBounds[i]; bound.changeClassLoader(loader); upperBounds[i] = bound.getType(); } for (int i = 0; i < oldLowerBounds.length; i++) { GenericClass bound = new GenericClass(oldLowerBounds[i]); // bound.type = oldLowerBounds[i]; bound.changeClassLoader(loader); lowerBounds[i] = bound.getType(); } this.type = new WildcardTypeImpl(upperBounds, lowerBounds); } else if (type instanceof TypeVariable<?>) { for (TypeVariable<?> newVar : rawClass.getTypeParameters()) { if (newVar.getName().equals(((TypeVariable<?>) type).getName())) { this.type = newVar; break; } } } else { this.type = addTypeParameters(rawClass); //GenericTypeReflector.addWildcardParameters(raw_class); } } catch (ClassNotFoundException e) { logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e); } catch (SecurityException e) { logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e); } }
From source file:org.evosuite.utils.generic.GenericClass.java
/** * If this is a LinkedList<?> and the super class is a List<Integer> then * this returns a LinkedList<Integer> * /*from ww w . ja v a 2 s .c o m*/ * @param superClass * @return * @throws ConstructionFailedException */ public GenericClass getWithParametersFromSuperclass(GenericClass superClass) throws ConstructionFailedException { GenericClass exactClass = new GenericClass(type); if (!(type instanceof ParameterizedType)) { exactClass.type = type; return exactClass; } ParameterizedType pType = (ParameterizedType) type; if (superClass.isParameterizedType()) { Map<TypeVariable<?>, Type> typeMap = TypeUtils.determineTypeArguments(rawClass, (ParameterizedType) superClass.getType()); return getGenericInstantiation(typeMap); } Class<?> targetClass = superClass.getRawClass(); Class<?> currentClass = rawClass; Type[] parameterTypes = new Type[superClass.getNumParameters()]; superClass.getParameterTypes().toArray(parameterTypes); if (targetClass.equals(currentClass)) { logger.info("Raw classes match, setting parameters to: " + superClass.getParameterTypes()); exactClass.type = new ParameterizedTypeImpl(currentClass, parameterTypes, pType.getOwnerType()); } else { Type ownerType = pType.getOwnerType(); Map<TypeVariable<?>, Type> superTypeMap = superClass.getTypeVariableMap(); Type[] origArguments = pType.getActualTypeArguments(); Type[] arguments = new Type[origArguments.length]; // For some reason, doing this would lead to arguments being // of component type TypeVariable, which would lead to // ArrayStoreException if we try to assign a WildcardType //Type[] arguments = Arrays.copyOf(origArguments, origArguments.length); for (int i = 0; i < origArguments.length; i++) arguments[i] = origArguments[i]; List<TypeVariable<?>> variables = getTypeVariables(); for (int i = 0; i < arguments.length; i++) { TypeVariable<?> var = variables.get(i); if (superTypeMap.containsKey(var)) { arguments[i] = superTypeMap.get(var); logger.info("Setting type variable " + var + " to " + superTypeMap.get(var)); } else if (arguments[i] instanceof WildcardType && i < parameterTypes.length) { logger.info("Replacing wildcard with " + parameterTypes[i]); logger.info("Lower Bounds: " + Arrays.asList(TypeUtils.getImplicitLowerBounds((WildcardType) arguments[i]))); logger.info("Upper Bounds: " + Arrays.asList(TypeUtils.getImplicitUpperBounds((WildcardType) arguments[i]))); logger.info("Type variable: " + variables.get(i)); if (!TypeUtils.isAssignable(parameterTypes[i], arguments[i])) { logger.info("Not assignable to bounds!"); return null; } else { boolean assignable = false; for (Type bound : variables.get(i).getBounds()) { if (TypeUtils.isAssignable(parameterTypes[i], bound)) { assignable = true; break; } } if (!assignable) { logger.info("Not assignable to type variable!"); return null; } } arguments[i] = parameterTypes[i]; } } GenericClass ownerClass = new GenericClass(ownerType).getWithParametersFromSuperclass(superClass); if (ownerClass == null) return null; exactClass.type = new ParameterizedTypeImpl(currentClass, arguments, ownerClass.getType()); } return exactClass; }
From source file:org.evosuite.utils.generic.GenericClass.java
/** * Serialize, but need to abstract classloader away * //from w ww .ja v a 2 s.c om * @param oos * @throws IOException */ private void writeObject(ObjectOutputStream oos) throws IOException { if (rawClass == null) { oos.writeObject(null); } else { oos.writeObject(rawClass.getName()); if (type instanceof ParameterizedType) { oos.writeObject(Boolean.TRUE); ParameterizedType pt = (ParameterizedType) type; // oos.writeObject(new GenericClass(pt.getRawType())); oos.writeObject(new GenericClass(pt.getOwnerType())); List<GenericClass> parameterClasses = new ArrayList<GenericClass>(); for (Type parameterType : pt.getActualTypeArguments()) { parameterClasses.add(new GenericClass(parameterType)); } oos.writeObject(parameterClasses); } else { oos.writeObject(Boolean.FALSE); } } }
From source file:org.evosuite.utils.generic.GenericUtils.java
public static Type replaceTypeVariablesWithWildcards(Type targetType) { if (targetType instanceof TypeVariable) { TypeVariable<?> typeVariable = (TypeVariable<?>) targetType; return new WildcardTypeImpl(typeVariable.getBounds(), new Type[] {}); } else if (targetType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) targetType; Type owner = null;/* w w w .jav a 2 s . c o m*/ if (parameterizedType.getOwnerType() != null) owner = replaceTypeVariablesWithWildcards(parameterizedType.getOwnerType()); Type[] currentParameters = parameterizedType.getActualTypeArguments(); Type[] parameters = new Type[currentParameters.length]; for (int i = 0; i < parameters.length; i++) { parameters[i] = replaceTypeVariablesWithWildcards(currentParameters[i]); } return new ParameterizedTypeImpl((Class<?>) parameterizedType.getRawType(), parameters, owner); } return targetType; }
From source file:org.evosuite.utils.generic.GenericUtils.java
public static Type replaceTypeVariable(Type targetType, TypeVariable<?> variable, Type variableType) { if (targetType instanceof Class<?>) return targetType; else if (targetType instanceof GenericArrayType) { GenericArrayType gType = (GenericArrayType) targetType; Type componentType = replaceTypeVariable(gType.getGenericComponentType(), variable, variableType); return GenericArrayTypeImpl.createArrayType(componentType); } else if (targetType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) targetType; Type ownerType = null;/*from ww w .j a v a2 s. c om*/ if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), variable, variableType); } Type[] originalParameterTypes = pType.getActualTypeArguments(); Type[] parameterTypes = new Type[originalParameterTypes.length]; for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], variable, variableType); } /* if (variableType instanceof ParameterizedType) { ParameterizedType parameterizedVars = (ParameterizedType) variableType; Map<TypeVariable<?>, Type> subTypes = TypeUtils.getTypeArguments(parameterizedVars); for (Entry<TypeVariable<?>, Type> subTypeEntry : subTypes.entrySet()) { if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), subTypeEntry.getKey(), subTypeEntry.getValue()); } for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], subTypeEntry.getKey(), subTypeEntry.getValue()); } } } */ return new ParameterizedTypeImpl((Class<?>) pType.getRawType(), parameterTypes, ownerType); } else if (targetType instanceof WildcardType) { WildcardType wType = (WildcardType) targetType; Type[] originalUpperBounds = wType.getUpperBounds(); Type[] originalLowerBounds = wType.getLowerBounds(); Type[] upperBounds = new Type[originalUpperBounds.length]; Type[] lowerBounds = new Type[originalLowerBounds.length]; for (int i = 0; i < originalUpperBounds.length; i++) { upperBounds[i] = replaceTypeVariable(originalUpperBounds[i], variable, variableType); } for (int i = 0; i < originalLowerBounds.length; i++) { lowerBounds[i] = replaceTypeVariable(originalLowerBounds[i], variable, variableType); } return new WildcardTypeImpl(upperBounds, lowerBounds); } else if (targetType instanceof TypeVariable<?>) { if (targetType.equals(variable)) { //logger.debug("Do equal: " + variable + "/" + targetType); return variableType; } else { //logger.debug("Do not equal: " + variable + "/" + targetType); //logger.debug("Do not equal: " + variable.getGenericDeclaration() + "/" // + ((TypeVariable<?>) targetType).getGenericDeclaration()); return targetType; } } else { //logger.debug("Unknown type of class " + targetType.getClass() + ": " // + targetType); return targetType; } }
From source file:org.evosuite.utils.generic.GenericUtils.java
/** * TODO: Try to match p2 superclasses? // www . j ava 2 s .co m * * @param p1 Desired TypeVariable assignment * @param p2 Generic type with the TypeVariables that need assignment * @return */ public static Map<TypeVariable<?>, Type> getMatchingTypeParameters(ParameterizedType p1, ParameterizedType p2) { logger.debug("Matching generic types between " + p1 + " and " + p2); Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>(); if (!p1.getRawType().equals(p2.getRawType())) { logger.debug("Raw types do not match!"); GenericClass ownerClass = new GenericClass(p2); if (GenericClass.isSubclass(p1.getRawType(), p2.getRawType())) { logger.debug(p1 + " is a super type of " + p2); Map<TypeVariable<?>, Type> commonsMap = TypeUtils.determineTypeArguments((Class<?>) p2.getRawType(), p1); logger.debug("Adding to map: " + commonsMap); // TODO: Now we would need to iterate over the type parameters, and update the map? //map.putAll(commonsMap); for (TypeVariable<?> t : map.keySet()) { logger.debug(t + ": " + t.getGenericDeclaration()); } // For each type variable of the raw type, map the parameter type to that type Type[] p2TypesA = ((Class<?>) p2.getRawType()).getTypeParameters(); Type[] p2TypesB = p2.getActualTypeArguments(); for (int i = 0; i < p2TypesA.length; i++) { Type a = p2TypesA[i]; Type b = p2TypesB[i]; logger.debug("Should be mapping " + a + " and " + b); if (a instanceof TypeVariable<?>) { logger.debug(a + " is a type variable: " + ((TypeVariable<?>) a).getGenericDeclaration()); if (b instanceof TypeVariable<?>) { logger.debug( b + " is a type variable: " + ((TypeVariable<?>) b).getGenericDeclaration()); if (commonsMap.containsKey((TypeVariable<?>) a) && !(commonsMap.get((TypeVariable<?>) a) instanceof WildcardType) && !(commonsMap.get((TypeVariable<?>) a) instanceof TypeVariable<?>)) map.put((TypeVariable<?>) b, commonsMap.get((TypeVariable<?>) a)); //else // map.put((TypeVariable<?>)a, b); } } // if(b instanceof TypeVariable<?>) { // if(map.containsKey(a)) // map.put((TypeVariable<?>)b, map.get(a)); // //else // // map.put((TypeVariable<?>)b, a); // } logger.debug("Updated map: " + map); } } for (GenericClass interfaceClass : ownerClass.getInterfaces()) { if (interfaceClass.isParameterizedType()) map.putAll(getMatchingTypeParameters(p1, (ParameterizedType) interfaceClass.getType())); else logger.debug("Interface " + interfaceClass + " is not parameterized"); } if (ownerClass.getRawClass().getSuperclass() != null) { GenericClass ownerSuperClass = ownerClass.getSuperClass(); if (ownerSuperClass.isParameterizedType()) map.putAll(getMatchingTypeParameters(p1, (ParameterizedType) ownerSuperClass.getType())); else logger.debug("Super type " + ownerSuperClass + " is not parameterized"); } return map; } for (int i = 0; i < p1.getActualTypeArguments().length; i++) { Type t1 = p1.getActualTypeArguments()[i]; Type t2 = p2.getActualTypeArguments()[i]; if (t1 == t2) continue; logger.debug("First match: " + t1 + " - " + t2); if (t1 instanceof TypeVariable<?>) { map.put((TypeVariable<?>) t1, t2); } if (t2 instanceof TypeVariable<?>) { map.put((TypeVariable<?>) t2, t1); } else if (t2 instanceof ParameterizedType && t1 instanceof ParameterizedType) { map.putAll(getMatchingTypeParameters((ParameterizedType) t1, (ParameterizedType) t2)); } logger.debug("Updated map: " + map); } if (p1.getOwnerType() != null && p1.getOwnerType() instanceof ParameterizedType && p2.getOwnerType() instanceof ParameterizedType) { map.putAll(getMatchingTypeParameters((ParameterizedType) p1.getOwnerType(), (ParameterizedType) p2.getOwnerType())); } return map; }
From source file:org.evosuite.utils.GenericClass.java
/** * <p>// www . j a v a2 s. c o m * changeClassLoader * </p> * * @param loader * a {@link java.lang.ClassLoader} object. */ public void changeClassLoader(ClassLoader loader) { try { if (rawClass != null) rawClass = getClass(rawClass.getName(), loader); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; // GenericClass rawType = new GenericClass(pt.getRawType()); // rawType.changeClassLoader(loader); GenericClass ownerType = null; if (pt.getOwnerType() != null) { ownerType = new GenericClass(pt.getOwnerType()); // ownerType.type = pt.getOwnerType(); ownerType.changeClassLoader(loader); } List<GenericClass> parameterClasses = new ArrayList<GenericClass>(); for (Type parameterType : pt.getActualTypeArguments()) { GenericClass parameter = new GenericClass(parameterType); // parameter.type = parameterType; parameter.changeClassLoader(loader); parameterClasses.add(parameter); } Type[] parameterTypes = new Type[parameterClasses.size()]; for (int i = 0; i < parameterClasses.size(); i++) parameterTypes[i] = parameterClasses.get(i).getType(); this.type = new ParameterizedTypeImpl(rawClass, parameterTypes, ownerType != null ? ownerType.getType() : null); } else if (type instanceof GenericArrayType) { GenericClass componentClass = getComponentClass(); componentClass.changeClassLoader(loader); this.type = GenericArrayTypeImpl.createArrayType(componentClass.getType()); } else if (type instanceof WildcardType) { Type[] oldUpperBounds = ((WildcardType) type).getUpperBounds(); Type[] oldLowerBounds = ((WildcardType) type).getLowerBounds(); Type[] upperBounds = new Type[oldUpperBounds.length]; Type[] lowerBounds = new Type[oldLowerBounds.length]; for (int i = 0; i < oldUpperBounds.length; i++) { GenericClass bound = new GenericClass(oldUpperBounds[i]); // bound.type = oldUpperBounds[i]; bound.changeClassLoader(loader); upperBounds[i] = bound.getType(); } for (int i = 0; i < oldLowerBounds.length; i++) { GenericClass bound = new GenericClass(oldLowerBounds[i]); // bound.type = oldLowerBounds[i]; bound.changeClassLoader(loader); lowerBounds[i] = bound.getType(); } this.type = new WildcardTypeImpl(upperBounds, lowerBounds); } else if (type instanceof TypeVariable<?>) { for (TypeVariable<?> newVar : rawClass.getTypeParameters()) { if (newVar.getName().equals(((TypeVariable<?>) type).getName())) { this.type = newVar; break; } } } else { this.type = addTypeParameters(rawClass); //GenericTypeReflector.addWildcardParameters(raw_class); } } catch (ClassNotFoundException e) { logger.warn("Class not found: {} - keeping old class loader ", rawClass, e); } catch (SecurityException e) { logger.warn("Class not found: {} - keeping old class loader ", rawClass, e); } }
From source file:org.evosuite.utils.GenericClass.java
/** * If this is a LinkedList<?> and the super class is a List<Integer> then * this returns a LinkedList<Integer> * //w w w. j a v a 2 s.c o m * @param superClass * @return * @throws ConstructionFailedException */ public GenericClass getWithParametersFromSuperclass(GenericClass superClass) throws ConstructionFailedException { GenericClass exactClass = new GenericClass(type); if (!(type instanceof ParameterizedType)) { exactClass.type = type; return exactClass; } ParameterizedType pType = (ParameterizedType) type; if (superClass.isParameterizedType()) { Map<TypeVariable<?>, Type> typeMap = TypeUtils.determineTypeArguments(rawClass, (ParameterizedType) superClass.getType()); return getGenericInstantiation(typeMap); } Class<?> targetClass = superClass.getRawClass(); Class<?> currentClass = rawClass; Type[] parameterTypes = new Type[superClass.getNumParameters()]; superClass.getParameterTypes().toArray(parameterTypes); if (targetClass.equals(currentClass)) { logger.info("Raw classes match, setting parameters to: {}", superClass.getParameterTypes()); exactClass.type = new ParameterizedTypeImpl(currentClass, parameterTypes, pType.getOwnerType()); } else { Type ownerType = pType.getOwnerType(); Map<TypeVariable<?>, Type> superTypeMap = superClass.getTypeVariableMap(); Type[] origArguments = pType.getActualTypeArguments(); Type[] arguments = Arrays.copyOf(origArguments, origArguments.length); List<TypeVariable<?>> variables = getTypeVariables(); for (int i = 0; i < arguments.length; i++) { TypeVariable<?> var = variables.get(i); if (superTypeMap.containsKey(var)) { arguments[i] = superTypeMap.get(var); logger.info("Setting type variable {} to {}", var, superTypeMap.get(var)); } else if (arguments[i] instanceof WildcardType && i < parameterTypes.length) { logger.info("Replacing wildcard with {}", parameterTypes[i]); logger.info("Lower Bounds: {}", Arrays.asList(TypeUtils.getImplicitLowerBounds((WildcardType) arguments[i]))); logger.info("Upper Bounds: {}", Arrays.asList(TypeUtils.getImplicitUpperBounds((WildcardType) arguments[i]))); logger.info("Type variable: {}", variables.get(i)); if (!TypeUtils.isAssignable(parameterTypes[i], arguments[i])) { logger.info("Not assignable to bounds!"); return null; } else { boolean assignable = false; for (Type bound : variables.get(i).getBounds()) { if (TypeUtils.isAssignable(parameterTypes[i], bound)) { assignable = true; break; } } if (!assignable) { logger.info("Not assignable to type variable!"); return null; } } arguments[i] = parameterTypes[i]; } } GenericClass ownerClass = new GenericClass(ownerType).getWithParametersFromSuperclass(superClass); if (ownerClass == null) return null; exactClass.type = new ParameterizedTypeImpl(currentClass, arguments, ownerClass.getType()); } return exactClass; }
From source file:org.romaframework.core.schema.SchemaHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Class<?> resolveClassFromType(Type type, ParameterizedType params) { if (type instanceof Class<?>) return (Class<?>) type; if (type instanceof ParameterizedType) { return resolveClassFromType(((ParameterizedType) type).getRawType(), (ParameterizedType) type); }//from w w w . j a v a2s . co m if (type instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) type; Class<?> arrItemp = resolveClassFromType(gat.getGenericComponentType(), null); return Array.newInstance(arrItemp, 0).getClass(); } if (type instanceof TypeVariable<?>) { TypeVariable<?> t = (TypeVariable<?>) type; if (params != null) { Class<?> cl = resolveClassFromType(params.getRawType(), null); if (cl != null) { TypeVariable<Class<?>>[] var = ((Class) cl).getTypeParameters(); int i = 0; for (; i < var.length; i++) { if (var[i].getName().equals(t.getName())) { return resolveClassFromType(params.getActualTypeArguments()[i], resolveParameterizedType(params.getOwnerType())); } } } } Type[] bounds = t.getBounds(); if (bounds.length == 1) return resolveClassFromType(bounds[0], params); } if (type instanceof WildcardType) { // TODO: } return null; }
From source file:org.soybeanMilk.SbmUtils.java
/** * /* ww w . java 2s . c om*/ * @param type * @param variableTypesMap * @return * @date 2012-5-14 */ private static Type reifyInner(Type type, Map<TypeVariable<?>, Type> variableTypesMap) { Type result = null; if (type instanceof Class<?>) { result = type; } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] at = pt.getActualTypeArguments(); Type[] cat = new Type[at.length]; //pt?pt?? boolean reified = true; for (int i = 0; i < at.length; i++) { cat[i] = reifyInner(at[i], variableTypesMap); if (cat[i] != at[i]) reified = false; } if (reified) result = pt; else result = new CustomParameterizedType(pt.getRawType(), pt.getOwnerType(), cat); } else if (type instanceof GenericArrayType) { GenericArrayType gap = (GenericArrayType) type; Type ct = gap.getGenericComponentType(); Type cct = reifyInner(ct, variableTypesMap); if (cct == ct) result = gap; else result = new CustomGenericArrayType(cct); } else if (type instanceof TypeVariable<?>) { TypeVariable<?> tv = (TypeVariable<?>) type; if (variableTypesMap != null) result = variableTypesMap.get(tv); if (result == null) { Type[] bounds = tv.getBounds(); if (bounds == null || bounds.length == 0) result = Object.class; else result = bounds[0]; } result = reifyInner(result, variableTypesMap); } else if (type instanceof WildcardType) { WildcardType wt = (WildcardType) type; Type[] upperBounds = wt.getUpperBounds(); Type upperType = (upperBounds != null && upperBounds.length > 0 ? upperBounds[0] : null); if (upperType == null) upperType = Object.class; result = reifyInner(upperType, variableTypesMap); } else result = type; return result; }