List of usage examples for java.lang.reflect WildcardType getUpperBounds
Type[] getUpperBounds();
From source file:org.datalorax.populace.core.util.TypeResolver.java
private Type resolveWildcard(final WildcardType type, final TypeToken<?> assigningType) { final Type[] lowerBounds = type.getLowerBounds(); if (lowerBounds.length != 0) { final Type[] resolvedLowerBounds = resolve(lowerBounds, assigningType); if (Arrays.equals(resolvedLowerBounds, lowerBounds)) { return type; }/*from w w w. ja v a2 s.com*/ return TypeUtils.wildcardTypeWithLowerBounds(resolvedLowerBounds); } final Type[] resolvedUpperBounds = resolve(type.getUpperBounds(), assigningType); if (Arrays.equals(resolvedUpperBounds, type.getUpperBounds())) { return type; } return TypeUtils.wildcardTypeWithUpperBounds(resolvedUpperBounds); }
From source file:org.datalorax.populace.core.util.TypeUtils.java
private static Type ensureConsistentWildcard(final WildcardType type) { final Type[] lowerBounds = Arrays.stream(type.getLowerBounds()).map(TypeUtils::ensureConsistentType) .toArray(Type[]::new); final Type[] upperBounds = Arrays.stream(type.getUpperBounds()).map(TypeUtils::ensureConsistentType) .toArray(Type[]::new); return org.apache.commons.lang3.reflect.TypeUtils.wildcardType().withLowerBounds(lowerBounds) .withUpperBounds(upperBounds).build(); }
From source file:org.diorite.cfg.system.ConfigField.java
public static void getAllPossibleTypes(final Set<Class<?>> classes, final Set<Type> checkedTypes, final Type rawType) { if (!checkedTypes.add(rawType)) { return;/*from ww w.j av a 2s .co m*/ } if (rawType instanceof Class) { classes.add((Class<?>) rawType); } if (rawType instanceof WildcardType) { final WildcardType type = (WildcardType) rawType; for (final Type t : type.getLowerBounds()) { getAllPossibleTypes(classes, checkedTypes, t); } for (final Type t : type.getUpperBounds()) { getAllPossibleTypes(classes, checkedTypes, t); } } if (rawType instanceof GenericArrayType) { getAllPossibleTypes(classes, checkedTypes, ((GenericArrayType) rawType).getGenericComponentType()); } if (rawType instanceof TypeVariable) { final TypeVariable<?> type = (TypeVariable<?>) rawType; for (final Type t : type.getBounds()) { getAllPossibleTypes(classes, checkedTypes, t); } } if (rawType instanceof ParameterizedType) { final ParameterizedType type = (ParameterizedType) rawType; getAllPossibleTypes(classes, checkedTypes, type.getRawType()); getAllPossibleTypes(classes, checkedTypes, type.getOwnerType()); for (final Type t : type.getActualTypeArguments()) { getAllPossibleTypes(classes, checkedTypes, t); } } }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * @param runtime/*from w ww .j av a2 s.c o m*/ * is <code>true</code> if we need name for class loading, <code>false</code> if we need * name for source generation. * * @return the fully qualified name of given {@link Type}. */ public static String getFullyQualifiedName(Type type, boolean runtime) { Assert.isNotNull(type); // Class if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; // array if (clazz.isArray()) { return getFullyQualifiedName(clazz.getComponentType(), runtime) + "[]"; } // object String name = clazz.getName(); if (!runtime) { name = name.replace('$', '.'); } return name; } // GenericArrayType if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; return getFullyQualifiedName(genericArrayType.getGenericComponentType(), runtime) + "[]"; } // ParameterizedType if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); // raw type StringBuilder sb = new StringBuilder(); sb.append(getFullyQualifiedName(rawType, runtime)); // type arguments sb.append("<"); boolean firstTypeArgument = true; for (Type typeArgument : parameterizedType.getActualTypeArguments()) { if (!firstTypeArgument) { sb.append(","); } firstTypeArgument = false; sb.append(getFullyQualifiedName(typeArgument, runtime)); } sb.append(">"); // done return sb.toString(); } // WildcardType if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; return "? extends " + getFullyQualifiedName(wildcardType.getUpperBounds()[0], runtime); } // TypeVariable TypeVariable<?> typeVariable = (TypeVariable<?>) type; return typeVariable.getName(); }
From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java
private static IGenericType resolveType(Type type) { if (type instanceof Class<?>) { return new ClassGenericType((Class<?>) type, null, null); }/*w w w .j a v a2s. com*/ if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; if (ArrayUtils.isEmpty(wildcardType.getUpperBounds()) && ArrayUtils.isEmpty(wildcardType.getLowerBounds())) { return ClassGenericType.WILDCARD; } } if (type instanceof ParameterizedType || type instanceof GenericArrayType || type instanceof WildcardType) { return new ClassGenericType(null, resolveTypeName(type), "???"); } if (type instanceof TypeVariable<?>) { return ClassGenericType.WILDCARD; } Assert.fail(MessageFormat.format("Undefine type: {0}", type)); return null; }
From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java
private static String resolveTypeName(Type type) { if (type instanceof Class<?>) { Class<?> rawType = (Class<?>) type; return convertPrimitiveType(ReflectionUtils.getFullyQualifiedName(rawType, false)); }/* w w w .j a va 2s .c om*/ if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> rawType = (Class<?>) parameterizedType.getRawType(); StringBuffer fullName = new StringBuffer(); fullName.append(CoreUtils.getClassName(rawType)); fullName.append("<"); Type[] types = parameterizedType.getActualTypeArguments(); for (int i = 0; i < types.length; i++) { if (i > 0) { fullName.append(", "); } fullName.append(resolveTypeName(types[i])); } fullName.append(">"); return fullName.toString(); } if (type instanceof GenericArrayType) { StringBuffer fullName = new StringBuffer(); Type elementType = null; GenericArrayType arrayType = (GenericArrayType) type; while (true) { fullName.append("[]"); elementType = arrayType.getGenericComponentType(); if (elementType instanceof GenericArrayType) { arrayType = (GenericArrayType) elementType; continue; } break; } fullName.insert(0, resolveTypeName(elementType)); return fullName.toString(); } if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; Type[] upperBounds = wildcardType.getUpperBounds(); Type[] lowerBounds = wildcardType.getLowerBounds(); if (!ArrayUtils.isEmpty(upperBounds)) { Type upperBound = upperBounds[0]; boolean isWildcard = upperBound instanceof Class<?> && ((Class<?>) upperBound).getName().equals("java.lang.Object"); if (!isWildcard) { return "? extends " + resolveTypeName(upperBound); } } else if (!ArrayUtils.isEmpty(lowerBounds)) { return "? super " + resolveTypeName(lowerBounds[0]); } return "?"; } if (type instanceof TypeVariable<?>) { return "?"; } Assert.fail("Undefine type: " + type); return null; }
From source file:org.evosuite.utils.generic.GenericClass.java
/** * Determine whether the upper and lower boundaries are satisfied by this * class/*from w w w . j av a2s . c o m*/ * * @param wildcardType * @return */ public boolean satisfiesBoundaries(WildcardType wildcardType, Map<TypeVariable<?>, Type> typeMap) { boolean isAssignable = true; Map<TypeVariable<?>, Type> ownerVariableMap = getTypeVariableMap(); ownerVariableMap.putAll(typeMap); // ? extends X for (Type theType : wildcardType.getUpperBounds()) { logger.debug("Checking upper bound " + theType); // Special case: Enum is defined as Enum<T extends Enum> if (GenericTypeReflector.erase(theType).equals(Enum.class)) { // if this is an enum then it's ok. if (isEnum()) continue; else { // If it's not an enum, it cannot be assignable to enum! isAssignable = false; break; } } Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap); //logger.debug("Bound after variable replacement: " + type); if (!isAssignableTo(type)) { // If the boundary is not assignable it may still be possible // to instantiate the generic to an assignable type if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) { Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass()); if (instanceType == null) { // This happens when the raw class is not a supertype // of the boundary isAssignable = false; break; } if (GenericClass.isAssignable(type, instanceType)) { logger.debug("Found assignable generic exact type: " + instanceType); continue; } } isAssignable = false; break; } } // ? super X Type[] lowerBounds = wildcardType.getLowerBounds(); if (lowerBounds != null && lowerBounds.length > 0) { for (Type theType : wildcardType.getLowerBounds()) { logger.debug("Checking lower bound " + theType); Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap); logger.debug("Bound after variable replacement: " + type); logger.debug("Is assignable from " + toString() + "?"); if (!isAssignableFrom(type)) { logger.debug("Not assignable from " + toString()); // If the boundary is not assignable it may still be possible // to instantiate the generic to an assignable type if (type instanceof WildcardType) continue; if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) { Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass()); if (instanceType == null) { // This happens when the raw class is not a supertype // of the boundary isAssignable = false; break; } if (GenericClass.isAssignable(type, instanceType)) { logger.debug("Found assignable generic exact type: " + instanceType); continue; } } isAssignable = false; break; } else { logger.debug("Is assignable from " + toString()); } } } return isAssignable; }
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 w ww .j av a 2 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.GenericClass.java
/** * Determine whether the upper and lower boundaries are satisfied by this * class/*from w w w . j a v a 2 s. c o m*/ * * @param wildcardType * @return */ public boolean satisfiesBoundaries(WildcardType wildcardType, Map<TypeVariable<?>, Type> typeMap) { boolean isAssignable = true; Map<TypeVariable<?>, Type> ownerVariableMap = getTypeVariableMap(); ownerVariableMap.putAll(typeMap); // ? extends X for (Type theType : wildcardType.getUpperBounds()) { // Special case: Enum is defined as Enum<T extends Enum> if (GenericTypeReflector.erase(theType).equals(Enum.class)) { // if this is an enum then it's ok. if (isEnum()) continue; else { // If it's not an enum, it cannot be assignable to enum! isAssignable = false; break; } } Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap); //logger.debug("Bound after variable replacement: " + type); if (!isAssignableTo(type)) { // If the boundary is not assignable it may still be possible // to instantiate the generic to an assignable type if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) { Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass()); if (instanceType == null) { // This happens when the raw class is not a supertype // of the boundary isAssignable = false; break; } if (GenericClass.isAssignable(type, instanceType)) { logger.debug("Found assignable generic exact type: {}", instanceType); continue; } } isAssignable = false; break; } } // ? super X Type[] lowerBounds = wildcardType.getLowerBounds(); if (lowerBounds != null && lowerBounds.length > 0) { for (Type theType : wildcardType.getLowerBounds()) { Type type = GenericUtils.replaceTypeVariables(theType, ownerVariableMap); logger.debug("Bound after variable replacement: {}", type); if (!isAssignableTo(type)) { // If the boundary is not assignable it may still be possible // to instantiate the generic to an assignable type if (GenericTypeReflector.erase(type).isAssignableFrom(getRawClass())) { Type instanceType = GenericTypeReflector.getExactSuperType(type, getRawClass()); if (instanceType == null) { // This happens when the raw class is not a supertype // of the boundary isAssignable = false; break; } if (GenericClass.isAssignable(type, instanceType)) { logger.debug("Found assignable generic exact type: {}", instanceType); continue; } } isAssignable = false; break; } } } return isAssignable; }
From source file:org.jiemamy.utils.reflect.GenericUtil.java
/** * {@code type}???/* w w w .ja v a 2 s . co m*/ * <ul> * <li>{@code type}?{@code Class}?????????</li> * <li>{@code type}??????????</li> * <li>{@code type}????(??)??</li> * <li>{@code type}??????????????</li> * <li>??????{@code null}?</li> * </ul> * * @param type * @return {@code type}? * @throws IllegalArgumentException ?{@code null}??? */ public static Class<?> getRawClass(Type type) { Validate.notNull(type); if (Class.class.isInstance(type)) { return Class.class.cast(type); } if (ParameterizedType.class.isInstance(type)) { ParameterizedType parameterizedType = ParameterizedType.class.cast(type); return getRawClass(parameterizedType.getRawType()); } if (WildcardType.class.isInstance(type)) { WildcardType wildcardType = WildcardType.class.cast(type); Type[] types = wildcardType.getUpperBounds(); return getRawClass(types[0]); } if (GenericArrayType.class.isInstance(type)) { GenericArrayType genericArrayType = GenericArrayType.class.cast(type); Class<?> rawClass = getRawClass(genericArrayType.getGenericComponentType()); return Array.newInstance(rawClass, 0).getClass(); } return null; }