@Override public BeanProperty.Builder<?> processBeanProperty(final BeanProperty.Builder<?> property, Class<?> beanOrNestedClass) { property.getAnnotation(Caption.class).ifPresent(a -> { property.message(a.value()); if (!Caption.NO_VALUE.equals(a.messageCode())) { property.messageCode(a.messageCode()); } LOGGER.debug(() -> "BeanPropertyCaptionPostProcessor: setted property [" + property + "] caption [" + a.value() + "/" + a.messageCode() + "]"); }); return property; }
@SuppressWarnings("hiding") static // tag::caption[] class Bean2 { public static final BeanPropertySet<Bean2> PROPERTIES = BeanPropertySet.create(Bean2.class); @Caption("Code") // <1> private Long id; @Caption(value = "Name", messageCode = "name.localization.code") // <2> private String name; // getters and setters omitted } // end::caption[]
/** * Get given enum localizable caption value, using the {@link Caption} annotation if available. * @param value The enum value * @return The enum localizable caption, using the {@link Caption} annotation if available or the enum value name if * not */ private static Localizable getEnumCaptionLocalizable(Enum<?> value) { try { final java.lang.reflect.Field field = value.getClass().getField(value.name()); if (field.isAnnotationPresent(Caption.class)) { String captionMessage = AnnotationUtils.getStringValue(field.getAnnotation(Caption.class).value()); return Localizable.builder().message((captionMessage != null) ? captionMessage : value.name()) .messageCode(AnnotationUtils.getStringValue(field.getAnnotation(Caption.class).messageCode())) .build(); } } catch (@SuppressWarnings("unused") NoSuchFieldException | SecurityException e) { return Localizable.of(value.name()); } return Localizable.of(value.name()); }
@SuppressWarnings("unused") public class ExamplePresenter { // tag::presenter[] enum MyEnum { @Caption("The value 1") VALUE1, @Caption(value = "The value 2", messageCode = "message.value2") VALUE2 } public void present() { String presented = StringValuePresenter.getDefault().present("stringValue"); // <1> presented = StringValuePresenter.getDefault().present("stringValue", ParameterSet.builder().withParameter(StringValuePresenter.MAX_LENGTH, 6).build()); // <2> presented = StringValuePresenter.getDefault().present(MyEnum.VALUE1); // <3> presented = StringValuePresenter.getDefault().present(new MyEnum[] { MyEnum.VALUE1, MyEnum.VALUE2 }); // <4> } // end::presenter[] }
/** * Convert a {@link Enum} value * @param value Value to convert * @return String value */ private static String convertEnum(Enum<?> value) { Localizable lv = null; if (value instanceof Localizable) { lv = (Localizable) value; } // check Caption annotation on value try { final Field fld = value.getClass().getField(value.name()); if (fld.isAnnotationPresent(Caption.class)) { lv = Localizable.builder().message(fld.getAnnotation(Caption.class).value()) .messageCode(AnnotationUtils.getStringValue(fld.getAnnotation(Caption.class).messageCode())) .build(); } } catch (@SuppressWarnings("unused") Exception e) { // ignore } return (lv != null) ? convertLocalizable(lv) : value.name(); }
final java.lang.reflect.Field fld = item.getClass().getField(enm.name()); if (fld.isAnnotationPresent(Caption.class)) { lv = Localizable.builder().message(fld.getAnnotation(Caption.class).value()).messageCode( AnnotationUtils.getStringValue(fld.getAnnotation(Caption.class).messageCode())) .build();
final java.lang.reflect.Field fld = item.getClass().getField(enm.name()); if (fld.isAnnotationPresent(Caption.class)) { lv = Localizable.builder().message(fld.getAnnotation(Caption.class).value()).messageCode( AnnotationUtils.getStringValue(fld.getAnnotation(Caption.class).messageCode())) .build();
cfg.setCaption(cpt.value()); cfg.setCaptionMessageCode(cpt.messageCode());