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

The following examples show how to use org.springframework.core.convert.TypeDescriptor#getAnnotation() . 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 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 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 = 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 4
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 5
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 6
Source File: DelimitedStringToCollectionConverter.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
private Object convert(String source, TypeDescriptor sourceType,
                       TypeDescriptor targetType) {
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
    String[] elements = getElements(source,
            (delimiter != null) ? delimiter.value() : ",");
    TypeDescriptor elementDescriptor = targetType.getElementTypeDescriptor();
    Collection<Object> target = createCollection(targetType, elementDescriptor,
            elements.length);
    Stream<Object> stream = Arrays.stream(elements).map(String::trim);
    if (elementDescriptor != null) {
        stream = stream.map((element) -> this.conversionService.convert(element,
                sourceType, elementDescriptor));
    }
    stream.forEach(target::add);
    return target;
}
 
Example 7
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 8
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 9
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 10
Source File: DelimitedStringToArrayConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private Object convert(String source, TypeDescriptor sourceType,
                       TypeDescriptor targetType) {
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
    String[] elements = getElements(source,
            (delimiter != null) ? delimiter.value() : ",");
    TypeDescriptor elementDescriptor = targetType.getElementTypeDescriptor();
    Object target = Array.newInstance(elementDescriptor.getType(), elements.length);
    for (int i = 0; i < elements.length; i++) {
        String sourceElement = elements[i];
        Object targetElement = this.conversionService.convert(sourceElement.trim(),
                sourceType, elementDescriptor);
        Array.set(target, i, targetElement);
    }
    return target;
}
 
Example 11
Source File: StringToDataSizeConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private DataUnit getDataUnit(TypeDescriptor targetType) {
    DataSizeUnit annotation = targetType.getAnnotation(DataSizeUnit.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 12
Source File: GenericConversionServiceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	ExampleAnnotation ann = targetType.getAnnotation(ExampleAnnotation.class);
	return (ann != null && ann.active());
}
 
Example 13
Source File: StringToDurationConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private DurationStyle getStyle(TypeDescriptor targetType) {
    DurationFormat annotation = targetType.getAnnotation(DurationFormat.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 14
Source File: StringToDurationConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private ChronoUnit getDurationUnit(TypeDescriptor targetType) {
    DurationUnit annotation = targetType.getAnnotation(DurationUnit.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 15
Source File: DurationToStringConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private ChronoUnit getDurationUnit(TypeDescriptor sourceType) {
    DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 16
Source File: DurationToStringConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private DurationStyle getDurationStyle(TypeDescriptor sourceType) {
    DurationFormat annotation = sourceType.getAnnotation(DurationFormat.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 17
Source File: DurationToNumberConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private ChronoUnit getDurationUnit(TypeDescriptor sourceType) {
    DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class);
    return (annotation != null) ? annotation.value() : null;
}
 
Example 18
Source File: CollectionToDelimitedStringConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private CharSequence getDelimiter(TypeDescriptor sourceType) {
    Delimiter annotation = sourceType.getAnnotation(Delimiter.class);
    return (annotation != null) ? annotation.value() : ",";
}
 
Example 19
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	ExampleAnnotation ann = targetType.getAnnotation(ExampleAnnotation.class);
	return (ann != null && ann.active());
}