public static A instanceOf()

in processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java [45:78]


    public static <A extends Annotation> A instanceOf(final Class<A> annotationType, final Map<String, ?> elements) {
        final ClassLoader proxyClassLoader = Validate.notNull(annotationType, "annotationType").getClassLoader();
        final InvocationHandler invocationHandler = new InvocationHandler() {

            @Override
            @SuppressWarnings("PMD.UseVarargs") // overridden method
            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                if (method.getDeclaringClass().equals(annotationType)) {
                    if (elements.containsKey(method.getName())) {
                        return elements.get(method.getName());
                    }
                    return method.getDefaultValue();
                }
                if ("annotationType".equals(method.getName()) && method.getParameterTypes().length == 0) {
                    return annotationType;
                }
                if ("equals".equals(method.getName())
                    && Arrays.equals(method.getParameterTypes(), new Class[] { Object.class })) {
                    return AnnotationUtils.equals((Annotation) proxy, (Annotation) args[0]);
                }
                if ("hashCode".equals(method.getName()) && method.getParameterTypes().length == 0) {
                    return AnnotationUtils.hashCode((Annotation) proxy);
                }
                if ("toString".equals(method.getName()) && method.getParameterTypes().length == 0) {
                    return AnnotationUtils.toString((Annotation) proxy);
                }
                throw new UnsupportedOperationException();
            }
        };
        @SuppressWarnings("unchecked")
        final A result =
            (A) Proxy.newProxyInstance(proxyClassLoader, new Class[] { annotationType }, invocationHandler);
        return result;
    }