Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/GenericsUtil.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/GenericsUtil.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/GenericsUtil.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/GenericsUtil.java Wed Jan 31 20:10:03 2018 @@ -25,21 +25,15 @@ import java.util.*; /** * Utility classes for generic type operations. */ -public final class GenericsUtil -{ - public static boolean satisfiesDependency(boolean isDelegateOrEvent, boolean isProducer, Type injectionPointType, Type beanType) - { - if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) - { +public final class GenericsUtil { + public static boolean satisfiesDependency(boolean isDelegateOrEvent, boolean isProducer, Type injectionPointType, Type beanType) { + if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); - } - else - { - Type injectionPointRawType = injectionPointType instanceof ParameterizedType? ((ParameterizedType)injectionPointType).getRawType(): injectionPointType; - Type beanRawType = beanType instanceof ParameterizedType? ((ParameterizedType)beanType).getRawType(): beanType; - - if (ClassUtil.isSame(injectionPointRawType, beanRawType)) - { + } else { + Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType) injectionPointType).getRawType() : injectionPointType; + Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType) beanType).getRawType() : beanType; + + if (ClassUtil.isSame(injectionPointRawType, beanRawType)) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); } } @@ -47,39 +41,28 @@ public final class GenericsUtil return false; } - public static boolean satisfiesDependencyRaw(boolean isDelegateOrEvent, boolean isProducer, Type injectionPointType, Type beanType) - { - if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) - { + public static boolean satisfiesDependencyRaw(boolean isDelegateOrEvent, boolean isProducer, Type injectionPointType, Type beanType) { + if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); - } - else - { - Type injectionPointRawType = injectionPointType instanceof ParameterizedType? ((ParameterizedType)injectionPointType).getRawType(): injectionPointType; - Type beanRawType = beanType instanceof ParameterizedType? ((ParameterizedType)beanType).getRawType(): beanType; + } else { + Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType) injectionPointType).getRawType() : injectionPointType; + Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType) beanType).getRawType() : beanType; - if (ClassUtil.isSame(injectionPointRawType, beanRawType)) - { + if (ClassUtil.isSame(injectionPointRawType, beanRawType)) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointRawType, beanRawType); - } - else - { + } else { Class bean = (Class) beanType; - if (bean.getSuperclass() != null && ClassUtil.isRawClassEquals(injectionPointType, bean.getSuperclass())) - { + if (bean.getSuperclass() != null && ClassUtil.isRawClassEquals(injectionPointType, bean.getSuperclass())) { return true; } Class<?>[] interfaces = bean.getInterfaces(); - if (interfaces == null || interfaces.length == 0) - { + if (interfaces == null || interfaces.length == 0) { return false; } - for (Class<?> clazz : interfaces) - { - if (ClassUtil.isRawClassEquals(injectionPointType, clazz)) - { + for (Class<?> clazz : interfaces) { + if (ClassUtil.isRawClassEquals(injectionPointType, clazz)) { return true; } } @@ -92,74 +75,46 @@ public final class GenericsUtil /** * 5.2.3 and 5.2.4 */ - public static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, Type requiredType, Type beanType) - { - if (requiredType instanceof Class) - { - return isAssignableFrom(isDelegateOrEvent, (Class<?>)requiredType, beanType); - } - else if (requiredType instanceof ParameterizedType) - { - return isAssignableFrom(isDelegateOrEvent, isProducer, (ParameterizedType)requiredType, beanType); - } - else if (requiredType instanceof TypeVariable) - { - return isAssignableFrom(isDelegateOrEvent, (TypeVariable<?>)requiredType, beanType); - } - else if (requiredType instanceof GenericArrayType) - { + public static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, Type requiredType, Type beanType) { + if (requiredType instanceof Class) { + return isAssignableFrom(isDelegateOrEvent, (Class<?>) requiredType, beanType); + } else if (requiredType instanceof ParameterizedType) { + return isAssignableFrom(isDelegateOrEvent, isProducer, (ParameterizedType) requiredType, beanType); + } else if (requiredType instanceof TypeVariable) { + return isAssignableFrom(isDelegateOrEvent, (TypeVariable<?>) requiredType, beanType); + } else if (requiredType instanceof GenericArrayType) { return Class.class.isInstance(beanType) && Class.class.cast(beanType).isArray() - && isAssignableFrom(isDelegateOrEvent, (GenericArrayType)requiredType, beanType); - } - else if (requiredType instanceof WildcardType) - { - return isAssignableFrom(isDelegateOrEvent, (WildcardType)requiredType, beanType); - } - else - { + && isAssignableFrom(isDelegateOrEvent, (GenericArrayType) requiredType, beanType); + } else if (requiredType instanceof WildcardType) { + return isAssignableFrom(isDelegateOrEvent, (WildcardType) requiredType, beanType); + } else { throw new IllegalArgumentException("Unsupported type " + requiredType.getClass()); } } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, Type beanType) - { - if (beanType instanceof Class) - { - return isAssignableFrom(injectionPointType, (Class<?>)beanType); - } - else if (beanType instanceof TypeVariable) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>)beanType); - } - else if (beanType instanceof ParameterizedType) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType)beanType); - } - else if (beanType instanceof GenericArrayType) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (GenericArrayType)beanType); - } - else if (beanType instanceof WildcardType) - { - return isAssignableFrom(isDelegateOrEvent, (Type)injectionPointType, (WildcardType)beanType); - } - else - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, Type beanType) { + if (beanType instanceof Class) { + return isAssignableFrom(injectionPointType, (Class<?>) beanType); + } else if (beanType instanceof TypeVariable) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>) beanType); + } else if (beanType instanceof ParameterizedType) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType) beanType); + } else if (beanType instanceof GenericArrayType) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (GenericArrayType) beanType); + } else if (beanType instanceof WildcardType) { + return isAssignableFrom(isDelegateOrEvent, (Type) injectionPointType, (WildcardType) beanType); + } else { throw new IllegalArgumentException("Unsupported type " + injectionPointType.getClass()); } } - private static boolean isAssignableFrom(Class<?> injectionPointType, Class<?> beanType) - { + private static boolean isAssignableFrom(Class<?> injectionPointType, Class<?> beanType) { return ClassUtil.isClassAssignableFrom(injectionPointType, beanType); } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, TypeVariable<?> beanType) - { - for (Type bounds: beanType.getBounds()) - { - if (isAssignableFrom(isDelegateOrEvent, injectionPointType, bounds)) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, TypeVariable<?> beanType) { + for (Type bounds : beanType.getBounds()) { + if (isAssignableFrom(isDelegateOrEvent, injectionPointType, bounds)) { return true; } } @@ -170,35 +125,27 @@ public final class GenericsUtil * CDI Spec. 5.2.4: "A parameterized bean type is considered assignable to a raw required type * if the raw generics are identical and all type parameters of the bean type are either unbounded type variables or java.lang.Object." */ - private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, ParameterizedType beanType) - { - if (beanType.getRawType() != injectionPointType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, ParameterizedType beanType) { + if (beanType.getRawType() != injectionPointType) { return false; //raw generics don't match } - if (isDelegateOrEvent) - { + if (isDelegateOrEvent) { // for delegate and events we match 'in reverse' kind off // @Observes ProcessInjectionPoint<?, Instance> does also match Instance<SomeBean> return isAssignableFrom(true, injectionPointType, beanType.getRawType()); } - for (Type typeArgument: beanType.getActualTypeArguments()) - { - if (typeArgument == Object.class) - { + for (Type typeArgument : beanType.getActualTypeArguments()) { + if (typeArgument == Object.class) { continue; } - if (!(typeArgument instanceof TypeVariable)) - { + if (!(typeArgument instanceof TypeVariable)) { return false; //neither object nor type variable } - TypeVariable<?> typeVariable = (TypeVariable<?>)typeArgument; - for (Type bounds: typeVariable.getBounds()) - { - if (bounds != Object.class) - { + TypeVariable<?> typeVariable = (TypeVariable<?>) typeArgument; + for (Type bounds : typeVariable.getBounds()) { + if (bounds != Object.class) { return false; //bound type variable } } @@ -206,71 +153,47 @@ public final class GenericsUtil return true; } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, GenericArrayType beanType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, GenericArrayType beanType) { return injectionPointType.isArray() && isAssignableFrom(isDelegateOrEvent, injectionPointType.getComponentType(), beanType.getGenericComponentType()); } - - private static boolean isAssignableFrom(boolean isDelegateOrEvent, Type injectionPointType, WildcardType beanType) - { - for (Type bounds: beanType.getLowerBounds()) - { - if (!isAssignableFrom(isDelegateOrEvent, false, bounds, injectionPointType)) - { + + private static boolean isAssignableFrom(boolean isDelegateOrEvent, Type injectionPointType, WildcardType beanType) { + for (Type bounds : beanType.getLowerBounds()) { + if (!isAssignableFrom(isDelegateOrEvent, false, bounds, injectionPointType)) { return false; } } - for (Type bounds: beanType.getUpperBounds()) - { - if (isAssignableFrom(isDelegateOrEvent, false, injectionPointType, bounds)) - { + for (Type bounds : beanType.getUpperBounds()) { + if (isAssignableFrom(isDelegateOrEvent, false, injectionPointType, bounds)) { return true; } } return false; } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, Type beanType) - { - if (beanType instanceof Class) - { - return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (Class<?>)beanType); - } - else if (beanType instanceof TypeVariable) - { - return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (TypeVariable<?>)beanType); - } - else if (beanType instanceof ParameterizedType) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType)beanType); - } - else if (beanType instanceof WildcardType) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (WildcardType)beanType); - } - else if (beanType instanceof GenericArrayType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, Type beanType) { + if (beanType instanceof Class) { + return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (Class<?>) beanType); + } else if (beanType instanceof TypeVariable) { + return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (TypeVariable<?>) beanType); + } else if (beanType instanceof ParameterizedType) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType) beanType); + } else if (beanType instanceof WildcardType) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (WildcardType) beanType); + } else if (beanType instanceof GenericArrayType) { return false; - } - else - { + } else { throw new IllegalArgumentException("Unsupported type " + beanType.getClass()); } } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, Class<?> beanType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, Class<?> beanType) { Class<?> rawInjectionPointType = getRawType(injectionPointType); - if (rawInjectionPointType.equals(beanType)) - { - if (isProducer) - { - for (final Type t : injectionPointType.getActualTypeArguments()) - { - if (!TypeVariable.class.isInstance(t) || !isNotBound(TypeVariable.class.cast(t).getBounds())) - { - if (!Class.class.isInstance(t) || Object.class != t) - { + if (rawInjectionPointType.equals(beanType)) { + if (isProducer) { + for (final Type t : injectionPointType.getActualTypeArguments()) { + if (!TypeVariable.class.isInstance(t) || !isNotBound(TypeVariable.class.cast(t).getBounds())) { + if (!Class.class.isInstance(t) || Object.class != t) { return false; } } @@ -278,35 +201,27 @@ public final class GenericsUtil } return true; } - if (!rawInjectionPointType.isAssignableFrom(beanType)) - { + if (!rawInjectionPointType.isAssignableFrom(beanType)) { return false; } - if (beanType.getSuperclass() != null && isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType.getGenericSuperclass())) - { + if (beanType.getSuperclass() != null && isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType.getGenericSuperclass())) { return true; } - for (Type genericInterface: beanType.getGenericInterfaces()) - { - if (isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, genericInterface)) - { + for (Type genericInterface : beanType.getGenericInterfaces()) { + if (isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, genericInterface)) { return true; } } return false; } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, TypeVariable<?> beanType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, TypeVariable<?> beanType) { final Type[] types = beanType.getBounds(); - if (isNotBound(types)) - { + if (isNotBound(types)) { return true; } - for (final Type bounds: types) - { - if (isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, bounds)) - { + for (final Type bounds : types) { + if (isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, bounds)) { return true; } } @@ -316,68 +231,51 @@ public final class GenericsUtil /** * CDI Spec. 5.2.4 */ - private static boolean isAssignableFrom(boolean isDelegateOrEvent, ParameterizedType injectionPointType, ParameterizedType beanType) - { - if (injectionPointType.getRawType() != beanType.getRawType()) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, ParameterizedType injectionPointType, ParameterizedType beanType) { + if (injectionPointType.getRawType() != beanType.getRawType()) { return false; } boolean swapParams = !isDelegateOrEvent; Type[] injectionPointTypeArguments = injectionPointType.getActualTypeArguments(); Type[] beanTypeArguments = beanType.getActualTypeArguments(); - for (int i = 0; i < injectionPointTypeArguments.length; i++) - { + for (int i = 0; i < injectionPointTypeArguments.length; i++) { Type injectionPointTypeArgument = injectionPointTypeArguments[i]; Type beanTypeArgument = beanTypeArguments[i]; // for this special case it's actually an 'assignable to', thus we swap the params, see CDI-389 // but this special rule does not apply to Delegate injection points... if (swapParams && - (injectionPointTypeArgument instanceof Class || injectionPointTypeArgument instanceof TypeVariable) && - beanTypeArgument instanceof TypeVariable) - { + (injectionPointTypeArgument instanceof Class || injectionPointTypeArgument instanceof TypeVariable) && + beanTypeArgument instanceof TypeVariable) { final Type[] bounds = ((TypeVariable<?>) beanTypeArgument).getBounds(); final boolean isNotBound = isNotBound(bounds); - if (!isNotBound) - { - for (final Type upperBound : bounds) - { - if (!isAssignableFrom(true, false, upperBound, injectionPointTypeArgument)) - { + if (!isNotBound) { + for (final Type upperBound : bounds) { + if (!isAssignableFrom(true, false, upperBound, injectionPointTypeArgument)) { return false; } } } - } - else if (swapParams && injectionPointTypeArgument instanceof TypeVariable) - { + } else if (swapParams && injectionPointTypeArgument instanceof TypeVariable) { return false; - } - else if (isDelegateOrEvent && injectionPointTypeArgument instanceof Class && beanTypeArgument instanceof Class) - { + } else if (isDelegateOrEvent && injectionPointTypeArgument instanceof Class && beanTypeArgument instanceof Class) { // if no wildcard type was given then we require a real exact match. return injectionPointTypeArgument.equals(beanTypeArgument); - } - else if (!isAssignableFrom(isDelegateOrEvent, false, injectionPointTypeArgument, beanTypeArgument)) - { + } else if (!isAssignableFrom(isDelegateOrEvent, false, injectionPointTypeArgument, beanTypeArgument)) { return false; } } return true; } - private static boolean isNotBound(final Type... bounds) - { + private static boolean isNotBound(final Type... bounds) { return bounds == null || bounds.length == 0 || (bounds.length == 1 && Object.class == bounds[0]); } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, TypeVariable<?> injectionPointType, Type beanType) - { - for (Type bounds: injectionPointType.getBounds()) - { - if (!isAssignableFrom(isDelegateOrEvent, false, bounds, beanType)) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, TypeVariable<?> injectionPointType, Type beanType) { + for (Type bounds : injectionPointType.getBounds()) { + if (!isAssignableFrom(isDelegateOrEvent, false, bounds, beanType)) { return false; } } @@ -385,131 +283,97 @@ public final class GenericsUtil } // rules are a bit different when in an array so we handle ParameterizedType manually (not reusing isAssignableFrom) - private static boolean isAssignableFrom(boolean isDelegateOrEvent, GenericArrayType injectionPointType, Type beanType) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, GenericArrayType injectionPointType, Type beanType) { final Type genericComponentType = injectionPointType.getGenericComponentType(); final Class componentType = Class.class.cast(beanType).getComponentType(); - if (Class.class.isInstance(genericComponentType)) - { + if (Class.class.isInstance(genericComponentType)) { return Class.class.cast(genericComponentType).isAssignableFrom(componentType); } - if (ParameterizedType.class.isInstance(genericComponentType)) - { + if (ParameterizedType.class.isInstance(genericComponentType)) { return isAssignableFrom(isDelegateOrEvent, false, ParameterizedType.class.cast(genericComponentType).getRawType(), componentType); } return isAssignableFrom(isDelegateOrEvent, false, genericComponentType, componentType); } - private static boolean isAssignableFrom(boolean isDelegateOrEvent, WildcardType injectionPointType, Type beanType) - { - if (beanType instanceof TypeVariable) - { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>)beanType); + private static boolean isAssignableFrom(boolean isDelegateOrEvent, WildcardType injectionPointType, Type beanType) { + if (beanType instanceof TypeVariable) { + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>) beanType); } - for (Type bounds: injectionPointType.getLowerBounds()) - { - if (!isAssignableFrom(isDelegateOrEvent, false, beanType, bounds)) - { + for (Type bounds : injectionPointType.getLowerBounds()) { + if (!isAssignableFrom(isDelegateOrEvent, false, beanType, bounds)) { return false; } } - for (Type bounds: injectionPointType.getUpperBounds()) - { + for (Type bounds : injectionPointType.getUpperBounds()) { Set<Type> beanTypeClosure = getTypeClosure(beanType); boolean isAssignable = false; - for (Type beanSupertype: beanTypeClosure) - { + for (Type beanSupertype : beanTypeClosure) { if (isAssignableFrom(isDelegateOrEvent, false, bounds, beanSupertype) - || (Class.class.isInstance(bounds) + || (Class.class.isInstance(bounds) && ParameterizedType.class.isInstance(beanSupertype) - && bounds == ParameterizedType.class.cast(beanSupertype).getRawType())) - { + && bounds == ParameterizedType.class.cast(beanSupertype).getRawType())) { isAssignable = true; break; } } - if (!isAssignable) - { + if (!isAssignable) { return false; } } return true; } - + /** * CDI 1.1 Spec. 5.2.4, third bullet point */ - private static boolean isAssignableFrom(boolean isDelegateOrEvent, WildcardType injectionPointType, TypeVariable<?> beanType) - { - for (Type upperBound: injectionPointType.getUpperBounds()) - { - for (Type bound: beanType.getBounds()) - { - if (!isAssignableFrom(isDelegateOrEvent, false, upperBound, bound) && !isAssignableFrom(isDelegateOrEvent, false, bound, upperBound)) - { + private static boolean isAssignableFrom(boolean isDelegateOrEvent, WildcardType injectionPointType, TypeVariable<?> beanType) { + for (Type upperBound : injectionPointType.getUpperBounds()) { + for (Type bound : beanType.getBounds()) { + if (!isAssignableFrom(isDelegateOrEvent, false, upperBound, bound) && !isAssignableFrom(isDelegateOrEvent, false, bound, upperBound)) { return false; } } } - for (Type lowerBound: injectionPointType.getLowerBounds()) - { - for (Type bound: beanType.getBounds()) - { - if (!isAssignableFrom(isDelegateOrEvent, false, bound, lowerBound)) - { + for (Type lowerBound : injectionPointType.getLowerBounds()) { + for (Type bound : beanType.getBounds()) { + if (!isAssignableFrom(isDelegateOrEvent, false, bound, lowerBound)) { return false; } } } return true; } - + /** * @return <tt>true</tt>, if the specified type declaration contains an unresolved type variable. */ - public static boolean containsTypeVariable(Type type) - { - if (type instanceof Class) - { + public static boolean containsTypeVariable(Type type) { + if (type instanceof Class) { return false; - } - else if (type instanceof TypeVariable) - { + } else if (type instanceof TypeVariable) { return true; - } - else if (type instanceof ParameterizedType) - { - ParameterizedType parameterizedType = (ParameterizedType)type; + } else if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; return containTypeVariable(parameterizedType.getActualTypeArguments()); - } - else if (type instanceof WildcardType) - { - WildcardType wildcardType = (WildcardType)type; + } else if (type instanceof WildcardType) { + WildcardType wildcardType = (WildcardType) type; return containTypeVariable(wildcardType.getUpperBounds()) || containTypeVariable(wildcardType.getLowerBounds()); - } - else if (type instanceof GenericArrayType) - { - GenericArrayType arrayType = (GenericArrayType)type; + } else if (type instanceof GenericArrayType) { + GenericArrayType arrayType = (GenericArrayType) type; return containsTypeVariable(arrayType.getGenericComponentType()); - } - else - { + } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); } } - - public static boolean containTypeVariable(Collection<? extends Type> types) - { + + public static boolean containTypeVariable(Collection<? extends Type> types) { return containTypeVariable(types.toArray(new Type[types.size()])); } - - public static boolean containTypeVariable(Type[] types) - { - for (Type type: types) - { - if (containsTypeVariable(type)) - { + + public static boolean containTypeVariable(Type[] types) { + for (Type type : types) { + if (containsTypeVariable(type)) { return true; } } @@ -517,32 +381,22 @@ public final class GenericsUtil } /** - * * @param type to check - * * @return {@code true} if the given type contains a {@link WildcardType} - * {@code false} otherwise + * {@code false} otherwise */ - public static boolean containsWildcardType(Type type) - { - if (!(type instanceof ParameterizedType)) - { + public static boolean containsWildcardType(Type type) { + if (!(type instanceof ParameterizedType)) { return false; } - for (Type typeArgument : getParameterizedType(type).getActualTypeArguments()) - { - if (ClassUtil.isParametrizedType(typeArgument)) - { - if (containsWildcardType(typeArgument)) - { + for (Type typeArgument : getParameterizedType(type).getActualTypeArguments()) { + if (ClassUtil.isParametrizedType(typeArgument)) { + if (containsWildcardType(typeArgument)) { return true; } - } - else - { - if (ClassUtil.isWildCardType(typeArgument)) - { + } else { + if (ClassUtil.isWildCardType(typeArgument)) { return true; } } @@ -555,101 +409,78 @@ public final class GenericsUtil /** * Resolves the actual type of the specified field for the type hierarchy specified by the given subclass */ - public static Type resolveType(Class<?> subclass, Field field) - { + public static Type resolveType(Class<?> subclass, Field field) { return resolveType(field.getGenericType(), subclass, newSeenList()); } /** * Resolves the actual return type of the specified method for the type hierarchy specified by the given subclass */ - public static Type resolveReturnType(Class<?> subclass, Method method) - { + public static Type resolveReturnType(Class<?> subclass, Method method) { return resolveType(method.getGenericReturnType(), subclass, newSeenList()); } /** * Resolves the actual parameter generics of the specified constructor for the type hierarchy specified by the given subclass */ - public static Type[] resolveParameterTypes(Class<?> subclass, Constructor<?> constructor) - { + public static Type[] resolveParameterTypes(Class<?> subclass, Constructor<?> constructor) { return resolveTypes(constructor.getGenericParameterTypes(), subclass); } /** * Resolves the actual parameter generics of the specified method for the type hierarchy specified by the given subclass */ - public static Type[] resolveParameterTypes(Class<?> subclass, Method method) - { + public static Type[] resolveParameterTypes(Class<?> subclass, Method method) { return resolveTypes(method.getGenericParameterTypes(), subclass); } /** * Resolves the actual type of the specified type for the type hierarchy specified by the given subclass */ - public static Type resolveType(Type type, Class<?> subclass, Member member) - { + public static Type resolveType(Type type, Class<?> subclass, Member member) { return resolveType(type, subclass, newSeenList()); } - public static Type resolveType(Type type, Class<?> subclass, Member member, Collection<TypeVariable<?>> seen) - { + public static Type resolveType(Type type, Class<?> subclass, Member member, Collection<TypeVariable<?>> seen) { return resolveType(type, subclass, seen); } - public static Type resolveType(Type type, Type actualType, Collection<TypeVariable<?>> seen) - { - if (type instanceof Class) - { + public static Type resolveType(Type type, Type actualType, Collection<TypeVariable<?>> seen) { + if (type instanceof Class) { return type; - } - else if (type instanceof ParameterizedType) - { - ParameterizedType parameterizedType = (ParameterizedType)type; + } else if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; Type[] resolvedTypeArguments; - if (Enum.class.equals(parameterizedType.getRawType())) - { + if (Enum.class.equals(parameterizedType.getRawType())) { // Enums derive from themselves, which would create an infinite loop // we directly escape the loop if we detect this. resolvedTypeArguments = new Type[]{new OwbWildcardTypeImpl(new Type[]{Enum.class}, ClassUtil.NO_TYPES)}; - } - else - { + } else { resolvedTypeArguments = resolveTypes(parameterizedType.getActualTypeArguments(), actualType, seen); } return new OwbParametrizedTypeImpl(parameterizedType.getOwnerType(), parameterizedType.getRawType(), resolvedTypeArguments); - } - else if (type instanceof TypeVariable) - { - TypeVariable<?> variable = (TypeVariable<?>)type; + } else if (type instanceof TypeVariable) { + TypeVariable<?> variable = (TypeVariable<?>) type; return resolveTypeVariable(variable, actualType, seen); - } - else if (type instanceof WildcardType) - { - WildcardType wildcardType = (WildcardType)type; + } else if (type instanceof WildcardType) { + WildcardType wildcardType = (WildcardType) type; Type[] upperBounds = resolveTypes(wildcardType.getUpperBounds(), actualType, seen); Type[] lowerBounds = resolveTypes(wildcardType.getLowerBounds(), actualType, seen); return new OwbWildcardTypeImpl(upperBounds, lowerBounds); - } - else if (type instanceof GenericArrayType) - { - GenericArrayType arrayType = (GenericArrayType)type; + } else if (type instanceof GenericArrayType) { + GenericArrayType arrayType = (GenericArrayType) type; return createArrayType(resolveType(arrayType.getGenericComponentType(), actualType, seen)); - } - else - { + } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); } } - - public static Type[] resolveTypes(Type[] types, Type actualType, Collection<TypeVariable<?>> seen) - { + + public static Type[] resolveTypes(Type[] types, Type actualType, Collection<TypeVariable<?>> seen) { Type[] resolvedTypeArguments = new Type[types.length]; - for (int i = 0; i < types.length; i++) - { + for (int i = 0; i < types.length; i++) { final Type type = resolveType(types[i], actualType, seen); if (type != null) // means a stackoverflow was avoided, just keep what we have { @@ -659,23 +490,19 @@ public final class GenericsUtil return resolvedTypeArguments; } - public static Type[] resolveTypes(Type[] types, Type actualType) - { + public static Type[] resolveTypes(Type[] types, Type actualType) { Type[] resolvedTypeArguments = new Type[types.length]; - for (int i = 0; i < types.length; i++) - { + for (int i = 0; i < types.length; i++) { resolvedTypeArguments[i] = resolveType(types[i], actualType, newSeenList()); } return resolvedTypeArguments; } - public static Set<Type> getTypeClosure(Class<?> type) - { + public static Set<Type> getTypeClosure(Class<?> type) { return getTypeClosure(type, type); } - public static Set<Type> getTypeClosure(Type actualType) - { + public static Set<Type> getTypeClosure(Type actualType) { return getTypeClosure(actualType, actualType); } @@ -687,7 +514,7 @@ public final class GenericsUtil * </p> * <code> * public class Foo<T> { - * private T t; + * private T t; * } * public class Bar extends Foo<Number> { * } @@ -704,7 +531,7 @@ public final class GenericsUtil * </p> * <code> * public class Foo<T> { - * private T t; + * private T t; * } * public class Bar<T> extends Foo<T> { * } @@ -716,147 +543,111 @@ public final class GenericsUtil * <code> * GenericUtil.getTypeClosure(Foo.class, new TypeLiteral<Foo<Number>>() {}.getType(), Bar.class); * </code> - * - * @param type the type to get the closure for + * + * @param type the type to get the closure for * @param actualType the context to bind type variables * @return the type closure */ - public static Set<Type> getTypeClosure(Type type, Type actualType) - { + public static Set<Type> getTypeClosure(Type type, Type actualType) { Class<?> rawType = getRawType(type); Class<?> actualRawType = getRawType(actualType); - if (rawType.isAssignableFrom(actualRawType) && rawType != actualRawType) - { + if (rawType.isAssignableFrom(actualRawType) && rawType != actualRawType) { return getTypeClosure(actualType, type); } - if (hasTypeParameters(type)) - { + if (hasTypeParameters(type)) { type = getParameterizedType(type); } return getDirectTypeClosure(type, actualType); } - public static Set<Type> getDirectTypeClosure(final Type type, final Type actualType) - { + public static Set<Type> getDirectTypeClosure(final Type type, final Type actualType) { Set<Type> typeClosure = new HashSet<Type>(); typeClosure.add(Object.class); fillTypeHierarchy(typeClosure, type, actualType); return typeClosure; } - private static void fillTypeHierarchy(Set<Type> set, Type type, Type actualType) - { - if (type == null) - { - return; + private static void fillTypeHierarchy(Set<Type> set, Type type, Type actualType) { + if (type == null) { + return; } Type resolvedType = GenericsUtil.resolveType(type, actualType, newSeenList()); set.add(resolvedType); Class<?> resolvedClass = GenericsUtil.getRawType(resolvedType, actualType); - if (resolvedClass.getSuperclass() != null) - { + if (resolvedClass.getSuperclass() != null) { fillTypeHierarchy(set, resolvedClass.getGenericSuperclass(), resolvedType); } - for (Type interfaceType: resolvedClass.getGenericInterfaces()) - { + for (Type interfaceType : resolvedClass.getGenericInterfaces()) { fillTypeHierarchy(set, interfaceType, resolvedType); } } - private static Collection<TypeVariable<?>> newSeenList() - { + private static Collection<TypeVariable<?>> newSeenList() { return new ArrayList<TypeVariable<?>>(); } - public static boolean hasTypeParameters(Type type) - { - if (type instanceof Class) - { - Class<?> classType = (Class<?>)type; + public static boolean hasTypeParameters(Type type) { + if (type instanceof Class) { + Class<?> classType = (Class<?>) type; return classType.getTypeParameters().length > 0; } return false; } - public static ParameterizedType getParameterizedType(Type type) - { - if (type instanceof ParameterizedType) - { - return (ParameterizedType)type; - } - else if (type instanceof Class) - { - Class<?> classType = (Class<?>)type; + public static ParameterizedType getParameterizedType(Type type) { + if (type instanceof ParameterizedType) { + return (ParameterizedType) type; + } else if (type instanceof Class) { + Class<?> classType = (Class<?>) type; return new OwbParametrizedTypeImpl(classType.getDeclaringClass(), classType, classType.getTypeParameters()); - } - else - { + } else { throw new IllegalArgumentException(type.getClass().getSimpleName() + " is not supported"); } } - public static <T> Class<T> getRawType(Type type) - { + public static <T> Class<T> getRawType(Type type) { return getRawType(type, null); } - static <T> Class<T> getRawType(Type type, Type actualType) - { - if (type instanceof Class) - { - return (Class<T>)type; - } - else if (type instanceof ParameterizedType) - { - ParameterizedType parameterizedType = (ParameterizedType)type; + static <T> Class<T> getRawType(Type type, Type actualType) { + if (type instanceof Class) { + return (Class<T>) type; + } else if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; return getRawType(parameterizedType.getRawType(), actualType); - } - else if (type instanceof TypeVariable) - { - TypeVariable<?> typeVariable = (TypeVariable<?>)type; + } else if (type instanceof TypeVariable) { + TypeVariable<?> typeVariable = (TypeVariable<?>) type; Type mostSpecificType = getMostSpecificType(getRawTypes(typeVariable.getBounds(), actualType), typeVariable.getBounds()); return getRawType(mostSpecificType, actualType); - } - else if (type instanceof WildcardType) - { - WildcardType wildcardType = (WildcardType)type; + } else if (type instanceof WildcardType) { + WildcardType wildcardType = (WildcardType) type; Type mostSpecificType = getMostSpecificType(getRawTypes(wildcardType.getUpperBounds(), actualType), wildcardType.getUpperBounds()); return getRawType(mostSpecificType, actualType); - } - else if (type instanceof GenericArrayType) - { - GenericArrayType arrayType = (GenericArrayType)type; + } else if (type instanceof GenericArrayType) { + GenericArrayType arrayType = (GenericArrayType) type; return getRawType(createArrayType(getRawType(arrayType.getGenericComponentType(), actualType)), actualType); - } - else - { + } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); } } - private static <T> Class<T>[] getRawTypes(Type[] types) - { + private static <T> Class<T>[] getRawTypes(Type[] types) { return getRawTypes(types, null); } - private static <T> Class<T>[] getRawTypes(Type[] types, Type actualType) - { + private static <T> Class<T>[] getRawTypes(Type[] types, Type actualType) { Class<T>[] rawTypes = new Class[types.length]; - for (int i = 0; i < types.length; i++) - { + for (int i = 0; i < types.length; i++) { rawTypes[i] = getRawType(types[i], actualType); } return rawTypes; } - private static Type getMostSpecificType(Class<?>[] types, Type[] genericTypes) - { + private static Type getMostSpecificType(Class<?>[] types, Type[] genericTypes) { Class<?> mostSpecificType = types[0]; int mostSpecificIndex = 0; - for (int i = 0; i < types.length; i++) - { - if (mostSpecificType.isAssignableFrom(types[i])) - { + for (int i = 0; i < types.length; i++) { + if (mostSpecificType.isAssignableFrom(types[i])) { mostSpecificType = types[i]; mostSpecificIndex = i; } @@ -864,66 +655,49 @@ public final class GenericsUtil return genericTypes[mostSpecificIndex]; } - private static Class<?>[] getClassTypes(Class<?>[] rawTypes) - { + private static Class<?>[] getClassTypes(Class<?>[] rawTypes) { List<Class<?>> classTypes = new ArrayList<Class<?>>(); - for (Class<?> rawType : rawTypes) - { - if (!rawType.isInterface()) - { + for (Class<?> rawType : rawTypes) { + if (!rawType.isInterface()) { classTypes.add(rawType); } } return classTypes.toArray(new Class[classTypes.size()]); } - private static Type resolveTypeVariable(TypeVariable<?> variable, Type actualType, Collection<TypeVariable<?>> seen) - { - if (actualType == null) - { + private static Type resolveTypeVariable(TypeVariable<?> variable, Type actualType, Collection<TypeVariable<?>> seen) { + if (actualType == null) { return variable; } Class<?> declaringClass = getDeclaringClass(variable.getGenericDeclaration()); Class<?> actualClass = getRawType(actualType); - if (actualClass == declaringClass) - { + if (actualClass == declaringClass) { return resolveTypeVariable(variable, variable.getGenericDeclaration(), getParameterizedType(actualType), seen); - } - else if (actualClass.isAssignableFrom(declaringClass)) - { + } else if (actualClass.isAssignableFrom(declaringClass)) { Class<?> directSubclass = getDirectSubclass(declaringClass, actualClass); Type[] typeArguments = resolveTypeArguments(directSubclass, actualType); Type directSubtype = new OwbParametrizedTypeImpl(directSubclass.getDeclaringClass(), directSubclass, typeArguments); return resolveTypeVariable(variable, directSubtype, seen); - } - else // if (declaringClass.isAssignableFrom(actualClass)) - { + } else // if (declaringClass.isAssignableFrom(actualClass)) + { Type genericSuperclass = getGenericSuperclass(actualClass, declaringClass); - if (genericSuperclass == null) - { + if (genericSuperclass == null) { return variable; - } - else if (genericSuperclass instanceof Class) - { + } else if (genericSuperclass instanceof Class) { // special handling for type erasure - Class<?> superclass = (Class<?>)genericSuperclass; + Class<?> superclass = (Class<?>) genericSuperclass; genericSuperclass = new OwbParametrizedTypeImpl(superclass.getDeclaringClass(), superclass, getRawTypes(superclass.getTypeParameters())); - } - else - { + } else { ParameterizedType genericSupertype = getParameterizedType(genericSuperclass); Type[] typeArguments = resolveTypeArguments(getParameterizedType(actualType), genericSupertype); genericSuperclass = new OwbParametrizedTypeImpl(genericSupertype.getOwnerType(), genericSupertype.getRawType(), typeArguments); } Type resolvedType = resolveTypeVariable(variable, genericSuperclass, seen); - if (resolvedType instanceof TypeVariable) - { - TypeVariable<?> resolvedTypeVariable = (TypeVariable<?>)resolvedType; + if (resolvedType instanceof TypeVariable) { + TypeVariable<?> resolvedTypeVariable = (TypeVariable<?>) resolvedType; TypeVariable<?>[] typeParameters = actualClass.getTypeParameters(); - for (int i = 0; i < typeParameters.length; i++) - { - if (typeParameters[i].getName().equals(resolvedTypeVariable.getName())) - { + for (int i = 0; i < typeParameters.length; i++) { + if (typeParameters[i].getName().equals(resolvedTypeVariable.getName())) { resolvedType = getParameterizedType(actualType).getActualTypeArguments()[i]; break; } @@ -933,45 +707,30 @@ public final class GenericsUtil } } - private static Class<?> getDeclaringClass(GenericDeclaration declaration) - { - if (declaration instanceof Class) - { - return (Class<?>)declaration; - } - else if (declaration instanceof Member) - { - return ((Member)declaration).getDeclaringClass(); - } - else - { + private static Class<?> getDeclaringClass(GenericDeclaration declaration) { + if (declaration instanceof Class) { + return (Class<?>) declaration; + } else if (declaration instanceof Member) { + return ((Member) declaration).getDeclaringClass(); + } else { throw new IllegalArgumentException("Unsupported type " + declaration.getClass()); } } private static Type resolveTypeVariable(TypeVariable<?> variable, GenericDeclaration declaration, ParameterizedType type, - Collection<TypeVariable<?>> seen) - { + Collection<TypeVariable<?>> seen) { int index = getIndex(declaration, variable); - if (declaration instanceof Class) - { - if (index >= 0) - { + if (declaration instanceof Class) { + if (index >= 0) { return type.getActualTypeArguments()[index]; - } - else - { + } else { index = getIndex(type, variable); - if (index >= 0) - { + if (index >= 0) { return declaration.getTypeParameters()[index]; } } - } - else - { - if (seen.contains(variable)) - { + } else { + if (seen.contains(variable)) { return null; } seen.add(variable); @@ -981,34 +740,26 @@ public final class GenericsUtil } return variable; } - - private static int getIndex(GenericDeclaration declaration, TypeVariable<?> variable) - { + + private static int getIndex(GenericDeclaration declaration, TypeVariable<?> variable) { Type[] typeParameters = declaration.getTypeParameters(); - for (int i = 0; i < typeParameters.length; i++) - { - if (typeParameters[i] instanceof TypeVariable) - { - TypeVariable<?> variableArgument = (TypeVariable<?>)typeParameters[i]; - if (variableArgument.getName().equals(variable.getName())) - { + for (int i = 0; i < typeParameters.length; i++) { + if (typeParameters[i] instanceof TypeVariable) { + TypeVariable<?> variableArgument = (TypeVariable<?>) typeParameters[i]; + if (variableArgument.getName().equals(variable.getName())) { return i; } } } return -1; } - - private static int getIndex(ParameterizedType type, TypeVariable<?> variable) - { + + private static int getIndex(ParameterizedType type, TypeVariable<?> variable) { Type[] actualTypeArguments = type.getActualTypeArguments(); - for (int i = 0; i < actualTypeArguments.length; i++) - { - if (actualTypeArguments[i] instanceof TypeVariable) - { - TypeVariable<?> variableArgument = (TypeVariable<?>)actualTypeArguments[i]; - if (variableArgument.getName().equals(variable.getName())) - { + for (int i = 0; i < actualTypeArguments.length; i++) { + if (actualTypeArguments[i] instanceof TypeVariable) { + TypeVariable<?> variableArgument = (TypeVariable<?>) actualTypeArguments[i]; + if (variableArgument.getName().equals(variable.getName())) { return i; } } @@ -1016,51 +767,35 @@ public final class GenericsUtil return -1; } - private static Class<?> getDirectSubclass(Class<?> declaringClass, Class<?> actualClass) - { - if (actualClass.isInterface()) - { + private static Class<?> getDirectSubclass(Class<?> declaringClass, Class<?> actualClass) { + if (actualClass.isInterface()) { Class<?> subclass = declaringClass; - for (Class<?> iface: declaringClass.getInterfaces()) - { - if (iface == actualClass) - { + for (Class<?> iface : declaringClass.getInterfaces()) { + if (iface == actualClass) { return subclass; } - if (actualClass.isAssignableFrom(iface)) - { + if (actualClass.isAssignableFrom(iface)) { subclass = iface; - } - else - { + } else { subclass = declaringClass.getSuperclass(); } } return getDirectSubclass(subclass, actualClass); - } - else - { + } else { Class<?> directSubclass = declaringClass; - while (directSubclass.getSuperclass() != actualClass) - { + while (directSubclass.getSuperclass() != actualClass) { directSubclass = directSubclass.getSuperclass(); } return directSubclass; } } - private static Type getGenericSuperclass(Class<?> subclass, Class<?> superclass) - { - if (!superclass.isInterface()) - { + private static Type getGenericSuperclass(Class<?> subclass, Class<?> superclass) { + if (!superclass.isInterface()) { return subclass.getGenericSuperclass(); - } - else - { - for (Type genericInterface: subclass.getGenericInterfaces()) - { - if (getRawType(genericInterface) == superclass) - { + } else { + for (Type genericInterface : subclass.getGenericInterfaces()) { + if (getRawType(genericInterface) == superclass) { return genericInterface; } } @@ -1068,87 +803,64 @@ public final class GenericsUtil return superclass; } - private static Type[] resolveTypeArguments(Class<?> subclass, Type supertype) - { - if (supertype instanceof ParameterizedType) - { - ParameterizedType parameterizedSupertype = (ParameterizedType)supertype; + private static Type[] resolveTypeArguments(Class<?> subclass, Type supertype) { + if (supertype instanceof ParameterizedType) { + ParameterizedType parameterizedSupertype = (ParameterizedType) supertype; return resolveTypeArguments(subclass, parameterizedSupertype); - } - else - { + } else { return subclass.getTypeParameters(); } } - private static Type[] resolveTypeArguments(Class<?> subclass, ParameterizedType parameterizedSupertype) - { + private static Type[] resolveTypeArguments(Class<?> subclass, ParameterizedType parameterizedSupertype) { Type genericSuperclass = getGenericSuperclass(subclass, getRawType(parameterizedSupertype)); - if (!(genericSuperclass instanceof ParameterizedType)) - { + if (!(genericSuperclass instanceof ParameterizedType)) { return subclass.getTypeParameters(); } - ParameterizedType parameterizedSuperclass = (ParameterizedType)genericSuperclass; + ParameterizedType parameterizedSuperclass = (ParameterizedType) genericSuperclass; Type[] typeParameters = subclass.getTypeParameters(); Type[] actualTypeArguments = parameterizedSupertype.getActualTypeArguments(); return resolveTypeArguments(parameterizedSuperclass, typeParameters, actualTypeArguments); } - private static Type[] resolveTypeArguments(ParameterizedType subtype, ParameterizedType parameterizedSupertype) - { + private static Type[] resolveTypeArguments(ParameterizedType subtype, ParameterizedType parameterizedSupertype) { return resolveTypeArguments(getParameterizedType(getRawType(subtype)), parameterizedSupertype.getActualTypeArguments(), subtype.getActualTypeArguments()); } - private static Type[] resolveTypeArguments(ParameterizedType parameterizedType, Type[] typeParameters, Type[] actualTypeArguments) - { + private static Type[] resolveTypeArguments(ParameterizedType parameterizedType, Type[] typeParameters, Type[] actualTypeArguments) { Type[] resolvedTypeArguments = new Type[typeParameters.length]; - for (int i = 0; i < typeParameters.length; i++) - { + for (int i = 0; i < typeParameters.length; i++) { resolvedTypeArguments[i] = resolveTypeArgument(parameterizedType, typeParameters[i], actualTypeArguments); } return resolvedTypeArguments; } - private static Type resolveTypeArgument(ParameterizedType parameterizedType, Type typeParameter, Type[] actualTypeArguments) - { - if (typeParameter instanceof TypeVariable) - { - TypeVariable<?> variable = (TypeVariable<?>)typeParameter; + private static Type resolveTypeArgument(ParameterizedType parameterizedType, Type typeParameter, Type[] actualTypeArguments) { + if (typeParameter instanceof TypeVariable) { + TypeVariable<?> variable = (TypeVariable<?>) typeParameter; int index = getIndex(parameterizedType, variable); - if (index == -1) - { + if (index == -1) { return typeParameter; - } - else - { + } else { return actualTypeArguments[index]; } - } - else if (typeParameter instanceof GenericArrayType) - { - GenericArrayType array = (GenericArrayType)typeParameter; + } else if (typeParameter instanceof GenericArrayType) { + GenericArrayType array = (GenericArrayType) typeParameter; return createArrayType(resolveTypeArgument(parameterizedType, array.getGenericComponentType(), actualTypeArguments)); - } - else - { + } else { return typeParameter; } } - - private static Type createArrayType(Type componentType) - { - if (componentType instanceof Class) - { - return Array.newInstance((Class<?>)componentType, 0).getClass(); - } - else - { + + private static Type createArrayType(Type componentType) { + if (componentType instanceof Class) { + return Array.newInstance((Class<?>) componentType, 0).getClass(); + } else { return new OwbGenericArrayTypeImpl(componentType); } } - public static Type resolveType(ParameterizedType parameterizedType, Type metadataType) - { + public static Type resolveType(ParameterizedType parameterizedType, Type metadataType) { return resolveType(parameterizedType, metadataType, newSeenList()); } }
Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbGenericArrayTypeImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbGenericArrayTypeImpl.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbGenericArrayTypeImpl.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbGenericArrayTypeImpl.java Wed Jan 31 20:10:03 2018 @@ -22,54 +22,43 @@ import java.lang.reflect.GenericArrayTyp import java.lang.reflect.Type; -public class OwbGenericArrayTypeImpl implements GenericArrayType -{ +public class OwbGenericArrayTypeImpl implements GenericArrayType { private Type componentType; - - public OwbGenericArrayTypeImpl(Type componentType) - { + + public OwbGenericArrayTypeImpl(Type componentType) { this.componentType = componentType; } @Override - public Type getGenericComponentType() - { + public Type getGenericComponentType() { return componentType; } - + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override - public int hashCode() - { - return componentType.hashCode(); + public int hashCode() { + return componentType.hashCode(); } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override - public boolean equals(Object obj) - { - if (this == obj) - { - return true; - } - else if (obj instanceof GenericArrayType) - { - return ((GenericArrayType)obj).getGenericComponentType().equals(componentType); - } - else - { - return false; - } - + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj instanceof GenericArrayType) { + return ((GenericArrayType) obj).getGenericComponentType().equals(componentType); + } else { + return false; + } + } - public String toString() - { + public String toString() { return componentType + "[]"; } } Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbParametrizedTypeImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbParametrizedTypeImpl.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbParametrizedTypeImpl.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbParametrizedTypeImpl.java Wed Jan 31 20:10:03 2018 @@ -24,115 +24,102 @@ import java.util.Arrays; /** * Custom parametrized type implementation. - * @version $Rev: 1621935 $ $Date: 2014-09-02 09:07:32 +0200 (Tue, 02 Sep 2014) $ * + * @version $Rev: 1621935 $ $Date: 2014-09-02 09:07:32 +0200 (Tue, 02 Sep 2014) $ */ -public class OwbParametrizedTypeImpl implements ParameterizedType -{ - /**Owner type*/ +public class OwbParametrizedTypeImpl implements ParameterizedType { + /** + * Owner type + */ private final Type owner; - - /**Raw type*/ + + /** + * Raw type + */ private final Type rawType; - - /**Actual type arguments*/ + + /** + * Actual type arguments + */ private final Type[] types; /** * New instance. + * * @param owner owner - * @param raw raw + * @param raw raw */ - public OwbParametrizedTypeImpl(Type owner, Type raw, Type... types) - { + public OwbParametrizedTypeImpl(Type owner, Type raw, Type... types) { this.owner = owner; rawType = raw; this.types = types; } - + @Override - public Type[] getActualTypeArguments() - { + public Type[] getActualTypeArguments() { return types.clone(); } - + @Override - public Type getOwnerType() - { + public Type getOwnerType() { return owner; } @Override - public Type getRawType() - { + public Type getRawType() { return rawType; } - - + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override - public int hashCode() - { - return Arrays.hashCode(types) ^ (owner == null ? 0 : owner.hashCode()) ^ (rawType == null ? 0 : rawType.hashCode()); + public int hashCode() { + return Arrays.hashCode(types) ^ (owner == null ? 0 : owner.hashCode()) ^ (rawType == null ? 0 : rawType.hashCode()); } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override - public boolean equals(Object obj) - { - if (this == obj) - { - return true; - } - else if (obj instanceof ParameterizedType) - { - ParameterizedType that = (ParameterizedType) obj; - Type thatOwnerType = that.getOwnerType(); - Type thatRawType = that.getRawType(); - return (owner == null ? thatOwnerType == null : owner.equals(thatOwnerType)) - && (rawType == null ? thatRawType == null : rawType.equals(thatRawType)) - && Arrays.equals(types, that.getActualTypeArguments()); - } - else - { - return false; - } - + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj instanceof ParameterizedType) { + ParameterizedType that = (ParameterizedType) obj; + Type thatOwnerType = that.getOwnerType(); + Type thatRawType = that.getRawType(); + return (owner == null ? thatOwnerType == null : owner.equals(thatOwnerType)) + && (rawType == null ? thatRawType == null : rawType.equals(thatRawType)) + && Arrays.equals(types, that.getActualTypeArguments()); + } else { + return false; + } + } - public String toString() - { + public String toString() { StringBuilder buffer = new StringBuilder(); buffer.append(((Class<?>) rawType).getName()); Type[] actualTypes = getActualTypeArguments(); - if(actualTypes.length > 0) - { + if (actualTypes.length > 0) { buffer.append("<"); int length = actualTypes.length; - for(int i=0;i<length;i++) - { - if (actualTypes[i] instanceof Class) - { - buffer.append(((Class<?>)actualTypes[i]).getSimpleName()); - } - else - { + for (int i = 0; i < length; i++) { + if (actualTypes[i] instanceof Class) { + buffer.append(((Class<?>) actualTypes[i]).getSimpleName()); + } else { buffer.append(actualTypes[i].toString()); } - if(i != actualTypes.length-1) - { + if (i != actualTypes.length - 1) { buffer.append(", "); } } - + buffer.append(">"); } - + return buffer.toString(); } } Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbWildcardTypeImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbWildcardTypeImpl.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbWildcardTypeImpl.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/OwbWildcardTypeImpl.java Wed Jan 31 20:10:03 2018 @@ -21,79 +21,58 @@ package org.apache.aries.blueprint.utils import java.lang.reflect.Type; import java.lang.reflect.WildcardType; -public class OwbWildcardTypeImpl implements WildcardType -{ +public class OwbWildcardTypeImpl implements WildcardType { private Type[] upperBounds; private Type[] lowerBounds; - - public OwbWildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) - { + + public OwbWildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { this.upperBounds = upperBounds.clone(); this.lowerBounds = lowerBounds.clone(); } @Override - public Type[] getUpperBounds() - { + public Type[] getUpperBounds() { return upperBounds.clone(); } @Override - public Type[] getLowerBounds() - { + public Type[] getLowerBounds() { return lowerBounds.clone(); } - public String toString() - { + public String toString() { StringBuilder buffer = new StringBuilder("?"); - if (upperBounds.length > 0) - { + if (upperBounds.length > 0) { buffer.append(" extends"); boolean first = true; - for (Type upperBound: upperBounds) - { - if (first) - { + for (Type upperBound : upperBounds) { + if (first) { first = false; - } - else - { + } else { buffer.append(','); } buffer.append(' '); - if (upperBound instanceof Class) - { - buffer.append(((Class<?>)upperBound).getSimpleName()); - } - else - { + if (upperBound instanceof Class) { + buffer.append(((Class<?>) upperBound).getSimpleName()); + } else { buffer.append(upperBound); } } } - if (lowerBounds.length > 0) - { + if (lowerBounds.length > 0) { buffer.append(" super"); boolean first = true; - for (Type lowerBound: lowerBounds) - { - if (first) - { + for (Type lowerBound : lowerBounds) { + if (first) { first = false; - } - else - { + } else { buffer.append(','); } buffer.append(' '); - if (lowerBound instanceof Class) - { - buffer.append(((Class<?>)lowerBound).getSimpleName()); - } - else - { + if (lowerBound instanceof Class) { + buffer.append(((Class<?>) lowerBound).getSimpleName()); + } else { buffer.append(lowerBound); } } Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/TypeInference.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/TypeInference.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/TypeInference.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/generics/TypeInference.java Wed Jan 31 20:10:03 2018 @@ -168,7 +168,7 @@ public class TypeInference { if (oneTypes.length != twoTypes.length) return false; - for (int i=0; i<oneTypes.length; i++) { + for (int i = 0; i < oneTypes.length; i++) { if (!oneTypes[i].equals(twoTypes[i])) return false; } Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/threading/RWLock.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/threading/RWLock.java?rev=1822826&r1=1822825&r2=1822826&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/threading/RWLock.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/threading/RWLock.java Wed Jan 31 20:10:03 2018 @@ -23,51 +23,46 @@ import java.util.concurrent.locks.Reentr import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; -public class RWLock -{ - private ReentrantReadWriteLock _lock = new ReentrantReadWriteLock(); - - public <T> T runReadOperation(Callable<T> call) throws Exception - { - ReadLock rl = _lock.readLock(); - rl.lock(); - try { - return call.call(); - } finally { - rl.unlock(); +public class RWLock { + private ReentrantReadWriteLock _lock = new ReentrantReadWriteLock(); + + public <T> T runReadOperation(Callable<T> call) throws Exception { + ReadLock rl = _lock.readLock(); + rl.lock(); + try { + return call.call(); + } finally { + rl.unlock(); + } } - } - - public void runReadOperation(Runnable r) - { - ReadLock rl = _lock.readLock(); - rl.lock(); - try { - r.run(); - } finally { - rl.unlock(); + + public void runReadOperation(Runnable r) { + ReadLock rl = _lock.readLock(); + rl.lock(); + try { + r.run(); + } finally { + rl.unlock(); + } } - } - - public <T> T runWriteOperation(Callable<T> call) throws Exception - { - WriteLock wl = _lock.writeLock(); - wl.lock(); - try { - return call.call(); - } finally { - wl.unlock(); + + public <T> T runWriteOperation(Callable<T> call) throws Exception { + WriteLock wl = _lock.writeLock(); + wl.lock(); + try { + return call.call(); + } finally { + wl.unlock(); + } } - } - - public void runWriteOperation(Runnable r) - { - WriteLock wl = _lock.writeLock(); - wl.lock(); - try { - r.run(); - } finally { - wl.unlock(); + + public void runWriteOperation(Runnable r) { + WriteLock wl = _lock.writeLock(); + wl.lock(); + try { + r.run(); + } finally { + wl.unlock(); + } } - } } \ No newline at end of file
