Java Method Call invoke(Class annotationClass, Object instance, Object[] parameters)

Here you can find the source of invoke(Class annotationClass, Object instance, Object[] parameters)

Description

Invokes the method of the object instance annotated with the specified annotation.

License

Open Source License

Parameter

Parameter Description
annotationClass the class of the annotation
instance the object instance
parameters the parameters with which to invoke the method

Declaration

public static void invoke(Class<? extends Annotation> annotationClass, Object instance, Object[] parameters) 

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.logging.Logger;

public class Main {
    /**/*from ww  w  . j a  va  2 s .  c o m*/
     * Invokes the method of the object instance annotated with the specified
     * annotation.
     * 
     * @param annotationClass
     *            the class of the annotation
     * @param instance
     *            the object instance
     * @param parameters
     *            the parameters with which to invoke the method
     */
    public static void invoke(Class<? extends Annotation> annotationClass, Object instance, Object[] parameters) {
        Class<?> instanceClass = instance.getClass();
        Method[] methods = instanceClass.getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(annotationClass)) {
                method.setAccessible(true);
                try {
                    method.invoke(instance, parameters);
                } catch (Exception e) {
                    String name = getCallerClassName(1);
                    Logger.getLogger(name).throwing(name, method.getName(), e);
                    e.printStackTrace();
                }
                return;
            }
        }
    }

    /**
     * Returns the class name of the caller at the specified stack depth.
     * 
     * @param depth
     *            the stack depth: 0 for the direct caller, 1 for the caller of
     *            the caller, and so on...
     * @return the class name of the caller
     */
    public static final String getCallerClassName(int depth) {
        return getStack()[depth + 2].getClassName();
    }

    protected static final StackTraceElement[] getStack() {
        try {
            throw new Exception();
        } catch (Exception e) {
            StackTraceElement[] result = e.getStackTrace();
            return result;
        }
    }
}

Related

  1. invoke(@Nonnull Object source, @Nonnull String name, Object... args)
  2. invoke(boolean makeAccessible, T object, Class methodClass, String methodName, Class[] paramTypes, Object... params)
  3. invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  4. invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
  5. invoke(Class clazz, Object obj, String methodName, Object... args)
  6. invoke(Class clazz, String methodName, Class[] parameterTypes, Object instance, Object[] args, Class returnType)
  7. invoke(Class clazz, String methodName, Object instance, Class[] signature, Object... args)
  8. invoke(Class cls, String methodName, Object... parameter)