/** * 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 ); }
if ( isArray( type ) ) { return isAssignable( getComponentType( supertype ), getComponentType( 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 ); }
if ( isArray( type ) ) { return isAssignable( getComponentType( supertype ), getComponentType( type ) );
iter = ( (Iterable<?>) value ).iterator(); else if ( TypeHelper.isArray( type ) ) { List<?> arrayList = Arrays.asList( value ); iter = arrayList.iterator();
/** * Indicates if the type is considered indexable (ie is a {@code List}, an array or a {@code Map}). * <p> * Note that it does not include {@code Set}s as they are not indexable. * * @param type the type to inspect. * * @return Returns true if the type is indexable. */ public static boolean isIndexable(Type type) { return isList( type ) || isMap( type ) || TypeHelper.isArray( type ); }
public ContainerElementConstraintMappingContext containerElement(ContainerElementTarget parent, TypeConstraintMappingContextImpl<?> typeContext, ConstraintLocation location) { // HV-1428 Container element support is disabled for arrays if ( TypeHelper.isArray( configuredType ) ) { throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( configuredType ); } if ( configuredType instanceof ParameterizedType ) { if ( ( (ParameterizedType) configuredType ).getActualTypeArguments().length > 1 ) { throw LOG.getNoTypeArgumentIndexIsGivenForTypeWithMultipleTypeArgumentsException( configuredType ); } } else if ( !TypeHelper.isArray( configuredType ) ) { throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( configuredType ); } return containerElement( parent, typeContext, location, 0 ); }
/** * Indicates if the type is considered indexable (ie is a {@code List}, an array or a {@code Map}). * <p> * Note that it does not include {@code Set}s as they are not indexable. * * @param type the type to inspect. * * @return Returns true if the type is indexable. */ public static boolean isIndexable(Type type) { return isList( type ) || isMap( type ) || TypeHelper.isArray( type ); }
/** * 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; }
/** * 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; }
iterable = ( (Iterable<?>) value ); else if ( TypeHelper.isArray( type ) ) { iterable = CollectionHelper.iterableFromArray( value );
/** * Called when processing cascaded constraints. This methods inspects the type of the cascaded constraints and in case * of a list or array creates an iterator in order to validate each element. * @param valueContext context object containing state about the currently validated instance * @param type the type of the cascaded field or property. * @param value the actual value. * * @return An iterator over the value of a cascaded property. */ private Iterator<?> createIteratorForCascadedValue(ValueContext<?, ?> valueContext, Type type, Object value) { Iterator<?> iter; if ( ReflectionHelper.isIterable( type ) ) { iter = ( (Iterable<?>) value ).iterator(); valueContext.markCurrentPropertyAsIterable(); } else if ( ReflectionHelper.isMap( type ) ) { Map<?, ?> map = (Map<?, ?>) value; iter = map.entrySet().iterator(); valueContext.markCurrentPropertyAsIterable(); } else if ( TypeHelper.isArray( type ) ) { List<?> arrayList = Arrays.asList( (Object[]) value ); iter = arrayList.iterator(); valueContext.markCurrentPropertyAsIterable(); } else { iter = Collections.singletonList( value ).iterator(); } return iter; }
ContainerElementConstraintMappingContext nestedContainerElement(int[] nestedIndexes) { if ( !( configuredType instanceof ParameterizedType ) && !( TypeHelper.isArray( configuredType ) ) ) { throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( configuredType ); } ContainerElementConstraintMappingContextImpl nestedContext = nestedContainerElementContexts.get( nestedIndexes[0] ); if ( nestedContext == null ) { nestedContext = new ContainerElementConstraintMappingContextImpl( typeContext, parentContainerElementTarget, ConstraintLocation.forTypeArgument( parentLocation, typeParameter, getContainerElementType() ), nestedIndexes[0] ); nestedContainerElementContexts.put( nestedIndexes[0], nestedContext ); } if ( nestedIndexes.length > 1 ) { return nestedContext.nestedContainerElement( Arrays.copyOfRange( nestedIndexes, 1, nestedIndexes.length ) ); } else { return nestedContext; } }
if ( TypeHelper.isArray( configuredType ) ) { throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( configuredType ); if ( !( configuredType instanceof ParameterizedType ) && !( TypeHelper.isArray( configuredType ) ) ) { throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( configuredType );
ContainerElementConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, ContainerElementTarget parentContainerElementTarget, ConstraintLocation parentLocation, int index) { super( typeContext.getConstraintMapping(), parentLocation.getTypeForValidatorResolution() ); this.typeContext = typeContext; this.parentContainerElementTarget = parentContainerElementTarget; this.parentLocation = parentLocation; this.configuredType = parentLocation.getTypeForValidatorResolution(); // HV-1428 Container element support is disabled for arrays if ( TypeHelper.isArray( configuredType ) ) { throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( configuredType ); } if ( configuredType instanceof ParameterizedType ) { TypeVariable<?>[] typeParameters = ReflectionHelper.getClassFromType( configuredType ).getTypeParameters(); if ( index > typeParameters.length - 1 ) { throw LOG.getInvalidTypeArgumentIndexException( configuredType, index ); } else { this.typeParameter = typeParameters[index]; } } else { typeParameter = new ArrayElement( configuredType ); } this.index = index; this.constraints = new HashSet<>(); this.nestedContainerElementContexts = new HashMap<>(); }
/** * 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; }
if ( isArray( type ) ) { Type componentType = getComponentType( type ); Type erasedComponentType = getErasedType( componentType );
ConstraintLocation parentConstraintLocation, Type enclosingType) { if ( TypeHelper.isArray( enclosingType ) ) { throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( enclosingType ); if ( !( enclosingType instanceof ParameterizedType ) && !TypeHelper.isArray( enclosingType ) ) { throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( enclosingType ); CollectionHelper.newHashMap( containerElementTypeConfigurationStaxBuilders.size() ); boolean isArray = TypeHelper.isArray( enclosingType ); TypeVariable<?>[] typeParameters = isArray ? new TypeVariable[0] : ReflectionHelper.getClassFromType( enclosingType ).getTypeParameters();