下面列出了org.springframework.core.convert.TypeDescriptor#getAnnotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
@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);
}
@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);
}
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;
}
@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);
}
@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);
}
@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);
}
@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);
}
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;
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
ExampleAnnotation ann = targetType.getAnnotation(ExampleAnnotation.class);
return (ann != null && ann.active());
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
ExampleAnnotation ann = targetType.getAnnotation(ExampleAnnotation.class);
return (ann != null && ann.active());
}
private DataUnit getDataUnit(TypeDescriptor targetType) {
DataSizeUnit annotation = targetType.getAnnotation(DataSizeUnit.class);
return (annotation != null) ? annotation.value() : null;
}
private DurationStyle getStyle(TypeDescriptor targetType) {
DurationFormat annotation = targetType.getAnnotation(DurationFormat.class);
return (annotation != null) ? annotation.value() : null;
}
private ChronoUnit getDurationUnit(TypeDescriptor targetType) {
DurationUnit annotation = targetType.getAnnotation(DurationUnit.class);
return (annotation != null) ? annotation.value() : null;
}
private ChronoUnit getDurationUnit(TypeDescriptor sourceType) {
DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class);
return (annotation != null) ? annotation.value() : null;
}
private DurationStyle getDurationStyle(TypeDescriptor sourceType) {
DurationFormat annotation = sourceType.getAnnotation(DurationFormat.class);
return (annotation != null) ? annotation.value() : null;
}
private ChronoUnit getDurationUnit(TypeDescriptor sourceType) {
DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class);
return (annotation != null) ? annotation.value() : null;
}
private CharSequence getDelimiter(TypeDescriptor sourceType) {
Delimiter annotation = sourceType.getAnnotation(Delimiter.class);
return (annotation != null) ? annotation.value() : ",";
}