DefaultValueCodecResolver implements ValueCodecResolver { @Override @SneakyThrows public Optional<ValueCodec> resolve(EntityPropertyDescriptor descriptor) { Set<Annotation> annotations = descriptor.getAnnotations() .stream() .filter(ann -> null != ann.annotationType().getAnnotation(Codec.class)) .collect(Collectors.toSet()); List<ValueCodec> codecs = new ArrayList<>(); for (Annotation annotation : annotations) { BiFunction<EntityPropertyDescriptor, Annotation, ValueCodec> function = annotationStrategies.get(annotation.annotationType()); if (function != null) { codecs.add(function.apply(descriptor, annotation)); } } if (codecs.size() == 1) { return Optional.of(codecs.get(0)); } return Optional.ofNullable(typeStrategies.get(descriptor.getPropertyType())) .map(func -> func.apply(descriptor)) .map(Optional::of) .orElseGet(() -> predicateStrategies.entrySet().stream() .filter(e -> e.getKey().test(descriptor.getPropertyType())) .map(e -> e.getValue().apply(descriptor)) .findFirst()); }  @SuppressWarnings("all") void register(Class<T> ann, BiFunction<EntityPropertyDescriptor, T, ValueCodec> codecFunction); void register(Class ann, Function<EntityPropertyDescriptor, ValueCodec> codecFunction); void register(Predicate<Class> ann, Function<EntityPropertyDescriptor, ValueCodec> codecFunction); @Override @SneakyThrows Optional<ValueCodec> resolve(EntityPropertyDescriptor descriptor); static final DefaultValueCodecResolver COMMONS; }
@Test public void test(){ DefaultValueCodecResolver resolver=DefaultValueCodecResolver.COMMONS; Map<String, PropertyDescriptor> descriptorMap = Stream.of(PropertyUtils.getPropertyDescriptors(Entity.class)) .collect(Collectors.toMap(PropertyDescriptor::getName, Function.identity())); { ValueCodec codec= resolver.resolve(SimpleEntityPropertyDescriptor.of(descriptorMap.get("date"),null)) .orElseThrow(NullPointerException::new); Assert.assertTrue(codec instanceof DateTimeCodec); } { ValueCodec codec= resolver.resolve(SimpleEntityPropertyDescriptor.of(descriptorMap.get("arr"),null)) .orElseThrow(NullPointerException::new); Assert.assertTrue(codec instanceof JsonValueCodec); } { ValueCodec codec= resolver.resolve(SimpleEntityPropertyDescriptor.of(descriptorMap.get("state"),null)) .orElseThrow(NullPointerException::new); Assert.assertTrue(codec instanceof EnumValueCodec); } { ValueCodec codec= resolver.resolve(SimpleEntityPropertyDescriptor.of(descriptorMap.get("state2"),null)) .orElseThrow(NullPointerException::new); Assert.assertTrue(codec instanceof EnumValueCodec); } }