/** * @param type the type to check. * * @return Returns {@code true} if {@code type} is a iterable type, {@code false} otherwise. */ public static boolean isIterable(Type type) { if ( type instanceof Class && Iterable.class.isAssignableFrom( (Class<?>) type ) ) { return true; } if ( type instanceof ParameterizedType ) { return isIterable( ( (ParameterizedType) type ).getRawType() ); } if ( type instanceof WildcardType ) { Type[] upperBounds = ( (WildcardType) type ).getUpperBounds(); return upperBounds.length != 0 && isIterable( upperBounds[0] ); } return false; }
/** * @param type the type to check. * * @return Returns <code>true</code> if <code>type</code> is a iterable type, <code>false</code> otherwise. */ public static boolean isIterable(Type type) { if ( type instanceof Class && Iterable.class.isAssignableFrom( (Class<?>) type ) ) { return true; } if ( type instanceof ParameterizedType ) { return isIterable( ( (ParameterizedType) type ).getRawType() ); } if ( type instanceof WildcardType ) { Type[] upperBounds = ( (WildcardType) type ).getUpperBounds(); return upperBounds.length != 0 && isIterable( upperBounds[0] ); } return false; }
/** * 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 ); }
/** * 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 ( isIterable( type ) ) { iter = ( (Iterable<?>) value ).iterator();
/** * 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; }
if ( isIterable( type ) ) { iterable = ( (Iterable<?>) 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; }
private Set<MetaConstraint<?>> findTypeArgumentsConstraints(Member member, AnnotatedType annotatedType, boolean isCascaded) { Optional<AnnotatedType> typeParameter = getTypeParameter( annotatedType ); if ( !typeParameter.isPresent() ) { return Collections.emptySet(); } List<ConstraintDescriptorImpl<?>> constraintDescriptors = findTypeUseConstraints( member, typeParameter.get() ); if ( constraintDescriptors.isEmpty() ) { return Collections.emptySet(); } // HV-925 // We need to determine the validated type used for constraint validator resolution. // Iterables and maps need special treatment at this point, since the validated type is the type of the // specified type parameter. In the other cases the validated type is the parameterized type, eg Optional<String>. // In the latter case a value unwrapping has to occur Type validatedType = annotatedType.getType(); if ( ReflectionHelper.isIterable( annotatedType.getType() ) || ReflectionHelper.isMap( annotatedType.getType() ) ) { if ( !isCascaded ) { throw log.getTypeAnnotationConstraintOnIterableRequiresUseOfValidAnnotationException( member.getDeclaringClass(), member.getName() ); } validatedType = typeParameter.get().getType(); } return convertToTypeArgumentMetaConstraints( constraintDescriptors, member, validatedType ); }
if ( ReflectionHelper.isIterable( type ) || ReflectionHelper.isMap( type ) ) { Iterator<?> valueIter = Collections.singletonList( value ).iterator(); validateCascadedConstraint(