private static Type getArrayType(Type componentType) { Contracts.assertNotNull( componentType, "componentType" ); if ( componentType instanceof Class<?> ) { return Array.newInstance( (Class<?>) componentType, 0 ).getClass(); } return genericArrayType( componentType ); }
private static Type resolveTypeVariables(Type type, Type subtype) { // TODO: need to support other types in future, e.g. T[], etc. if ( !( type instanceof ParameterizedType ) ) { return type; } Map<Type, Type> actualTypeArgumentsByParameter = getActualTypeArgumentsByParameter( type, subtype ); Class<?> rawType = getErasedReferenceType( type ); return parameterizeClass( rawType, actualTypeArgumentsByParameter ); }
private static Map<Type, Type> getActualTypeArgumentsByParameter(Type... types) { // TODO: return Map<TypeVariable<Class<?>>, Type> somehow Map<Type, Type> actualTypeArgumentsByParameter = new LinkedHashMap<>(); for ( Type type : types ) { actualTypeArgumentsByParameter.putAll( getActualTypeArgumentsByParameterInternal( type ) ); } return normalize( actualTypeArgumentsByParameter ); }
private static boolean isSuperAssignable(Type supertype, Type type) { Type superclass = getResolvedSuperclass( type ); if ( superclass != null && isAssignable( supertype, superclass ) ) { return true; } for ( Type interphace : getResolvedInterfaces( type ) ) { if ( isAssignable( supertype, interphace ) ) { return true; } } return false; }
return isClassAssignable( (Class<?>) supertype, (Class<?>) type ); return isAssignable( supertype, ( (ParameterizedType) type ).getRawType() ); return isTypeVariableAssignable( supertype, (TypeVariable<?>) type ); return isAssignable( getComponentType( supertype ), getComponentType( type ) ); return isArraySupertype( (Class<?>) supertype ); return isClassAssignableToWildcardType( (Class<?>) supertype, (WildcardType) type ); return isSuperAssignable( supertype, type ); return isParameterizedTypeAssignable( (ParameterizedType) supertype, (ParameterizedType) type ); return isTypeVariableAssignable( supertype, (TypeVariable<?>) type ); if ( isArray( type ) ) { return isAssignable( getComponentType( supertype ), getComponentType( type ) ); return isWildcardTypeAssignable( (WildcardType) supertype, type );
/** * Gets the erased type of the specified type. * * @param type the type to perform erasure on * * @return the erased type, never a parameterized type nor a type variable * * @see <a href="http://docs.oracle.com/javase/specs/jls/se6/html/typesValues.html#4.6">4.6 Type Erasure</a> */ public static Type getErasedType(Type type) { // the erasure of a parameterized type G<T1, ... ,Tn> is |G| if ( type instanceof ParameterizedType ) { Type rawType = ( (ParameterizedType) type ).getRawType(); return getErasedType( rawType ); } // TODO: the erasure of a nested type T.C is |T|.C // the erasure of an array type T[] is |T|[] if ( isArray( type ) ) { Type componentType = getComponentType( type ); Type erasedComponentType = getErasedType( componentType ); return getArrayType( erasedComponentType ); } // the erasure of a type variable is the erasure of its leftmost bound if ( type instanceof TypeVariable<?> ) { Type[] bounds = ( (TypeVariable<?>) type ).getBounds(); return getErasedType( bounds[0] ); } // the erasure of every other type is the type itself return type; }
public static Type extractConstraintValidatorTypeArgumentType(Class<? extends ConstraintValidator<?, ?>> validator, int typeArgumentIndex) { Map<Type, Type> resolvedTypes = new HashMap<>(); Type constraintValidatorType = resolveTypes( resolvedTypes, validator ); //we now have all bind from a type to its resolution at one level Type type = ( (ParameterizedType) constraintValidatorType ).getActualTypeArguments()[typeArgumentIndex]; if ( type == null ) { throw LOG.getNullIsAnInvalidTypeForAConstraintValidatorException(); } else if ( type instanceof GenericArrayType ) { type = TypeHelper.getArrayType( TypeHelper.getComponentType( type ) ); } while ( resolvedTypes.containsKey( type ) ) { type = resolvedTypes.get( type ); } //FIXME raise an exception if validatorType is not a class return type; }
private static Type getResolvedSuperclass(Type type) { Contracts.assertNotNull( type, "type" ); Class<?> rawType = getErasedReferenceType( type ); Type supertype = rawType.getGenericSuperclass(); if ( supertype == null ) { return null; } return resolveTypeVariables( supertype, type ); }
/** * Indicates whether the given type represents a collection of elements or not (i.e. whether it is an * {@code Iterable}, {@code Map} or array type). */ public static boolean isCollection(Type type) { return isIterable( type ) || isMap( type ) || TypeHelper.isArray( type ); }
public static boolean isInstance(Type type, Object object) { return getErasedReferenceType( type ).isInstance( object ); }
public static Class<?> getErasedReferenceType(Type type) { Contracts.assertTrue( isReferenceType( type ), "type is not a reference type: %s", type ); return (Class<?>) getErasedType( type ); }
private static <T> ParameterizedType parameterizeClassCapture(Class<T> type, Map<Type, Type> actualTypeArgumentsByParameter) { // TODO: actualTypeArgumentsByParameter should be Map<TypeVariable<Class<T>>, Type> TypeVariable<Class<T>>[] typeParameters = type.getTypeParameters(); Type[] actualTypeArguments = new Type[typeParameters.length]; for ( int i = 0; i < typeParameters.length; i++ ) { TypeVariable<Class<T>> typeParameter = typeParameters[i]; Type actualTypeArgument = actualTypeArgumentsByParameter.get( typeParameter ); if ( actualTypeArgument == null ) { throw LOG.getMissingActualTypeArgumentForTypeParameterException( typeParameter ); } actualTypeArguments[i] = actualTypeArgument; } return parameterizedType( getErasedReferenceType( type ), actualTypeArguments ); }
/** * Determines the type of the elements of an {@code Iterable}, array or the value of a {@code Map}. */ public static Type getCollectionElementType(Type type) { Type indexedType = null; if ( isIterable( type ) && type instanceof ParameterizedType ) { ParameterizedType paramType = (ParameterizedType) type; indexedType = paramType.getActualTypeArguments()[0]; } else if ( isMap( type ) && type instanceof ParameterizedType ) { ParameterizedType paramType = (ParameterizedType) type; indexedType = paramType.getActualTypeArguments()[1]; } else if ( TypeHelper.isArray( type ) ) { indexedType = TypeHelper.getComponentType( type ); } return indexedType; }
private Set<ValueExtractorDescriptor> getRuntimeAndContainerElementCompliantValueExtractorsFromPossibleCandidates(Type declaredType, TypeVariable<?> typeParameter, Class<?> runtimeType, Collection<ValueExtractorDescriptor> valueExtractorCandidates) { if ( nonContainerTypes.contains( runtimeType ) ) { return Collections.emptySet(); } ValueExtractorCacheKey cacheKey = new ValueExtractorCacheKey( runtimeType, typeParameter ); Set<ValueExtractorDescriptor> valueExtractorDescriptors = possibleValueExtractorsByRuntimeTypeAndTypeParameter.get( cacheKey ); if ( valueExtractorDescriptors != null ) { return valueExtractorDescriptors; } boolean isInternal = TypeVariables.isInternal( typeParameter ); Class<?> erasedDeclaredType = TypeHelper.getErasedReferenceType( declaredType ); Set<ValueExtractorDescriptor> possibleValueExtractors = valueExtractorCandidates .stream() .filter( e -> TypeHelper.isAssignable( e.getContainerType(), runtimeType ) ) .filter( extractorDescriptor -> checkValueExtractorTypeCompatibility( typeParameter, isInternal, erasedDeclaredType, extractorDescriptor ) ).collect( Collectors.toSet() ); valueExtractorDescriptors = getMaximallySpecificValueExtractors( possibleValueExtractors ); if ( valueExtractorDescriptors.isEmpty() ) { nonContainerTypes.add( runtimeType ); return Collections.emptySet(); } Set<ValueExtractorDescriptor> valueExtractorDescriptorsToCache = CollectionHelper.toImmutableSet( valueExtractorDescriptors ); Set<ValueExtractorDescriptor> cachedValueExtractorDescriptors = possibleValueExtractorsByRuntimeTypeAndTypeParameter.putIfAbsent( cacheKey, valueExtractorDescriptorsToCache ); return cachedValueExtractorDescriptors != null ? cachedValueExtractorDescriptors : valueExtractorDescriptorsToCache; }
valueType = ReflectionHelper.unBoxedType( valueType ); if ( !TypeHelper.isAssignable( TypeHelper.getErasedType( parameterMetaData.getType() ), valueType ) ) {
public ElementDescriptorImpl(Type type, Set<ConstraintDescriptorImpl<?>> constraintDescriptors, boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) { this.type = (Class<?>) TypeHelper.getErasedType( type ); this.constraintDescriptors = Collections.unmodifiableSet( constraintDescriptors ); this.defaultGroupSequenceRedefined = defaultGroupSequenceRedefined; this.defaultGroupSequence = Collections.unmodifiableList( defaultGroupSequence ); }
Class<? extends ValueExtractor> extractorImplementationType) { AnnotatedType containerType = valueExtractorDefinition.getAnnotatedActualTypeArguments()[0]; Class<?> containerTypeRaw = (Class<?>) TypeHelper.getErasedType( containerType.getType() ); int i = 0; for ( AnnotatedType typeArgument : parameterizedExtractedType.getAnnotatedActualTypeArguments() ) { if ( !TypeHelper.isUnboundWildcard( typeArgument.getType() ) ) { throw LOG.getOnlyUnboundWildcardTypeArgumentsSupportedForContainerTypeOfValueExtractorException( extractorImplementationType );
private Type getContainerElementType() { if ( configuredType instanceof ParameterizedType ) { return ( (ParameterizedType) configuredType ).getActualTypeArguments()[index]; } else { return TypeHelper.getComponentType( configuredType ); } }
return isClassAssignable( (Class<?>) supertype, (Class<?>) type ); return isAssignable( supertype, ( (ParameterizedType) type ).getRawType() ); return isTypeVariableAssignable( supertype, (TypeVariable<?>) type ); return isAssignable( getComponentType( supertype ), getComponentType( type ) ); return isArraySupertype( (Class<?>) supertype ); return isClassAssignableToWildcardType( (Class<?>) supertype, (WildcardType) type ); return isSuperAssignable( supertype, type ); return isParameterizedTypeAssignable( (ParameterizedType) supertype, (ParameterizedType) type ); return isTypeVariableAssignable( supertype, (TypeVariable<?>) type ); if ( isArray( type ) ) { return isAssignable( getComponentType( supertype ), getComponentType( type ) ); return isWildcardTypeAssignable( (WildcardType) supertype, type );
Type rawType = ( (ParameterizedType) type ).getRawType(); return getErasedType( rawType ); if ( isArray( type ) ) { Type componentType = getComponentType( type ); Type erasedComponentType = getErasedType( componentType ); return getArrayType( erasedComponentType ); Type[] bounds = ( (TypeVariable<?>) type ).getBounds(); return getErasedType( bounds[0] ); Type[] upperBounds = ( (WildcardType) type ).getUpperBounds(); return getErasedType( upperBounds[0] );