Java Reflection Method Annotation getMethodsWithAnnotation(Class type, Class obj)

Here you can find the source of getMethodsWithAnnotation(Class type, Class obj)

Description

get Methods With Annotation

License

Open Source License

Declaration

public synchronized static Method[] getMethodsWithAnnotation(Class<? extends Annotation> type, Class<?> obj) 

Method Source Code


//package com.java2s;
import java.lang.annotation.Annotation;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class Main {
    public synchronized static Method[] getMethodsWithAnnotation(Class<? extends Annotation> type, Class<?> obj) {
        return getMethodsWithAnnotation(type, obj, false);
    }/*from   www.j a  va2 s .  c  o  m*/

    public synchronized static Method[] getMethodsWithAnnotation(Class<? extends Annotation> type, Class<?> obj,
            boolean includeInterfaces) {
        Method[] meths = obj.getDeclaredMethods();

        ArrayList<Method> methods = new ArrayList<Method>();

        for (Method field : meths) {

            if (field.isAnnotationPresent(type)) {
                field.setAccessible(true);
                methods.add(field);
            }
        }

        if (obj.getSuperclass() != null) {
            Method[] flds2 = getMethodsWithAnnotation(type, obj.getSuperclass(), includeInterfaces);

            if (flds2.length > 0) {
                for (int i = 0; i < flds2.length; i++) {
                    methods.add(flds2[i]);
                }
            }
        }

        if (includeInterfaces) {
            Class<?>[] interfs = obj.getInterfaces();

            for (int i = 0; i < interfs.length; i++) {
                Class<?> interf = interfs[i];

                // for now, no recursion
                Method[] flds2 = getMethodsWithAnnotation(type, interf);

                if (flds2.length > 0) {
                    for (int j = 0; j < flds2.length; j++) {
                        methods.add(flds2[j]);
                    }
                }
            }
        }

        meths = methods.toArray(new Method[methods.size()]);
        return meths;
    }
}

Related

  1. getMethodsAnnotatedWith(Class clazz, Class annotClazz)
  2. getMethodsAnnotatedWith(Class clazz, Class annotation)
  3. getMethodsAnnotatedWith(final Class type, final Class annotation)
  4. getMethodsInAnnotation(Annotation anno)
  5. getMethodsWithAnnotation(Class annotation, Class clazz)
  6. getMethodsWithAnnotation(Class clazz, Class annotationType)
  7. getMethodsWithAnnotation(Class type, Class annotation, boolean checkSuper)
  8. getMethodsWithAnnotation(final Class clazz, final Class... annotationClasses)
  9. getMethodValue(Class annotationClasss, Class targetClass, String... annotationFields)