Example usage for java.lang.reflect Method getAnnotations

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

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:com.aaplab.android.roboboost.RoboBoost.java

private static void injectMethods(Object holder, InjectionContext injectionContext, List<Method> methods) {
    for (Method method : methods) {
        Annotation[] annotations = method.getAnnotations();
        for (Annotation annotation : annotations) {
            Class<? extends Annotation> annotationClass = annotation.annotationType();
            if (InjectorRegister.contains(annotationClass)) {
                AbstractMethodInjector<Annotation> injector = InjectorRegister
                        .getMethodInjector(annotationClass);
                injector.doInjection(holder, injectionContext, method, annotation);
            }/*from w ww  . j  a va2  s. c o  m*/
        }
    }
}

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);
                }// w  w w  . j a v  a  2s  .  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));
    }
}

From source file:com.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forMethodReturnType(Method method, Class<?> implClass) {
    ResolvableType resolvableType = ResolvableType.forMethodReturnType(method, implClass);
    return new ResolvableTypeAccessor(method.getName(), resolvableType,
            Lists.newArrayList(method.getAnnotations()), implClass);
}

From source file:org.apache.axis2.jaxrs.JAXRSUtils.java

/**
  * given a jaxrs class model & java method , construct a jaxrs model associated with
  * method , reading the method level annotations.
  * @param classModel/*from   w  w w .  j  av  a2s.  c om*/
  * @param serviceMethod
  * @return
  */
public static JAXRSModel getMethodModel(JAXRSModel classModel, Method serviceMethod) {
    JAXRSModel model = new JAXRSModel();
    addProducesToMethodModel(classModel, model);
    addConsumesToMethodModel(classModel, model);
    addPathToMethodModel(classModel, model);
    Annotation[] annotation = serviceMethod.getAnnotations();
    for (Annotation a : annotation) {
        if (a != null) {
            if (a instanceof Produces) {
                addProducesToMethodModel((Produces) a, model);
            } else if (a instanceof Consumes) {
                addConsumesToMethodModel((Consumes) a, model);
            } else if (a instanceof Path) {
                addPathToMethodModel((Path) a, model);
            } else {
                addHTTPMethodToMethodModel(a, model);
            }

        }
    }
    return model;
}

From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java

private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
    synchronized (annotatedInterfaceCache) {
        Boolean flag = annotatedInterfaceCache.get(iface);
        if (flag != null) {
            return flag;
        }/*from  www. ja v a2  s.c o m*/
        boolean found = false;
        for (Method ifcMethod : iface.getMethods()) {
            if (ifcMethod.getAnnotations().length > 0) {
                found = true;
                break;
            }
        }
        annotatedInterfaceCache.put(iface, found);
        return found;
    }
}

From source file:com.astamuse.asta4d.util.annotation.AnnotatedPropertyUtil.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static AnnotatedPropertyInfoMap retrievePropertiesMap(Class cls) {
    String cacheKey = cls.getName();
    AnnotatedPropertyInfoMap map = propertiesMapCache.get(cacheKey);
    if (map == null) {
        List<AnnotatedPropertyInfo> infoList = new LinkedList<>();
        Set<String> beanPropertyNameSet = new HashSet<>();

        Method[] mtds = cls.getMethods();
        for (Method method : mtds) {
            List<Annotation> annoList = ConvertableAnnotationRetriever
                    .retrieveAnnotationHierarchyList(AnnotatedProperty.class, method.getAnnotations());

            if (CollectionUtils.isEmpty(annoList)) {
                continue;
            }/*from  w w  w  .  ja v  a  2s  .  c om*/

            AnnotatedPropertyInfo info = new AnnotatedPropertyInfo();
            info.setAnnotations(annoList);

            boolean isGet = false;
            boolean isSet = false;
            String propertySuffixe = method.getName();
            if (propertySuffixe.startsWith("set")) {
                propertySuffixe = propertySuffixe.substring(3);
                isSet = true;
            } else if (propertySuffixe.startsWith("get")) {
                propertySuffixe = propertySuffixe.substring(3);
                isGet = true;
            } else if (propertySuffixe.startsWith("is")) {
                propertySuffixe = propertySuffixe.substring(2);
                isSet = true;
            } else {
                String msg = String.format("Method [%s]:[%s] can not be treated as a getter or setter method.",
                        cls.getName(), method.toGenericString());
                throw new RuntimeException(msg);
            }

            char[] cs = propertySuffixe.toCharArray();
            cs[0] = Character.toLowerCase(cs[0]);
            info.setBeanPropertyName(new String(cs));

            AnnotatedProperty ap = (AnnotatedProperty) annoList.get(0);// must by
            String name = ap.name();
            if (StringUtils.isEmpty(name)) {
                name = info.getBeanPropertyName();
            }

            info.setName(name);

            if (isGet) {
                info.setGetter(method);
                info.setType(method.getReturnType());
                String setterName = "set" + propertySuffixe;
                Method setter = null;
                try {
                    setter = cls.getMethod(setterName, method.getReturnType());
                } catch (NoSuchMethodException | SecurityException e) {
                    String msg = "Could not find setter method:[{}({})] in class[{}] for annotated getter:[{}]";
                    logger.warn(msg, new Object[] { setterName, method.getReturnType().getName(), cls.getName(),
                            method.getName() });
                }
                info.setSetter(setter);
            }

            if (isSet) {
                info.setSetter(method);
                info.setType(method.getParameterTypes()[0]);
                String getterName = "get" + propertySuffixe;
                Method getter = null;
                try {
                    getter = cls.getMethod(getterName);
                } catch (NoSuchMethodException | SecurityException e) {
                    String msg = "Could not find getter method:[{}:{}] in class[{}] for annotated setter:[{}]";
                    logger.warn(msg, new Object[] { getterName, method.getReturnType().getName(), cls.getName(),
                            method.getName() });
                }
                info.setGetter(getter);
            }

            infoList.add(info);
            beanPropertyNameSet.add(info.getBeanPropertyName());
        }

        List<Field> list = new ArrayList<>(ClassUtil.retrieveAllFieldsIncludeAllSuperClasses(cls));
        Iterator<Field> it = list.iterator();

        while (it.hasNext()) {
            Field f = it.next();
            List<Annotation> annoList = ConvertableAnnotationRetriever
                    .retrieveAnnotationHierarchyList(AnnotatedProperty.class, f.getAnnotations());
            if (CollectionUtils.isNotEmpty(annoList)) {
                AnnotatedProperty ap = (AnnotatedProperty) annoList.get(0);// must by

                String beanPropertyName = f.getName();
                if (beanPropertyNameSet.contains(beanPropertyName)) {
                    continue;
                }

                String name = ap.name();
                if (StringUtils.isEmpty(name)) {
                    name = f.getName();
                }

                AnnotatedPropertyInfo info = new AnnotatedPropertyInfo();
                info.setAnnotations(annoList);
                info.setBeanPropertyName(beanPropertyName);
                info.setName(name);
                info.setField(f);
                info.setGetter(null);
                info.setSetter(null);
                info.setType(f.getType());
                infoList.add(info);
            }
        }

        map = new AnnotatedPropertyInfoMap(infoList);
        if (Configuration.getConfiguration().isCacheEnable()) {
            propertiesMapCache.put(cacheKey, map);
        }
    }
    return map;
}

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

public static boolean isByRefReturn(Method method) {

    Annotation[] methodAnnotations = method.getAnnotations();
    Class<?> returnType = method.getReturnType();
    return isByRef(returnType, methodAnnotations);

}

From source file:org.rhq.helpers.perftest.support.testng.DatabaseSetupInterceptor.java

/**
 * Obtain the required database state by looking for the @DatabaseState annotation.
 * Lookup is first done at method level and if not found done at class level.
 * @param method Method that TestNG is about to invoke
 * @return the desired database state or null if @DatabaseState is not given.
 * @see org.rhq.helpers.perftest.support.testng.DatabaseState
 *///from  w ww  .j  a v  a 2  s.com
private static DatabaseState getRequiredDatabaseState(IInvokedMethod method) {
    Method javaMethod = method.getTestMethod().getMethod();

    DatabaseState annotation = javaMethod.getAnnotation(DatabaseState.class);
    if (annotation == null) {
        //            System.out.println("Method : " + javaMethod.getName());

        boolean skip = false;

        // Filter out methods that are marked as setup/tear down
        Annotation[] annots = javaMethod.getAnnotations();
        for (Annotation an : annots) {
            //                System.out.println("       :  " + an.toString());
            if (an.annotationType().equals(BeforeMethod.class) || an.annotationType().equals(AfterMethod.class)
                    || an.annotationType().equals(BeforeSuite.class)
                    || an.annotationType().equals(AfterSuite.class)
                    || an.annotationType().equals(BeforeTest.class)
                    || an.annotationType().equals(AfterTest.class))
                skip = true;
        }

        if (!skip)
            annotation = javaMethod.getDeclaringClass().getAnnotation(DatabaseState.class);
        //            else
        //                System.out.println("      ..... Skipped");

    }
    return annotation;
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

/**
 * @param method the method to inspect/*from w  w w  .  j  a  v a  2  s  . c o  m*/
 * @return {@code true} if it is annotated with any JPA annotations
 */
private static boolean hasAnnotations(Method method) {
    return with(method.getAnnotations()).exists(new Filter<Annotation>() {
        @Override
        public boolean accepts(Annotation item) {
            return item.getClass().getCanonicalName().startsWith("javax.presistence.");
        }
    });
}

From source file:org.topazproject.otm.metadata.AnnotationClassMetaFactory.java

private static boolean isAnnotated(Method method) {
    String ours = Id.class.getPackage().getName();

    for (Annotation a : method.getAnnotations())
        if (a.annotationType().getPackage().getName().equals(ours))
            return true;

    return false;
}