public static ContainerElementKey forValueExtractor()

in bval-jsr/src/main/java/org/apache/bval/jsr/metadata/ContainerElementKey.java [59:98]


    public static ContainerElementKey forValueExtractor(ValueExtractor<?> extractor) {
        @SuppressWarnings("rawtypes")
        final Class<? extends ValueExtractor> extractorType = extractor.getClass();
        final Lazy<Set<ContainerElementKey>> result = new Lazy<>(HashSet::new);

        Stream.of(extractorType.getAnnotatedInterfaces()).filter(AnnotatedParameterizedType.class::isInstance)
            .map(AnnotatedParameterizedType.class::cast)
            .filter(apt -> ValueExtractor.class.equals(((ParameterizedType) apt.getType()).getRawType()))
            .forEach(decl -> {
                final AnnotatedType containerType = decl.getAnnotatedActualTypeArguments()[0];

                if (containerType.isAnnotationPresent(ExtractedValue.class)) {
                    final Class<?> extractedType = containerType.getAnnotation(ExtractedValue.class).type();
                    if (void.class.equals(extractedType)) {
                        Exceptions.raise(ValueExtractorDefinitionException::new, "%s does not specify %s type for %s",
                            extractorType, ExtractedValue.class.getSimpleName(), containerType);
                    }
                    result.get().add(new ContainerElementKey(containerType, null) {
                        public AnnotatedType getAnnotatedType() {
                            return EmulatedAnnotatedType.wrap(extractedType);
                        }
                    });
                }
                Optional.of(containerType).filter(AnnotatedParameterizedType.class::isInstance)
                    .map(AnnotatedParameterizedType.class::cast)
                    .map(AnnotatedParameterizedType::getAnnotatedActualTypeArguments).ifPresent(args -> {
                        IntStream.range(0, args.length).forEach(n -> {
                            if (args[n].isAnnotationPresent(ExtractedValue.class)) {
                                if (!void.class.equals(args[n].getAnnotation(ExtractedValue.class).type())) {
                                    log.warning(String.format("Ignoring non-default %s type specified for %s by %s",
                                        ExtractedValue.class.getSimpleName(), containerType.getType(), extractorType));
                                }
                                result.get().add(new ContainerElementKey(containerType, Integer.valueOf(n)));
                            }
                        });
                    });
            });
        return result.optional().filter(s -> s.size() == 1)
            .orElseThrow(() -> new ValueExtractorDefinitionException(extractorType.getName())).iterator().next();
    }