Java Code Examples for java.lang.reflect.TypeVariable#getBounds()
The following examples show how to use
java.lang.reflect.TypeVariable#getBounds() .
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: ClassUtil.java From qaf with MIT License | 7 votes |
public static Class<?> getRawType(Type type) { if (type instanceof Class<?>) { // type is a normal class. return (Class<?>) type; } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); return (Class<?>) rawType; } else if (type instanceof GenericArrayType) { final GenericArrayType genericArrayType = (GenericArrayType) type; final Class<?> componentRawType = getRawType(genericArrayType.getGenericComponentType()); return Array.newInstance(componentRawType, 0).getClass(); } else if (type instanceof TypeVariable) { final TypeVariable typeVar = (TypeVariable) type; if ((typeVar.getBounds() != null) && (typeVar.getBounds().length > 0)) { return getRawType(typeVar.getBounds()[0]); } } throw new RuntimeException("Unable to determine base class from Type"); }
Example 2
Source File: CovariantTypes.java From quarkus with Apache License 2.0 | 5 votes |
/** * Returns <tt>true</tt> if <tt>type2</tt> is a "sub-variable" of <tt>type1</tt>, i.e. if they are equal or if * <tt>type2</tt> (transitively) extends <tt>type1</tt>. */ private static boolean isAssignableFrom(TypeVariable<?> type1, TypeVariable<?> type2) { if (type1.equals(type2)) { return true; } // if a type variable extends another type variable, it cannot declare other bounds if (type2.getBounds()[0] instanceof TypeVariable<?>) { return isAssignableFrom(type1, (TypeVariable<?>) type2.getBounds()[0]); } return false; }
Example 3
Source File: ApiSurface.java From beam with Apache License 2.0 | 5 votes |
/** * Adds any types exposed to this set. These will come from the (possibly absent) bounds on the * type variable. */ private void addExposedTypes(TypeVariable type, Class<?> cause) { if (done(type)) { return; } visit(type); for (Type bound : type.getBounds()) { LOG.debug("Adding exposed types from {}, which is a type bound on {}", bound, type); addExposedTypes(bound, cause); } }
Example 4
Source File: ArrayDeserializer.java From elasticsearch-jdbc with MIT License | 5 votes |
@SuppressWarnings({"unchecked", "rawtypes"}) public <T> T deserialize(Object object, Type type) { JSONArray jsonArray; if (object instanceof JSONArray) { jsonArray = (JSONArray) object; } else { jsonArray = new JSONArray(object); } Class componentClass = null; Type componentType = null; if (type instanceof GenericArrayType) { componentType = ((GenericArrayType) type).getGenericComponentType(); if (componentType instanceof TypeVariable) { TypeVariable<?> componentVar = (TypeVariable<?>) componentType; componentType = componentVar.getBounds()[0]; } if (componentType instanceof Class<?>) { componentClass = (Class<?>) componentType; } } else { Class clazz = (Class) type; componentType = componentClass = clazz.getComponentType(); } int size = jsonArray.size(); Object array = Array.newInstance(componentClass, size); for (int i = 0; i < size; i++) { Object value = jsonArray.get(i); Deserializer deserializer = JSONParser.getDeserializer(componentClass); Array.set(array, i, deserializer.deserialize(value, componentType)); } return (T) array; }
Example 5
Source File: PlantRenderer.java From Java2PlantUML with Apache License 2.0 | 5 votes |
private void addClassTypeParams(StringBuilder sb, Class<?> aClass) { List<String> typeParams = new ArrayList<>(); // TODO: we are leaving lower bounds out, e.g. <? super Integer> for (TypeVariable t : aClass.getTypeParameters()) { Type[] bounds = t.getBounds(); String jointBounds = TypesHelper.getSimpleName(StringUtils.join(bounds, "&")); typeParams.add(t.getName() + " extends " + jointBounds); } if (0 < typeParams.size()) { sb.append(" <").append(StringUtils.join(typeParams, ", ")).append(">"); } }
Example 6
Source File: TypeResolver.java From onetwo with Apache License 2.0 | 5 votes |
/** * Resolves the first bound for the {@code typeVariable}, returning {@code Unknown.class} if none can be resolved. */ public static Type resolveBound(TypeVariable<?> typeVariable) { Type[] bounds = typeVariable.getBounds(); if (bounds.length == 0) return Unknown.class; Type bound = bounds[0]; if (bound instanceof TypeVariable) bound = resolveBound((TypeVariable<?>) bound); return bound == Object.class ? Unknown.class : bound; }
Example 7
Source File: BoundedGenericMethodsTests.java From j2objc with Apache License 2.0 | 5 votes |
/** * Tests whether the specified method declares a parameter with the type of * the type parameter. * * @param method * the declaring method */ private void parameterType(Method method) { TypeVariable<Method> typeParameter = getTypeParameter(method); assertLenghtOne(method.getGenericParameterTypes()); Type genericParameterType = method.getGenericParameterTypes()[0]; assertEquals(typeParameter, genericParameterType); assertTrue(genericParameterType instanceof TypeVariable); TypeVariable<?> typeVariable = (TypeVariable<?>) genericParameterType; assertEquals(method, typeVariable.getGenericDeclaration()); Type[] paramBounds = typeVariable.getBounds(); assertLenghtOne(paramBounds); Type paramBound = paramBounds[0]; assertEquals(BoundedGenericMethods.class, paramBound); }
Example 8
Source File: TypeVariableTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testSimpleTypeVariableOnClass(){ Class<? extends A> clazz = A.class; TypeVariable[] typeParameters = clazz.getTypeParameters(); assertLenghtOne(typeParameters); TypeVariable<Class> typeVariable = typeParameters[0]; assertEquals(clazz, typeVariable.getGenericDeclaration()); assertEquals("T", typeVariable.getName()); assertEquals("T", typeVariable.toString()); assertEquals("T", typeVariable.getTypeName()); Type[] bounds = typeVariable.getBounds(); assertLenghtOne(bounds); assertEquals(Object.class, bounds[0]); }
Example 9
Source File: TypeVariableTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testSingleBound() throws Exception { Class<? extends G> clazz = G.class; TypeVariable[] typeParameters = clazz.getTypeParameters(); TypeVariable<Class> typeVariable = typeParameters[0]; Type[] bounds = typeVariable.getBounds(); assertLenghtOne(bounds); assertEquals(Number.class, bounds[0]); assertEquals("T", typeVariable.toString()); assertEquals("T", typeVariable.getTypeName()); }
Example 10
Source File: TypeUtil.java From spearal-java with Apache License 2.0 | 5 votes |
public static Type getBoundType(TypeVariable<?> typeVariable) { Type[] ubs = typeVariable.getBounds(); if (ubs.length > 0) return ubs[0]; // should never happen... if (typeVariable.getGenericDeclaration() instanceof Type) return (Type)typeVariable.getGenericDeclaration(); return typeVariable; }
Example 11
Source File: TypeVariableTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testSimpleTypeVariableOnMethod() throws Exception{ Class<? extends B> clazz = B.class; Method method = clazz.getDeclaredMethod("b"); TypeVariable<Method>[] typeParameters = method.getTypeParameters(); assertLenghtOne(typeParameters); TypeVariable<Method> typeVariable = typeParameters[0]; assertEquals(method, typeVariable.getGenericDeclaration()); assertEquals("T", typeVariable.getName()); assertEquals("T", typeVariable.toString()); assertEquals("T", typeVariable.getTypeName()); Type[] bounds = typeVariable.getBounds(); assertLenghtOne(bounds); assertEquals(Object.class, bounds[0]); }
Example 12
Source File: TestGenerics.java From yGuard with MIT License | 5 votes |
public void run(){ new GenericSignatureFormatError(); ParameterizedType<MyStringType> pt = new ParameterizedType<MyStringType>(); pt.add(new MyStringType()); pt.add(new MyStringType(){}); for (MyType myType : pt.getList()){ System.out.println(); System.out.println("myType " + myType); System.out.println("Enclosed by " + myType.getClass().getEnclosingMethod()); System.out.println("Enclosed by " + myType.getClass().getEnclosingClass().getName()); } // Field[] fields = this.getClass().getDeclaredFields(); for (Field field : this.getClass().getDeclaredFields()){ System.out.println(); for (Annotation a : field.getAnnotations()){ System.out.println(a); } System.out.println(field); System.out.println("generic type " + field.getGenericType()); } for (TypeVariable tv : pt.getClass().getTypeParameters()){ System.out.println(); System.out.println(tv); for (Type t : tv.getBounds()){ System.out.println("bounds " + t); } } }
Example 13
Source File: Generics.java From ldp4j with Apache License 2.0 | 5 votes |
protected static <T> Class<T> processTypeVariable(Class<? super T> bound, TypeVariable<?> typeVariable) { for(Type paramBound:typeVariable.getBounds()) { if(paramBound instanceof Class<?>) { Class<T >cls=determineClass(bound,paramBound); if(cls!=null) { return cls; } } } return null; }
Example 14
Source File: TypesWalker.java From generics-resolver with MIT License | 5 votes |
/** * Declarations like {@code Some<T extends Some<T>>} could cause infinite analysis cycles without proper detection. * * @param src generic declaration class * @param genericName generic name * @param genericType actual generic value * @return true if cycle detected, false otherwise */ private static boolean isGenericLoop(final Class<?> src, final String genericName, final Type genericType) { // to avoid redundant checks, first look if same type is declared in parameter if (src.isAssignableFrom(GenericsUtils.resolveClass(genericType))) { // look if this generic declaration reference itself (Some<T extends Some<T>>) for (TypeVariable var : src.getTypeParameters()) { if (var.getName().equals(genericName)) { for (Type bound : var.getBounds()) { // declaration through the same type found (Some<T extends Some>) if (bound instanceof ParameterizedType && ((ParameterizedType) bound).getRawType().equals(src)) { for (Type param : ((ParameterizedType) bound).getActualTypeArguments()) { // loop detected (recursive generic declaration) if (param instanceof TypeVariable && ((TypeVariable) param).getName().equals(genericName)) { return false; } } break; } } break; } } } return false; }
Example 15
Source File: BeanTypeAssignabilityRules.java From quarkus with Apache License 2.0 | 4 votes |
static Type[] getUppermostTypeVariableBounds(TypeVariable<?> bound) { if (bound.getBounds()[0] instanceof TypeVariable<?>) { return getUppermostTypeVariableBounds((TypeVariable<?>) bound.getBounds()[0]); } return bound.getBounds(); }
Example 16
Source File: TypeResolver.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Resolves {@code var} using the encapsulated type mapping. If it maps to yet another * non-reified type or has bounds, {@code forDependants} is used to do further resolution, which * doesn't try to resolve any type variable on generic declarations that are already being * resolved. * * <p>Should only be called and overridden by {@link #resolve(TypeVariable)}. */ Type resolveInternal(TypeVariable<?> var, TypeTable forDependants) { Type type = map.get(new TypeVariableKey(var)); if (type == null) { Type[] bounds = var.getBounds(); if (bounds.length == 0) { return var; } Type[] resolvedBounds = new TypeResolver(forDependants).resolveTypes(bounds); /* * We'd like to simply create our own TypeVariable with the newly resolved bounds. There's * just one problem: Starting with JDK 7u51, the JDK TypeVariable's equals() method doesn't * recognize instances of our TypeVariable implementation. This is a problem because users * compare TypeVariables from the JDK against TypeVariables returned by TypeResolver. To * work with all JDK versions, TypeResolver must return the appropriate TypeVariable * implementation in each of the three possible cases: * * 1. Prior to JDK 7u51, the JDK TypeVariable implementation interoperates with ours. * Therefore, we can always create our own TypeVariable. * * 2. Starting with JDK 7u51, the JDK TypeVariable implementations does not interoperate * with ours. Therefore, we have to be careful about whether we create our own TypeVariable: * * 2a. If the resolved types are identical to the original types, then we can return the * original, identical JDK TypeVariable. By doing so, we sidestep the problem entirely. * * 2b. If the resolved types are different from the original types, things are trickier. The * only way to get a TypeVariable instance for the resolved types is to create our own. The * created TypeVariable will not interoperate with any JDK TypeVariable. But this is OK: We * don't _want_ our new TypeVariable to be equal to the JDK TypeVariable because it has * _different bounds_ than the JDK TypeVariable. And it wouldn't make sense for our new * TypeVariable to be equal to any _other_ JDK TypeVariable, either, because any other JDK * TypeVariable must have a different declaration or name. The only TypeVariable that our * new TypeVariable _will_ be equal to is an equivalent TypeVariable that was also created * by us. And that equality is guaranteed to hold because it doesn't involve the JDK * TypeVariable implementation at all. */ if (Types.NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY && Arrays.equals(bounds, resolvedBounds)) { return var; } return Types.newArtificialTypeVariable(var.getGenericDeclaration(), var.getName(), resolvedBounds); } // in case the type is yet another type variable. return new TypeResolver(forDependants).resolveType(type); }
Example 17
Source File: TypeResolver.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Resolves {@code var} using the encapsulated type mapping. If it maps to yet another * non-reified type or has bounds, {@code forDependants} is used to do further resolution, which * doesn't try to resolve any type variable on generic declarations that are already being * resolved. * * <p>Should only be called and overridden by {@link #resolve(TypeVariable)}. */ Type resolveInternal(TypeVariable<?> var, TypeTable forDependants) { Type type = map.get(new TypeVariableKey(var)); if (type == null) { Type[] bounds = var.getBounds(); if (bounds.length == 0) { return var; } Type[] resolvedBounds = new TypeResolver(forDependants).resolveTypes(bounds); /* * We'd like to simply create our own TypeVariable with the newly resolved bounds. There's * just one problem: Starting with JDK 7u51, the JDK TypeVariable's equals() method doesn't * recognize instances of our TypeVariable implementation. This is a problem because users * compare TypeVariables from the JDK against TypeVariables returned by TypeResolver. To * work with all JDK versions, TypeResolver must return the appropriate TypeVariable * implementation in each of the three possible cases: * * 1. Prior to JDK 7u51, the JDK TypeVariable implementation interoperates with ours. * Therefore, we can always create our own TypeVariable. * * 2. Starting with JDK 7u51, the JDK TypeVariable implementations does not interoperate * with ours. Therefore, we have to be careful about whether we create our own TypeVariable: * * 2a. If the resolved types are identical to the original types, then we can return the * original, identical JDK TypeVariable. By doing so, we sidestep the problem entirely. * * 2b. If the resolved types are different from the original types, things are trickier. The * only way to get a TypeVariable instance for the resolved types is to create our own. The * created TypeVariable will not interoperate with any JDK TypeVariable. But this is OK: We * don't _want_ our new TypeVariable to be equal to the JDK TypeVariable because it has * _different bounds_ than the JDK TypeVariable. And it wouldn't make sense for our new * TypeVariable to be equal to any _other_ JDK TypeVariable, either, because any other JDK * TypeVariable must have a different declaration or name. The only TypeVariable that our * new TypeVariable _will_ be equal to is an equivalent TypeVariable that was also created * by us. And that equality is guaranteed to hold because it doesn't involve the JDK * TypeVariable implementation at all. */ if (Types.NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY && Arrays.equals(bounds, resolvedBounds)) { return var; } return Types.newArtificialTypeVariable(var.getGenericDeclaration(), var.getName(), resolvedBounds); } // in case the type is yet another type variable. return new TypeResolver(forDependants).resolveType(type); }
Example 18
Source File: TypeVariableResolver.java From openpojo with Apache License 2.0 | 4 votes |
public Type[] getParameterTypes(TypeVariable type) { return type.getBounds(); }
Example 19
Source File: Lang_15_TypeUtils_s.java From coming with MIT License | 2 votes |
/** * <p> Returns an array containing the sole type of {@link Object} if * {@link TypeVariable#getBounds()} returns an empty array. Otherwise, it * returns the result of <code>TypeVariable.getBounds()</code> passed into * {@link #normalizeUpperBounds}. </p> * * @param typeVariable the subject type variable * @return a non-empty array containing the bounds of the type variable. */ public static Type[] getImplicitBounds(TypeVariable<?> typeVariable) { Type[] bounds = typeVariable.getBounds(); return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds); }
Example 20
Source File: Lang_15_TypeUtils_t.java From coming with MIT License | 2 votes |
/** * <p> Returns an array containing the sole type of {@link Object} if * {@link TypeVariable#getBounds()} returns an empty array. Otherwise, it * returns the result of <code>TypeVariable.getBounds()</code> passed into * {@link #normalizeUpperBounds}. </p> * * @param typeVariable the subject type variable * @return a non-empty array containing the bounds of the type variable. */ public static Type[] getImplicitBounds(TypeVariable<?> typeVariable) { Type[] bounds = typeVariable.getBounds(); return bounds.length == 0 ? new Type[] { Object.class } : normalizeUpperBounds(bounds); }