private static Optional substitute()

in bval-jsr/src/main/java/org/apache/bval/jsr/util/AnnotationsManager.java [281:304]


    private static Optional<AnnotatedElement> substitute(AnnotatedElement e) {
        if (e instanceof Parameter) {
            final Parameter p = (Parameter) e;
            if (p.getDeclaringExecutable() instanceof Constructor<?>) {
                final Constructor<?> ctor = (Constructor<?>) p.getDeclaringExecutable();
                final Class<?> dc = ctor.getDeclaringClass();
                if (!(dc.getDeclaringClass() == null || Modifier.isStatic(dc.getModifiers()))) {
                    // found ctor for non-static inner class
                    final Annotation[][] parameterAnnotations = ctor.getParameterAnnotations();
                    if (parameterAnnotations.length == ctor.getParameterCount() - 1) {
                        final Parameter[] parameters = ctor.getParameters();
                        final int idx = ObjectUtils.indexOf(parameters, p);
                        if (idx == 0) {
                            return Optional.empty();
                        }
                        return Optional.of(parameters[idx - 1]);
                    }
                    Validate.validState(parameterAnnotations.length == ctor.getParameterCount(),
                            "Cannot make sense of parameter annotations of %s", ctor);
                }
            }
        }
        return Optional.of(e);
    }