Java Code Examples for java.lang.reflect.WildcardType#getLowerBounds()
The following examples show how to use
java.lang.reflect.WildcardType#getLowerBounds() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Types.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** Returns the array type of {@code componentType}. */ static Type newArrayType(Type componentType) { if (componentType instanceof WildcardType) { WildcardType wildcard = (WildcardType) componentType; Type[] lowerBounds = wildcard.getLowerBounds(); checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds."); if (lowerBounds.length == 1) { return supertypeOf(newArrayType(lowerBounds[0])); } else { Type[] upperBounds = wildcard.getUpperBounds(); checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound."); return subtypeOf(newArrayType(upperBounds[0])); } } return JavaVersion.CURRENT.newArrayType(componentType); }
Example 2
Source File: ResolvableType.java From dolphin with Apache License 2.0 | 6 votes |
/** * Get a {@link WildcardBounds} instance for the specified type, returning * {@code null} if the specified type cannot be resolved to a {@link WildcardType}. * @param type the source type * @return a {@link WildcardBounds} instance or {@code null} */ public static WildcardBounds get(ResolvableType type) { ResolvableType resolveToWildcard = type; while (!(resolveToWildcard.getType() instanceof WildcardType)) { if (resolveToWildcard == NONE) { return null; } resolveToWildcard = resolveToWildcard.resolveType(); } WildcardType wildcardType = (WildcardType) resolveToWildcard.type; Kind boundsType = (wildcardType.getLowerBounds().length > 0 ? Kind.LOWER : Kind.UPPER); Type[] bounds = boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds(); ResolvableType[] resolvableBounds = new ResolvableType[bounds.length]; for (int i = 0; i < bounds.length; i++) { resolvableBounds[i] = ResolvableType.forType(bounds[i], type.variableResolver); } return new WildcardBounds(boundsType, resolvableBounds); }
Example 3
Source File: ResolvableType.java From java-technology-stack with MIT License | 6 votes |
/** * Get a {@link WildcardBounds} instance for the specified type, returning * {@code null} if the specified type cannot be resolved to a {@link WildcardType}. * @param type the source type * @return a {@link WildcardBounds} instance or {@code null} */ @Nullable public static WildcardBounds get(ResolvableType type) { ResolvableType resolveToWildcard = type; while (!(resolveToWildcard.getType() instanceof WildcardType)) { if (resolveToWildcard == NONE) { return null; } resolveToWildcard = resolveToWildcard.resolveType(); } WildcardType wildcardType = (WildcardType) resolveToWildcard.type; Kind boundsType = (wildcardType.getLowerBounds().length > 0 ? Kind.LOWER : Kind.UPPER); Type[] bounds = (boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds()); ResolvableType[] resolvableBounds = new ResolvableType[bounds.length]; for (int i = 0; i < bounds.length; i++) { resolvableBounds[i] = ResolvableType.forType(bounds[i], type.variableResolver); } return new WildcardBounds(boundsType, resolvableBounds); }
Example 4
Source File: b.java From letv with Apache License 2.0 | 6 votes |
public static Type a(Type type) { if (type instanceof Class) { c cVar; Class cls = (Class) type; if (cls.isArray()) { cVar = new c(a(cls.getComponentType())); } else { Object obj = cls; } return cVar; } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return new d(parameterizedType.getOwnerType(), parameterizedType.getRawType(), parameterizedType.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { return new c(((GenericArrayType) type).getGenericComponentType()); } else { if (!(type instanceof WildcardType)) { return type; } WildcardType wildcardType = (WildcardType) type; return new e(wildcardType.getUpperBounds(), wildcardType.getLowerBounds()); } }
Example 5
Source File: $Gson$Types.java From GVGAI_GYM with Apache License 2.0 | 6 votes |
/** * Returns a type that is functionally equal but not necessarily equal * according to {@link Object#equals(Object) Object.equals()}. The returned * type is {@link java.io.Serializable}. */ public static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { // type is either serializable as-is or unsupported return type; } }
Example 6
Source File: MoreTypes.java From businessworks with Apache License 2.0 | 5 votes |
/** * Returns a type that is functionally equal but not necessarily equal * according to {@link Object#equals(Object) Object.equals()}. The returned * type is {@link Serializable}. */ public static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof CompositeType) { return type; } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { // type is either serializable as-is or unsupported return type; } }
Example 7
Source File: ResolvableType.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Determine whether the underlying type represents a wildcard * without specific bounds (i.e., equal to {@code ? extends Object}). */ private boolean isWildcardWithoutBounds() { if (this.type instanceof WildcardType) { WildcardType wt = (WildcardType) this.type; if (wt.getLowerBounds().length == 0) { Type[] upperBounds = wt.getUpperBounds(); if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) { return true; } } } return false; }
Example 8
Source File: ReflectHelpers.java From beam with Apache License 2.0 | 5 votes |
private void formatWildcardType(StringBuilder builder, WildcardType t) { builder.append("?"); for (Type lowerBound : t.getLowerBounds()) { builder.append(" super "); format(builder, lowerBound); } for (Type upperBound : t.getUpperBounds()) { if (!Object.class.equals(upperBound)) { builder.append(" extends "); format(builder, upperBound); } } }
Example 9
Source File: $Gson$Types.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
public static Type canonicalize(Type type) { if (type instanceof Class) { type = (Class)type; if (type.isArray()) { type = new a(canonicalize(((Type) (type.getComponentType())))); } } else { if (type instanceof ParameterizedType) { ParameterizedType parameterizedtype = (ParameterizedType)type; return new b(parameterizedtype.getOwnerType(), parameterizedtype.getRawType(), parameterizedtype.getActualTypeArguments()); } if (type instanceof GenericArrayType) { return new a(((GenericArrayType)type).getGenericComponentType()); } if (type instanceof WildcardType) { WildcardType wildcardtype = (WildcardType)type; return new c(wildcardtype.getUpperBounds(), wildcardtype.getLowerBounds()); } } return type; }
Example 10
Source File: TypeResolver.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
Type capture(Type type) { checkNotNull(type); if (type instanceof Class) { return type; } if (type instanceof TypeVariable) { return type; } if (type instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) type; return Types.newArrayType(capture(arrayType.getGenericComponentType())); } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return Types.newParameterizedTypeWithOwner(captureNullable(parameterizedType.getOwnerType()), (Class<?>) parameterizedType.getRawType(), capture(parameterizedType.getActualTypeArguments())); } if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; Type[] lowerBounds = wildcardType.getLowerBounds(); if (lowerBounds.length == 0) { // ? extends something changes to capture-of Type[] upperBounds = wildcardType.getUpperBounds(); String name = "capture#" + id.incrementAndGet() + "-of ? extends " + Joiner.on('&').join(upperBounds); return Types.newArtificialTypeVariable(WildcardCapturer.class, name, wildcardType.getUpperBounds()); } else { // TODO(benyu): handle ? super T somehow. return type; } } throw new AssertionError("must have been one of the known types"); }
Example 11
Source File: InjectionUtils.java From cxf with Apache License 2.0 | 5 votes |
public static Class<?> getActualType(Type genericType, int pos) { if (genericType == null) { return null; } if (genericType == Object.class) { return (Class<?>)genericType; } if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) { if (genericType instanceof TypeVariable) { genericType = getType(((TypeVariable<?>)genericType).getBounds(), pos); } else if (genericType instanceof WildcardType) { WildcardType wildcardType = (WildcardType)genericType; Type[] bounds = wildcardType.getLowerBounds(); if (bounds.length == 0) { bounds = wildcardType.getUpperBounds(); } genericType = getType(bounds, pos); } else if (genericType instanceof GenericArrayType) { genericType = ((GenericArrayType)genericType).getGenericComponentType(); } Class<?> cls = null; if (!(genericType instanceof ParameterizedType)) { cls = (Class<?>)genericType; } else { cls = (Class<?>)((ParameterizedType)genericType).getRawType(); } return cls.isArray() ? cls.getComponentType() : cls; } ParameterizedType paramType = (ParameterizedType)genericType; Type t = getType(paramType.getActualTypeArguments(), pos); return t instanceof Class ? (Class<?>)t : getActualType(t, 0); }
Example 12
Source File: ModelType.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
public ModelType<?> getLowerBound() { WildcardType wildcardType = getWildcardType(); if (wildcardType == null) { return null; } else { Type[] lowerBounds = wildcardType.getLowerBounds(); if (lowerBounds.length == 0) { return null; } else { return ModelType.of(lowerBounds[0]); } } }
Example 13
Source File: TypeUtils.java From dolphin-platform with Apache License 2.0 | 5 votes |
/** * Format a {@link WildcardType} as a {@link String}. * * @param w {@code WildcardType} to format * @return String * @since 3.2 */ private static String wildcardTypeToString(final WildcardType w) { final StringBuilder buf = new StringBuilder().append('?'); final Type[] lowerBounds = w.getLowerBounds(); final Type[] upperBounds = w.getUpperBounds(); if (lowerBounds.length > 1 || lowerBounds.length == 1 && lowerBounds[0] != null) { appendAllTo(buf.append(" super "), " & ", lowerBounds); } else if (upperBounds.length > 1 || upperBounds.length == 1 && !Object.class.equals(upperBounds[0])) { appendAllTo(buf.append(" extends "), " & ", upperBounds); } return buf.toString(); }
Example 14
Source File: MoreTypes.java From soabase-halva with Apache License 2.0 | 5 votes |
/** * Returns a type that is functionally equal but not necessarily equal * according to {@link Object#equals(Object) Object.equals()}. The returned * type is {@link Serializable}. */ public static Type canonicalize(Type type) { if (type instanceof Class) { Class<?> c = (Class<?>) type; return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c; } else if (type instanceof CompositeType) { return type; } else if (type instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) type; return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments()); } else if (type instanceof GenericArrayType) { GenericArrayType g = (GenericArrayType) type; return new GenericArrayTypeImpl(g.getGenericComponentType()); } else if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); } else { // type is either serializable as-is or unsupported return type; } }
Example 15
Source File: Utils.java From stanbol-freeling with GNU Affero General Public License v3.0 | 4 votes |
/** * Tests if a generic type (may be <?>, <? extends {required}> * or <? super {required}>) is compatible with the required one. * TODO: Should be moved to an utility class * @param required the required class the generic type MUST BE compatible with * @param genericType the required class * @return if the generic type is compatible with the required class */ public static boolean testType(Class<?> required, Type type) { //for the examples let assume that a Set is the raw type and the //requested generic type is a Representation with the following class //hierarchy: // Object // -> Representation // -> RdfRepresentation // -> InMemoryRepresentation // -> InputStream // -> Collection<T> boolean typeOK = false; if(type instanceof Class<?>){ typeOK = required.isAssignableFrom((Class<?>) type); type = ((Class<?>)type).getGenericSuperclass(); } else if(type instanceof WildcardType){ //In cases <? super {class}>, <? extends {class}, <?> WildcardType wildcardSetType = (WildcardType) type; if(wildcardSetType.getLowerBounds().length > 0){ Type lowerBound = wildcardSetType.getLowerBounds()[0]; //OK // Set<? super RdfRepresentation> // Set<? super Representation> //NOT OK // Set<? super InputStream> // Set<? super Collection<Representation>> typeOK = lowerBound instanceof Class<?> && required.isAssignableFrom((Class<?>)lowerBound); } else if (wildcardSetType.getUpperBounds().length > 0){ Type upperBound = wildcardSetType.getUpperBounds()[0]; //OK // Set<? extends Representation> // Set<? extends Object> //NOT OK // Set<? extends RdfRepresentation> // Set<? extends InputStream> // Set<? extends Collection<Representation> typeOK = upperBound instanceof Class<?> && ((Class<?>)upperBound).isAssignableFrom(required); } else { //no upper nor lower bound // Set<?> typeOK = true; } } else if(required.isArray() && type instanceof GenericArrayType){ //In case the required type is an array we need also to support //possible generic Array specifications GenericArrayType arrayType = (GenericArrayType)type; typeOK = testType(required.getComponentType(), arrayType.getGenericComponentType()); } else if(type instanceof ParameterizedType){ ParameterizedType pType = ((ParameterizedType)type); typeOK = pType.getRawType() instanceof Class<?> && required.isAssignableFrom((Class<?>)pType.getRawType()); type = null; } else { typeOK = false; } return typeOK; }
Example 16
Source File: TypeUtil.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private static Type[] getEffectiveLowerBounds(final WildcardType type) { final Type[] lowerBounds = type.getLowerBounds(); return lowerBounds.length == 0 ? new Type[]{null} : lowerBounds; }
Example 17
Source File: TypeResolver.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
private WildcardType resolveWildcardType(WildcardType type) { Type[] lowerBounds = type.getLowerBounds(); Type[] upperBounds = type.getUpperBounds(); return new Types.WildcardTypeImpl(resolveTypes(lowerBounds), resolveTypes(upperBounds)); }
Example 18
Source File: TypesWalker.java From generics-resolver with MIT License | 3 votes |
/** * When both wildcards are lower bounded (? super) then bounds must be compatible. * For example, ? super Integer and ? super BigInteger are not compatible (Integer, BigInteger) * and ? super Comparable and ? super Number are compatible (Number assignable to Comparable). * <p> * Of course, even incompatible lower bounds share some commons (at least object) but these types * could not be casted to one another and so no compatibility. * * @param one first wildcard type * @param two second wildcard type * @return true if onw of wildcards is not lower bounded or lower bounds are compatible, false when * lower bounds are incompatible */ private static boolean isLowerBoundsCompatible(final WildcardType one, final WildcardType two) { boolean res = true; final Type[] oneLower = one.getLowerBounds(); final Type[] twoLower = two.getLowerBounds(); if (oneLower.length > 0 && twoLower.length > 0) { res = isCompatible(GenericsUtils.resolveClassIgnoringVariables(oneLower[0]), GenericsUtils.resolveClassIgnoringVariables(twoLower[0])); } return res; }
Example 19
Source File: TypeUtils.java From astor with GNU General Public License v2.0 | 2 votes |
/** * <p> Returns an array containing a single value of <code>null</code> if * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise, * it returns the result of <code>WildcardType.getLowerBounds()</code>. </p> * * @param wildcardType the subject wildcard type * @return a non-empty array containing the lower bounds of the wildcard * type. */ public static Type[] getImplicitLowerBounds(WildcardType wildcardType) { Type[] bounds = wildcardType.getLowerBounds(); return bounds.length == 0 ? new Type[] { null } : bounds; }
Example 20
Source File: Lang_15_TypeUtils_t.java From coming with MIT License | 2 votes |
/** * <p> Returns an array containing a single value of <code>null</code> if * {@link WildcardType#getLowerBounds()} returns an empty array. Otherwise, * it returns the result of <code>WildcardType.getLowerBounds()</code>. </p> * * @param wildcardType the subject wildcard type * @return a non-empty array containing the lower bounds of the wildcard * type. */ public static Type[] getImplicitLowerBounds(WildcardType wildcardType) { Type[] bounds = wildcardType.getLowerBounds(); return bounds.length == 0 ? new Type[] { null } : bounds; }