Example usage for java.lang.reflect Constructor getAnnotations

List of usage examples for java.lang.reflect Constructor getAnnotations

Introduction

In this page you can find the example usage for java.lang.reflect Constructor getAnnotations.

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java

public static void handleAnnotatedConstructors(Class clazz, Object object, ViewFinder viewFinder,
        boolean initForLazy) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException,
        InstantiationException {/* ww  w. j av  a 2 s  .c  o  m*/

    Constructor[] constructors = clazz.getConstructors();
    for (int i = 0; i < constructors.length; ++i) {

        Constructor constructor = constructors[i];
        Annotation[] annotations = constructor.getAnnotations();
        for (int j = 0; j < annotations.length; ++j) {

            Annotation anno = annotations[j];
            if (!(anno instanceof ListenerDefs)) {
                continue;
            }

            ListenerDefs annoContainer = (ListenerDefs) anno;
            if (annoContainer.value().length == 0) {
                continue;
            }

            Map<Class, Object> targetListenerCache = new HashMap<Class, Object>();

            Annotation[] setListenerAnnoArray = annoContainer.value();
            for (int k = 0; k < setListenerAnnoArray.length; ++k) {
                SetListeners setListenersAnno = (SetListeners) setListenerAnnoArray[k];

                if ((!initForLazy && setListenersAnno.lazy()) || (initForLazy && !setListenersAnno.lazy())) {
                    continue;
                }

                View view = viewFinder.findViewById(setListenersAnno.view());
                if (view == null) {
                    if (initForLazy && setListenersAnno.lazy()) {
                        // view == null is tolerable in this case, we assume that this field
                        // be injected later with another call to ViewWrapper.lazyInitializeLayout().
                        continue;
                    }

                    throw new IllegalArgumentException("The view specified in @SetListeners is not found: 0x"
                            + Integer.toHexString(setListenersAnno.view())
                            + ", if this field is meant to be injected lazily, remember to specify the 'lazy' attribute.");
                }

                Object targetListener = getTargetListener(clazz, object, targetListenerCache,
                        setListenersAnno.listener(), "@SetListeners");

                if (targetListener == null) {
                    targetListener = object;
                }

                AnnotationUtils.setListenersForView(view, setListenersAnno.listenerTypes(), targetListener);
            }

        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration.java

private static void process(final ClassConfiguration classConfiguration, final String hostClassName,
        final String expectedBrowserName, final float browserVersionNumeric) {
    final String simpleClassName = hostClassName.substring(hostClassName.lastIndexOf('.') + 1);

    CLASS_NAME_MAP_.put(hostClassName, simpleClassName);
    final Map<String, Method> allGetters = new HashMap<>();
    final Map<String, Method> allSetters = new HashMap<>();
    for (final Constructor<?> constructor : classConfiguration.getHostClass().getDeclaredConstructors()) {
        for (final Annotation annotation : constructor.getAnnotations()) {
            if (annotation instanceof JsxConstructor) {
                if (isSupported(((JsxConstructor) annotation).value(), expectedBrowserName,
                        browserVersionNumeric)) {
                    classConfiguration.setJSConstructor(constructor);
                }//from   ww w  . j a va  2  s  . c  o  m
            }
        }
    }
    for (final Method method : classConfiguration.getHostClass().getDeclaredMethods()) {
        for (final Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof JsxGetter) {
                final JsxGetter jsxGetter = (JsxGetter) annotation;
                if (isSupported(jsxGetter.value(), expectedBrowserName, browserVersionNumeric)) {
                    String property;
                    if (jsxGetter.propertyName().isEmpty()) {
                        final int prefix = method.getName().startsWith("is") ? 2 : 3;
                        property = method.getName().substring(prefix);
                        property = Character.toLowerCase(property.charAt(0)) + property.substring(1);
                    } else {
                        property = jsxGetter.propertyName();
                    }
                    allGetters.put(property, method);
                }
            } else if (annotation instanceof JsxSetter) {
                final JsxSetter jsxSetter = (JsxSetter) annotation;
                if (isSupported(jsxSetter.value(), expectedBrowserName, browserVersionNumeric)) {
                    String property;
                    if (jsxSetter.propertyName().isEmpty()) {
                        property = method.getName().substring(3);
                        property = Character.toLowerCase(property.charAt(0)) + property.substring(1);
                    } else {
                        property = jsxSetter.propertyName();
                    }
                    allSetters.put(property, method);
                }
            } else if (annotation instanceof JsxFunction) {
                if (isSupported(((JsxFunction) annotation).value(), expectedBrowserName,
                        browserVersionNumeric)) {
                    classConfiguration.addFunction(method);
                }
            } else if (annotation instanceof JsxStaticGetter) {
                final JsxStaticGetter jsxStaticGetter = (JsxStaticGetter) annotation;
                if (isSupported(jsxStaticGetter.value(), expectedBrowserName, browserVersionNumeric)) {
                    final int prefix = method.getName().startsWith("is") ? 2 : 3;
                    String property = method.getName().substring(prefix);
                    property = Character.toLowerCase(property.charAt(0)) + property.substring(1);
                    classConfiguration.addStaticProperty(property, method, null);
                }
            } else if (annotation instanceof JsxStaticFunction) {
                if (isSupported(((JsxStaticFunction) annotation).value(), expectedBrowserName,
                        browserVersionNumeric)) {
                    classConfiguration.addStaticFunction(method);
                }
            } else if (annotation instanceof JsxConstructor) {
                if (isSupported(((JsxConstructor) annotation).value(), expectedBrowserName,
                        browserVersionNumeric)) {
                    classConfiguration.setJSConstructor(method);
                }
            }
        }
    }
    for (final Field field : classConfiguration.getHostClass().getDeclaredFields()) {
        final JsxConstant jsxConstant = field.getAnnotation(JsxConstant.class);
        if (jsxConstant != null
                && isSupported(jsxConstant.value(), expectedBrowserName, browserVersionNumeric)) {
            classConfiguration.addConstant(field.getName());
        }
    }
    for (final Entry<String, Method> getterEntry : allGetters.entrySet()) {
        final String property = getterEntry.getKey();
        classConfiguration.addProperty(property, getterEntry.getValue(), allSetters.get(property));
    }
}