List of usage examples for java.lang.reflect WildcardType getUpperBounds
Type[] getUpperBounds();
From source file:Main.java
final static Type expand(Type type, Class<?> inClass) { Type expandedType = null;//from w ww .j a v a2s . 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:TypeUtils.java
private static boolean isAssignable(WildcardType lhsType, Type rhsType) { Type[] upperBounds = lhsType.getUpperBounds(); Type[] lowerBounds = lhsType.getLowerBounds(); for (int size = upperBounds.length, i = 0; i < size; ++i) { if (!isAssignable(upperBounds[i], rhsType)) { return false; }//from w ww . ja va 2 s. c o m } for (int size = lowerBounds.length, i = 0; i < size; ++i) { if (!isAssignable(rhsType, lowerBounds[i])) { return false; } } return true; }
From source file:com.autentia.common.util.ClassWithList.java
private static void print(WildcardType wt) { System.out.println("Wildcard type"); System.out.println("Lower bounds:"); for (Type b : wt.getLowerBounds()) { print(b);//w ww. java 2s. co m } System.out.println("Upper bounds:"); for (Type b : wt.getUpperBounds()) { print(b); } }
From source file:com.expedia.tesla.compiler.plugins.JavaTypeMapper.java
private Type fromJava(Schema.SchemaBuilder schemaBuilder, java.lang.reflect.Type jt) throws TeslaSchemaException { if (jt instanceof java.lang.Class) { return fromJavaForward(schemaBuilder, (java.lang.Class<?>) jt); } else if (jt instanceof java.lang.reflect.WildcardType) { // ? extends Interface java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) jt; return fromJava(schemaBuilder, wt.getUpperBounds()[0]); } else if (jt instanceof java.lang.reflect.GenericArrayType) { // T[]/* ww w. ja v a 2 s. c o m*/ java.lang.reflect.GenericArrayType ga = (java.lang.reflect.GenericArrayType) jt; Type elementType = fromJava(schemaBuilder, ga.getGenericComponentType()); return schemaBuilder.addType(String.format("array<%s>", elementType.getTypeId())); } else if (jt instanceof TypeVariable) { // T TypeVariable<?> tv = (TypeVariable<?>) jt; return fromJava(schemaBuilder, tv.getBounds()[0]); } else if (jt instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) jt; java.lang.Class<?> rt = (java.lang.Class<?>) pt.getRawType(); if (java.util.Map.class.isAssignableFrom(rt)) { // Map java.lang.reflect.Type kt = (java.lang.reflect.Type) pt.getActualTypeArguments()[0]; java.lang.reflect.Type vt = (java.lang.reflect.Type) pt.getActualTypeArguments()[1]; Type keyType = fromJava(schemaBuilder, kt); Type valueType = fromJava(schemaBuilder, vt); String fs = null; java.lang.Class<?> rawType = (java.lang.Class<?>) pt.getRawType(); if (rawType.isInterface()) { fs = "map<%s,%s>"; } else { fs = "map[" + rawType.getCanonicalName() + "]<%s,%s>"; } String tid = String.format(fs, keyType.getTypeId(), valueType.getTypeId()); return schemaBuilder.addType(tid); } else if (java.util.Collection.class.isAssignableFrom(rt)) { // Collection array (List<?>, Set<?>), use Collection and // ArrayList by default java.lang.reflect.Type et = (java.lang.reflect.Type) pt.getActualTypeArguments()[0]; Type elementType = fromJava(schemaBuilder, et); String fs = null; java.lang.Class<?> rawType = (java.lang.Class<?>) pt.getRawType(); if (rawType.isInterface()) { fs = "array[java.util.Collection,java.util.ArrayList]<%s>"; if (java.util.List.class.isAssignableFrom(rt)) { fs = "array[java.util.List,java.util.ArrayList]<%s>"; } else if (java.util.Set.class.isAssignableFrom(rt)) { fs = "array[java.util.Set,java.util.HashSet]<%s>"; } } else { fs = "array[" + rawType.getCanonicalName() + "]<%s>"; } String tid = String.format(fs, elementType.getTypeId()); return schemaBuilder.addType(tid); } return fromJavaForward(schemaBuilder, rt); } else { throw new TeslaSchemaException("BUG"); } }
From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java
@NotNull public Class getUpperBound(Type type, String desc) { if (type instanceof Class) { return (Class) type; } else if (type instanceof WildcardType) { WildcardType wildcard = ((WildcardType) type); if (wildcard.getUpperBounds().length != 1) { throw new IllegalArgumentException("Wrong number of upper bounds for " + type + " (" + wildcard.getUpperBounds().length + "): " + desc); }/* ww w. j a va 2 s.c om*/ Type upper = wildcard.getUpperBounds()[0]; if (upper instanceof Class) { return (Class) upper; } else { throw new IllegalArgumentException( "Upper bound for " + type + " is not a class, it is " + type + ": " + desc); } } else { throw new IllegalArgumentException(type + "is not a class nor wildcard type: " + type + ": " + desc); } }
From source file:com.alibaba.fastjson.parser.ParserConfig.java
public ObjectDeserializer getDeserializer(Type type) { ObjectDeserializer derializer = this.deserializers.get(type); if (derializer != null) { return derializer; }//from w w w .ja v a 2s . c om if (type instanceof Class<?>) { return getDeserializer((Class<?>) type, type); } if (type instanceof ParameterizedType) { Type rawType = ((ParameterizedType) type).getRawType(); if (rawType instanceof Class<?>) { return getDeserializer((Class<?>) rawType, type); } else { return getDeserializer(rawType); } } if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; Type[] upperBounds = wildcardType.getUpperBounds(); if (upperBounds.length == 1) { Type upperBoundType = upperBounds[0]; return getDeserializer(upperBoundType); } } return JavaObjectDeserializer.instance; }
From source file:com.clark.func.Functions.java
/** * <p>//from w w w.j a v a 2s. co m * Returns an array containing the sole value of {@link Object} if * {@link WildcardType#getUpperBounds()} returns an empty array. Otherwise, * it returns the result of <code>WildcardType.getUpperBounds()</code> * passed into {@link #normalizeUpperBounds}. * </p> * * @param wildcardType * the subject wildcard type * @return a non-empty array containing the upper bounds of the wildcard * type. */ public static Type[] getImplicitUpperBounds(WildcardType wildcardType) { Type[] bounds = wildcardType.getUpperBounds(); return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds); }
From source file:org.apache.sling.stanbol.rest.ported.JerseyUtils.java
/** * 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 *//*from w ww .j av a 2 s . c o m*/ public static boolean testType(Class<?> required, Type genericType) { //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; if (genericType instanceof Class<?>) { //OK // Set<Representation> // Set<Object> //NOT OK // Set<RdfRepresentation> // Set<InputStream> typeOK = ((Class<?>) genericType).isAssignableFrom(required); } else if (genericType instanceof WildcardType) { //In cases <? super {class}>, <? extends {class}, <?> WildcardType wildcardSetType = (WildcardType) genericType; 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() && genericType instanceof GenericArrayType) { //In case the required type is an array we need also to support //possible generic Array specifications GenericArrayType arrayType = (GenericArrayType) genericType; typeOK = testType(required.getComponentType(), arrayType.getGenericComponentType()); } else { //GenericArrayType but !required.isArray() -> incompatible //TypeVariable -> no variables define -> incompatible typeOK = false; } return typeOK; }
From source file:org.assertj.assertions.generator.util.ClassUtil.java
/** * Get the underlying class for a type, or null if the type is a variable type. * * @param type the type//w w w.j av a 2s. co m * @return the underlying class */ public static Class<?> getClass(final Type type) { if (type instanceof Class) return (Class<?>) type; if (type instanceof ParameterizedType) return getClass(((ParameterizedType) type).getRawType()); if (type instanceof GenericArrayType) { final Type componentType = ((GenericArrayType) type).getGenericComponentType(); final Class<?> componentClass = getClass(componentType); return componentClass == null ? null : Array.newInstance(componentClass, 0).getClass(); } else if (type instanceof WildcardType) { final WildcardType wildcardType = (WildcardType) type; return wildcardType.getUpperBounds() != null ? getClass(wildcardType.getUpperBounds()[0]) : wildcardType.getLowerBounds() != null ? getClass(wildcardType.getLowerBounds()[0]) : null; } else if (type instanceof TypeVariable) { final TypeVariable<?> typeVariable = (TypeVariable<?>) type; final Type[] bounds = typeVariable.getBounds(); return bounds.length > 0 ? getClass(bounds[0]) : Object.class; } return null; }
From source file:org.datalorax.populace.core.populate.PopulatorContext.java
private Object createInstanceFromWildcard(final WildcardType type, final Object parent) { final Type[] upperBounds = type.getUpperBounds(); final Optional<InstanceFactory> factory = upperBounds.length == 1 ? instanceFactories.getSpecific(type) : Optional.empty();// w ww . java 2 s .c o m final Class<?> rawType = factory.isPresent() ? TypeUtils.getRawType(upperBounds[0], null) : null; if (rawType != null) { return factory.get().createInstance(rawType, parent, instanceFactories); } if (upperBounds.length == 1) { return createInstance(upperBounds[0], parent); } return createInstance(Object.class, parent); }