if ( subType instanceof TypeVariable<?> && isAssignableFrom( expand( subType ), expand( superType ) ) ) if ( !isAssignableFrom( expand( superType ), expand( subType ) ) )
/** * Determines if the given generic type represents a concrete type. * * @param literal The generic type * @return {@code true} if the generic type is concrete; otherwise {@code false} */ public static boolean isConcrete( final TypeLiteral<?> literal ) { return isConcrete( literal.getRawType() ); }
/** * Determines if the given generic type represents an implicit binding. * * @param literal The generic type * @return {@code true} if the generic type is implicit; otherwise {@code false} */ public static boolean isImplicit( final TypeLiteral<?> literal ) { return isImplicit( literal.getRawType() ); }
/** * Deduces the role type based on the given @{@link Requirement} and expected type. * * @param requirement The Plexus requirement * @param asType The expected type * @return "Best-fit" role type */ public static TypeLiteral<?> roleType( final Requirement requirement, final TypeLiteral<?> asType ) { final Type role = requirement.role(); if ( role != Object.class ) { return TypeLiteral.get( role ); } final Class<?> rawType = asType.getRawType(); if ( Map.class == rawType ) { // Map<String, T> --> T return TypeParameters.get( asType, 1 ); } if ( List.class == rawType ) { // List<T> --> T return TypeParameters.get( asType, 0 ); } return asType; }
/** * Get all type arguments from a generic type, for example {@code [Foo,Bar]} from {@code Map<Foo,Bar>}. * * @param typeLiteral The generic type * @return Array of type arguments */ public static TypeLiteral<?>[] get( final TypeLiteral<?> typeLiteral ) { final Type type = typeLiteral.getType(); if ( type instanceof ParameterizedType ) { final Type[] argumentTypes = ( (ParameterizedType) type ).getActualTypeArguments(); final TypeLiteral<?>[] argumentLiterals = new TypeLiteral[argumentTypes.length]; for ( int i = 0; i < argumentTypes.length; i++ ) { argumentLiterals[i] = expand( argumentTypes[i] ); } return argumentLiterals; } if ( type instanceof GenericArrayType ) { return new TypeLiteral[] { expand( ( (GenericArrayType) type ).getGenericComponentType() ) }; } return NO_TYPE_LITERALS; }
return isAssignableFrom( superArgs, subArgs ); return isAssignableFrom( new Type[] { superComponent }, new Type[] { subComponent } );
public final void configure( final Binder binder ) { // make sure we pick up the right super type parameter, i.e. Foo from AbstractTypeConverter<Foo> final TypeLiteral<?> superType = TypeLiteral.get( getClass() ).getSupertype( AbstractTypeConverter.class ); binder.convertToTypes( Matchers.only( TypeParameters.get( superType, 0 ) ), this ); } }
/** * Get an indexed type argument from a generic type, for example {@code Bar} from {@code Map<Foo,Bar>}. * * @param typeLiteral The generic type * @param index The argument index * @return Indexed type argument; {@code TypeLiteral<Object>} if the given type is a raw class */ public static TypeLiteral<?> get( final TypeLiteral<?> typeLiteral, final int index ) { final Type type = typeLiteral.getType(); if ( type instanceof ParameterizedType ) { return expand( ( (ParameterizedType) type ).getActualTypeArguments()[index] ); } if ( type instanceof GenericArrayType ) { if ( 0 == index ) { return expand( ( (GenericArrayType) type ).getGenericComponentType() ); } throw new ArrayIndexOutOfBoundsException( index ); } return OBJECT_TYPE_LITERAL; }
/** * Determines if the given raw type represents an implicit binding. * * @param clazz The raw type * @return {@code true} if the raw type is implicit; otherwise {@code false} */ public static boolean isImplicit( final Class<?> clazz ) { return isConcrete( clazz ) || clazz.isAnnotationPresent( ImplementedBy.class ) || clazz.isAnnotationPresent( ProvidedBy.class ); }