Java Reflection Method Get from Object getMethodConfigByAnnotaton(Object instance, Class declaringClass, Class annotationType, Class type)

Here you can find the source of getMethodConfigByAnnotaton(Object instance, Class declaringClass, Class annotationType, Class type)

Description

get Method Config By Annotaton

License

Open Source License

Declaration

public static <T> T getMethodConfigByAnnotaton(Object instance, Class<?> declaringClass,
            Class<? extends Annotation> annotationType, Class<T> type) throws Throwable 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    public static <T> T getMethodConfigByAnnotaton(Object instance, Class<?> declaringClass,
            Class<? extends Annotation> annotationType, Class<T> type) throws Throwable {
        T result = null;//from w  w  w.j  ava  2 s  . com
        List<Method> methods = new ArrayList<>();
        methods.addAll(Arrays.asList(declaringClass.getDeclaredMethods()));
        methods.addAll(Arrays.asList(declaringClass.getMethods()));
        for (Method method : methods) {
            Annotation anno = method.getAnnotation(annotationType);
            if (anno != null) {
                method.setAccessible(true);
                result = (T) method.invoke(instance);
                break;
            }
        }
        return result;
    }
}

Related

  1. getMethod(Object target, String methodName, Class... parameterTypes)
  2. getMethod(Object target, String signature)
  3. getMethod(String methodName, Object instance)
  4. getMethodAnnotatedWith(final Class type, final Class annotation, String fieldName, Object fieldValue)
  5. getMethodByName(Object target, String methodName)
  6. getMethodDescriptor(Object instance, String methodName, Class aClass, Class... parameters)
  7. getMethodFirstParamType(final String methodName, final Object o)
  8. getMethodIfAny(final Object instance, final String name, final Class[] params)
  9. getMethodIgnoreCaseWithNoParams(Object o, String p)