@Override public int compareTo(ConverterCacheKey other) { int result = this.sourceType.getResolvableType().toString().compareTo( other.sourceType.getResolvableType().toString()); if (result == 0) { result = this.targetType.getResolvableType().toString().compareTo( other.targetType.getResolvableType().toString()); } return result; } }
@Override public String toString() { StringBuilder builder = new StringBuilder(); for (Annotation ann : getAnnotations()) { builder.append("@").append(ann.annotationType().getName()).append(' '); } builder.append(getResolvableType().toString()); return builder.toString(); }
@Override public int compareTo(ConverterCacheKey other) { int result = this.sourceType.getResolvableType().toString().compareTo( other.sourceType.getResolvableType().toString()); if (result == 0) { result = this.targetType.getResolvableType().toString().compareTo( other.targetType.getResolvableType().toString()); } return result; } }
@Override public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return Optional.empty(); } else if (source instanceof Optional) { return source; } else if (targetType.getResolvableType().hasGenerics()) { Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType)); if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) || (target instanceof Collection && ((Collection<?>) target).isEmpty())) { return Optional.empty(); } return Optional.of(target); } else { return Optional.of(source); } }
public GenericTypeDescriptor(TypeDescriptor typeDescriptor) { super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations()); } }
@Override public String toString() { StringBuilder builder = new StringBuilder(); for (Annotation ann : getAnnotations()) { builder.append("@").append(ann.annotationType().getName()).append(' '); } builder.append(getResolvableType().toString()); return builder.toString(); }
/** * Narrows this {@link TypeDescriptor} by setting its type to the class of the * provided value. * <p>If the value is {@code null}, no narrowing is performed and this TypeDescriptor * is returned unchanged. * <p>Designed to be called by binding frameworks when they read property, field, * or method return values. Allows such frameworks to narrow a TypeDescriptor built * from a declared property, field, or method return value type. For example, a field * declared as {@code java.lang.Object} would be narrowed to {@code java.util.HashMap} * if it was set to a {@code java.util.HashMap} value. The narrowed TypeDescriptor * can then be used to convert the HashMap to some other type. Annotation and nested * type context is preserved by the narrowed copy. * @param value the value to use for narrowing this type descriptor * @return this TypeDescriptor narrowed (returns a copy with its type updated to the * class of the provided value) */ public TypeDescriptor narrow(@Nullable Object value) { if (value == null) { return this; } ResolvableType narrowed = ResolvableType.forType(value.getClass(), getResolvableType()); return new TypeDescriptor(narrowed, value.getClass(), getAnnotations()); }
/** * If this type is a {@link Map} and its key type is parameterized, * returns the map's key type. If the Map's key type is not parameterized, * returns {@code null} indicating the key type is not declared. * @return the Map key type, or {@code null} if this type is a Map * but its key type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Map} */ @Nullable public TypeDescriptor getMapKeyTypeDescriptor() { Assert.state(isMap(), "Not a [java.util.Map]"); return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(0)); }
/** * If this type is a {@link Map} and its value type is parameterized, * returns the map's value type. * <p>If the Map's value type is not parameterized, returns {@code null} * indicating the value type is not declared. * @return the Map value type, or {@code null} if this type is a Map * but its value type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Map} */ @Nullable public TypeDescriptor getMapValueTypeDescriptor() { Assert.state(isMap(), "Not a [java.util.Map]"); return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(1)); }
/** * If this type is an array, returns the array's component type. * If this type is a {@code Stream}, returns the stream's component type. * If this type is a {@link Collection} and it is parameterized, returns the Collection's element type. * If the Collection is not parameterized, returns {@code null} indicating the element type is not declared. * @return the array component type or Collection element type, or {@code null} if this type is a * Collection but its element type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Collection} or array type */ @Nullable public TypeDescriptor getElementTypeDescriptor() { if (getResolvableType().isArray()) { return new TypeDescriptor(getResolvableType().getComponentType(), null, getAnnotations()); } if (Stream.class.isAssignableFrom(getType())) { return getRelatedIfResolvable(this, getResolvableType().as(Stream.class).getGeneric(0)); } return getRelatedIfResolvable(this, getResolvableType().asCollection().getGeneric(0)); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.getResolvableType().hasGenerics()) { return this.conversionService.canConvert(sourceType, new GenericTypeDescriptor(targetType)); } else { return true; } }
public GenericTypeDescriptor(TypeDescriptor typeDescriptor) { super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations()); } }
/** * Cast this {@link TypeDescriptor} to a superclass or implemented interface * preserving annotations and nested type context. * @param superType the super type to cast to (can be {@code null}) * @return a new TypeDescriptor for the up-cast type * @throws IllegalArgumentException if this type is not assignable to the super-type * @since 3.2 */ @Nullable public TypeDescriptor upcast(@Nullable Class<?> superType) { if (superType == null) { return null; } Assert.isAssignable(superType, getType()); return new TypeDescriptor(getResolvableType().as(superType), superType, getAnnotations()); }
/** * If this type is a {@link Map} and its key type is parameterized, * returns the map's key type. If the Map's key type is not parameterized, * returns {@code null} indicating the key type is not declared. * @return the Map key type, or {@code null} if this type is a Map * but its key type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Map} */ @Nullable public TypeDescriptor getMapKeyTypeDescriptor() { Assert.state(isMap(), "Not a [java.util.Map]"); return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(0)); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (targetType.getResolvableType().hasGenerics()) { return this.conversionService.canConvert(sourceType, new GenericTypeDescriptor(targetType)); } else { return true; } }
/** * If this type is a {@link Map} and its value type is parameterized, * returns the map's value type. * <p>If the Map's value type is not parameterized, returns {@code null} * indicating the value type is not declared. * @return the Map value type, or {@code null} if this type is a Map * but its value type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Map} */ @Nullable public TypeDescriptor getMapValueTypeDescriptor() { Assert.state(isMap(), "Not a [java.util.Map]"); return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(1)); }
/** * If this type is an array, returns the array's component type. * If this type is a {@code Stream}, returns the stream's component type. * If this type is a {@link Collection} and it is parameterized, returns the Collection's element type. * If the Collection is not parameterized, returns {@code null} indicating the element type is not declared. * @return the array component type or Collection element type, or {@code null} if this type is a * Collection but its element type is not parameterized * @throws IllegalStateException if this type is not a {@code java.util.Collection} or array type */ @Nullable public TypeDescriptor getElementTypeDescriptor() { if (getResolvableType().isArray()) { return new TypeDescriptor(getResolvableType().getComponentType(), null, getAnnotations()); } if (Stream.class.isAssignableFrom(getType())) { return getRelatedIfResolvable(this, getResolvableType().as(Stream.class).getGeneric(0)); } return getRelatedIfResolvable(this, getResolvableType().asCollection().getGeneric(0)); }
/** * Cast this {@link TypeDescriptor} to a superclass or implemented interface * preserving annotations and nested type context. * @param superType the super type to cast to (can be {@code null}) * @return a new TypeDescriptor for the up-cast type * @throws IllegalArgumentException if this type is not assignable to the super-type * @since 3.2 */ @Nullable public TypeDescriptor upcast(@Nullable Class<?> superType) { if (superType == null) { return null; } Assert.isAssignable(superType, getType()); return new TypeDescriptor(getResolvableType().as(superType), superType, getAnnotations()); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { // Check raw type first... if (this.typeInfo.getTargetType() != targetType.getObjectType()) { return false; } // Full check for complex generic type match required? ResolvableType rt = targetType.getResolvableType(); if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) && !this.targetType.hasUnresolvableGenerics()) { return false; } return !(this.converter instanceof ConditionalConverter) || ((ConditionalConverter) this.converter).matches(sourceType, targetType); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { // Check raw type first... if (this.typeInfo.getTargetType() != targetType.getObjectType()) { return false; } // Full check for complex generic type match required? ResolvableType rt = targetType.getResolvableType(); if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) && !this.targetType.hasUnresolvableGenerics()) { return false; } return !(this.converter instanceof ConditionalConverter) || ((ConditionalConverter) this.converter).matches(sourceType, targetType); }