Java Reflection Method Get from Object getMethodsIncludingHierarchy(Object comp, Class annotation)

Here you can find the source of getMethodsIncludingHierarchy(Object comp, Class annotation)

Description

same as getMethod but uses while loop to check hierarchy

License

LGPL

Parameter

Parameter Description
comp a parameter
annotation a parameter

Declaration

public static List<Method> getMethodsIncludingHierarchy(Object comp, Class<? extends Annotation> annotation) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**//from  w  ww  .  j a  va 2s . c  om
     * same as getMethod but uses while loop to check hierarchy
     * @param comp
     * @param annotation
     * @return
     */
    public static List<Method> getMethodsIncludingHierarchy(Object comp, Class<? extends Annotation> annotation) {
        LinkedList<Method> result = new LinkedList<Method>();
        Class<?> current = comp.getClass();
        while (current != null) {
            result.addLast(getMethod(current, annotation));
            current = current.getSuperclass();
        }
        return result;
    }

    /**
     * Checks for annotation of the class
     * @param compClassOrSuper
     * @param annotation
     * @return
     */

    private static Method getMethod(Class<?> compClassOrSuper, Class<? extends Annotation> annotation) {
        for (Method m : compClassOrSuper.getMethods()) {
            if (m.getAnnotation(annotation) != null) {
                return m;
            }
        }
        return null;
    }
}

Related

  1. getMethodsAnnotatedWith(Class ann, Object o)
  2. getMethodsAnnotatedWith(Object target, Class annotation)
  3. getMethodsAnnotatedWithValue(Class anno, Object o, String name, Object value)
  4. getMethodsByStartsWithName(String name, Object o)
  5. getMethodsForObject(Object o2, String[] passedMethods)
  6. getMethodValue(Method method, Object instance)
  7. getMethodValue(Object base, Method method)
  8. getMethodValue(Object target, String methodName)
  9. getMethodVector(Object object)