Java Code Examples for org.springframework.core.convert.TypeDescriptor#getObjectType()

The following examples show how to use org.springframework.core.convert.TypeDescriptor#getObjectType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormattingConversionService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 2
Source File: FormattingConversionService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 3
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 4
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 5
Source File: FormattingConversionService.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 6
Source File: FormattingConversionService.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 7
Source File: FormattingConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 8
Source File: FormattingConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 9
Source File: FallbackObjectToStringConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	Class<?> sourceClass = sourceType.getObjectType();
	if (String.class == sourceClass) {
		// no conversion required
		return false;
	}
	return (CharSequence.class.isAssignableFrom(sourceClass) ||
			StringWriter.class.isAssignableFrom(sourceClass) ||
			ObjectToObjectConverter.hasConversionMethodOrConstructor(sourceClass, String.class));
}
 
Example 10
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: BeanFactoryTypeConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canConvert(TypeDescriptor sourceTypeDescriptor, TypeDescriptor targetTypeDescriptor) {
	if (conversionService.canConvert(sourceTypeDescriptor, targetTypeDescriptor)) {
		return true;
	}
	// TODO: what does this mean? This method is not used in SpEL so probably ignorable?
	Class<?> sourceType = sourceTypeDescriptor.getObjectType();
	Class<?> targetType = targetTypeDescriptor.getObjectType();
	return canConvert(sourceType, targetType);
}
 
Example 12
Source File: BeanFactoryTypeConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean canConvert(TypeDescriptor sourceTypeDescriptor, TypeDescriptor targetTypeDescriptor) {
	if (conversionService.canConvert(sourceTypeDescriptor, targetTypeDescriptor)) {
		return true;
	}
	// TODO: what does this mean? This method is not used in SpEL so probably ignorable?
	Class<?> sourceType = sourceTypeDescriptor.getObjectType();
	Class<?> targetType = targetTypeDescriptor.getObjectType();
	return canConvert(sourceType, targetType);
}
 
Example 13
Source File: BeanFactoryTypeConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean canConvert(TypeDescriptor sourceTypeDescriptor, TypeDescriptor targetTypeDescriptor) {
	if (conversionService.canConvert(sourceTypeDescriptor, targetTypeDescriptor)) {
		return true;
	}
	// TODO: what does this mean? This method is not used in SpEL so probably ignorable?
	Class<?> sourceType = sourceTypeDescriptor.getObjectType();
	Class<?> targetType = targetTypeDescriptor.getObjectType();
	return canConvert(sourceType, targetType);
}
 
Example 14
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 15
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source File: ConversionServiceBeanSerializerModifier.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<BeanPropertyWriter> changeProperties(
        SerializationConfig config, BeanDescription beanDesc,
        List<BeanPropertyWriter> beanProperties) {

    // We need the BeanPropertyDefinition to get the related Field
    List<BeanPropertyDefinition> properties = beanDesc.findProperties();
    Map<String, BeanPropertyDefinition> propertyDefMap = new HashMap<String, BeanPropertyDefinition>();
    for (BeanPropertyDefinition property : properties) {
        propertyDefMap.put(property.getName(), property);
    }

    // iterate over bean's properties to configure serializers
    for (int i = 0; i < beanProperties.size(); i++) {
        BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
        Class<?> propertyType = beanPropertyWriter.getPropertyType();

        if (beanPropertyWriter.hasSerializer()) {
            continue;
        }

        // For conversion between collection, array, and map types,
        // ConversionService.canConvert() method will return 'true'
        // but better to delegate in default Jackson property writer for
        // right start and ends markers serialization and iteration
        if (propertyType.isArray()
                || Collection.class.isAssignableFrom(propertyType)
                || Map.class.isAssignableFrom(propertyType)) {

            // Don't set ConversionService serializer, let Jackson
            // use default Collection serializer
            continue;
        }
        else if (BindingResult.class.isAssignableFrom(propertyType)) {
            // Use BindingResultSerializer
            beanPropertyWriter.assignSerializer(bindingResultSerializer);
        }
        else {

            // ConversionService uses value Class plus related Field
            // annotations to be able to select the right converter,
            // so we must get/ the Field annotations for success
            // formatting
            BeanPropertyDefinition propertyDef = propertyDefMap
                    .get(beanPropertyWriter.getName());
            AnnotatedField annotatedField = propertyDef.getField();
            if (annotatedField == null) {
                continue;
            }
            AnnotatedElement annotatedEl = annotatedField.getAnnotated();

            // Field contains info about Annotations, info that
            // ConversionService uses for success formatting, use it if
            // available. Otherwise use the class of given value.
            TypeDescriptor sourceType = annotatedEl != null ? new TypeDescriptor(
                    (Field) annotatedEl) : TypeDescriptor
                    .valueOf(propertyType);

            TypeDescriptor targetType = TypeDescriptor
                    .valueOf(String.class);
            if (beanPropertyWriter.getSerializationType() != null) {
                targetType = TypeDescriptor.valueOf(beanPropertyWriter
                        .getSerializationType().getRawClass());
            }
            if (ObjectUtils.equals(sourceType, targetType)) {
                // No conversion needed
                continue;
            }
            else if (sourceType.getObjectType() == Object.class
                    && targetType.getObjectType() == String.class
                    && beanPropertyWriter.getSerializationType() == null) {
                // Can't determine source type and no target type has been
                // configure. Delegate on jackson.
                continue;
            }

            // All other converters must be set in ConversionService
            if (this.conversionService.canConvert(sourceType, targetType)) {

                // We must create BeanPropertyWriter own Serializer that
                // has knowledge about the Field related to that
                // BeanPropertyWriter in order to have access to
                // Field Annotations for success serialization
                JsonSerializer<Object> jsonSerializer = new ConversionServicePropertySerializer(
                        this.conversionService, sourceType, targetType);

                beanPropertyWriter.assignSerializer(jsonSerializer);
            }
            // If no converter set, use default Jackson property writer
            else {
                continue;
            }
        }
    }
    return beanProperties;
}
 
Example 17
Source File: GenericConversionService.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Template method to convert a {@code null} source.
 * <p>The default implementation returns {@code null} or the Java 8
 * {@link java.util.Optional#empty()} instance if the target type is
 * {@code java.util.Optional}. Subclasses may override this to return
 * custom {@code null} objects for specific target types.
 * @param sourceType the source type to convert from
 * @param targetType the target type to convert to
 * @return the converted null object
 */
@Nullable
protected Object convertNullSource(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.getObjectType() == Optional.class) {
		return Optional.empty();
	}
	return null;
}
 
Example 18
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Template method to convert a {@code null} source.
 * <p>The default implementation returns {@code null} or the Java 8
 * {@link java.util.Optional#empty()} instance if the target type is
 * {@code java.util.Optional}. Subclasses may override this to return
 * custom {@code null} objects for specific target types.
 * @param sourceType the sourceType to convert from
 * @param targetType the targetType to convert to
 * @return the converted null object
 */
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (javaUtilOptionalEmpty != null && targetType.getObjectType() == javaUtilOptionalEmpty.getClass()) {
		return javaUtilOptionalEmpty;
	}
	return null;
}
 
Example 19
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Template method to convert a {@code null} source.
 * <p>The default implementation returns {@code null} or the Java 8
 * {@link java.util.Optional#empty()} instance if the target type is
 * {@code java.util.Optional}. Subclasses may override this to return
 * custom {@code null} objects for specific target types.
 * @param sourceType the source type to convert from
 * @param targetType the target type to convert to
 * @return the converted null object
 */
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (javaUtilOptionalEmpty != null && targetType.getObjectType() == javaUtilOptionalEmpty.getClass()) {
		return javaUtilOptionalEmpty;
	}
	return null;
}
 
Example 20
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Template method to convert a {@code null} source.
 * <p>The default implementation returns {@code null} or the Java 8
 * {@link java.util.Optional#empty()} instance if the target type is
 * {@code java.util.Optional}. Subclasses may override this to return
 * custom {@code null} objects for specific target types.
 * @param sourceType the source type to convert from
 * @param targetType the target type to convert to
 * @return the converted null object
 */
@Nullable
protected Object convertNullSource(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.getObjectType() == Optional.class) {
		return Optional.empty();
	}
	return null;
}