private DurationStyle getStyle(TypeDescriptor targetType) { DurationFormat annotation = targetType.getAnnotation(DurationFormat.class); return (annotation != null) ? annotation.value() : null; }
@Test public void fieldAnnotated() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(getClass().getField("fieldAnnotated")); assertEquals(1, typeDescriptor.getAnnotations().length); assertNotNull(typeDescriptor.getAnnotation(FieldAnnotation.class)); }
private ChronoUnit getDurationUnit(TypeDescriptor sourceType) { DurationUnit annotation = sourceType.getAnnotation(DurationUnit.class); return (annotation != null) ? annotation.value() : null; }
@Test public void parameterAnnotated() throws Exception { TypeDescriptor t1 = new TypeDescriptor(new MethodParameter(getClass().getMethod("testAnnotatedMethod", String.class), 0)); assertEquals(String.class, t1.getType()); assertEquals(1, t1.getAnnotations().length); assertNotNull(t1.getAnnotation(ParameterAnnotation.class)); assertTrue(t1.hasAnnotation(ParameterAnnotation.class)); assertEquals(123, t1.getAnnotation(ParameterAnnotation.class).value()); }
private ChronoUnit getDurationUnit(TypeDescriptor targetType) { DurationUnit annotation = targetType.getAnnotation(DurationUnit.class); return (annotation != null) ? annotation.value() : null; }
private DataUnit getDataUnit(TypeDescriptor targetType) { DataSizeUnit annotation = targetType.getAnnotation(DataSizeUnit.class); return (annotation != null) ? annotation.value() : null; }
private CharSequence getDelimiter(TypeDescriptor sourceType) { Delimiter annotation = sourceType.getAnnotation(Delimiter.class); return (annotation != null) ? annotation.value() : ","; }
private DurationStyle getDurationStyle(TypeDescriptor sourceType) { DurationFormat annotation = sourceType.getAnnotation(DurationFormat.class); return (annotation != null) ? annotation.value() : null; }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { ExampleAnnotation ann = targetType.getAnnotation(ExampleAnnotation.class); return (ann != null && ann.active()); }
@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 = 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); }
@Test public void testUpCast() throws Exception { Property property = new Property(getClass(), getClass().getMethod("getProperty"), getClass().getMethod("setProperty", Map.class)); TypeDescriptor typeDescriptor = new TypeDescriptor(property); TypeDescriptor upCast = typeDescriptor.upcast(Object.class); assertTrue(upCast.getAnnotation(MethodAnnotation1.class) != null); }
private void assertAnnotationFoundOnMethod(Class<? extends Annotation> annotationType, String methodName) throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(new MethodParameter(getClass().getMethod(methodName), -1)); assertNotNull("Should have found @" + annotationType.getSimpleName() + " on " + methodName + ".", typeDescriptor.getAnnotation(annotationType)); }
@Test public void property() throws Exception { Property property = new Property( getClass(), getClass().getMethod("getProperty"), getClass().getMethod("setProperty", Map.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(Map.class, desc.getType()); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(MethodAnnotation1.class)); assertNotNull(desc.getAnnotation(MethodAnnotation2.class)); assertNotNull(desc.getAnnotation(MethodAnnotation3.class)); }
@Test public void propertyGenericClassList() throws Exception { IntegerClass genericBean = new IntegerClass(); Property property = new Property(genericBean.getClass(), genericBean.getClass().getMethod("getListProperty"), genericBean.getClass().getMethod("setListProperty", List.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(List.class, desc.getType()); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(MethodAnnotation1.class)); assertTrue(desc.hasAnnotation(MethodAnnotation1.class)); }
@Test public void mapKeyTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapKeyTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void mapValueTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapValueTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void elementTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("listPreserveContext")); assertEquals(Integer.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.elementTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }