Java Reflection Method Invoke invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)

Here you can find the source of invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)

Description

Invokes all methods on the given instance that have been annotated with the given Annotation.

License

Apache License

Parameter

Parameter Description
annotation the annotation to look for
instance to invoke a method of
args to supply in a method call

Exception

Parameter Description
InvocationTargetException ite
IllegalArgumentException iae
IllegalAccessException if not allowed to invoke that method

Declaration

public static void invokeMethodsWithAnnotation(final Class<? extends Annotation> annotation,
        final Object instance, final Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code

//package com.java2s;
/**/*from  w w  w. ja  v a 2s .c  o  m*/
 * Copyright (C) 2016 Hurence (support@hurence.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**
     * Invokes all methods on the given instance that have been annotated with
     * the given Annotation. If the signature of the method that is defined in
     * <code>instance</code> uses 1 or more parameters, those parameters must be
     * specified by the <code>args</code> parameter. However, if more arguments
     * are supplied by the <code>args</code> parameter than needed, the extra
     * arguments will be ignored.
     *
     * @param annotation the annotation to look for
     * @param instance to invoke a method of
     * @param args to supply in a method call
     * @throws InvocationTargetException ite
     * @throws IllegalArgumentException iae
     * @throws IllegalAccessException if not allowed to invoke that method
     */
    public static void invokeMethodsWithAnnotation(final Class<? extends Annotation> annotation,
            final Object instance, final Object... args)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        for (final Method method : instance.getClass().getMethods()) {
            if (method.isAnnotationPresent(annotation)) {
                final boolean isAccessible = method.isAccessible();
                method.setAccessible(true);

                try {
                    final Class<?>[] argumentTypes = method.getParameterTypes();
                    if (argumentTypes.length > args.length) {
                        throw new IllegalArgumentException(String.format(
                                "Unable to invoke method %1$s on %2$s because method expects %3$s parameters but only %4$s were given",
                                method.getName(), instance, argumentTypes.length, args.length));
                    }

                    for (int i = 0; i < argumentTypes.length; i++) {
                        final Class<?> argType = argumentTypes[i];
                        if (!argType.isAssignableFrom(args[i].getClass())) {
                            throw new IllegalArgumentException(String.format(
                                    "Unable to invoke method %1$s on %2$s because method parameter %3$s is expected to be of type %4$s but argument passed was of type %5$s",
                                    method.getName(), instance, i, argType, args[i].getClass()));
                        }
                    }

                    if (argumentTypes.length == args.length) {
                        method.invoke(instance, args);
                    } else {
                        final Object[] argsToPass = new Object[argumentTypes.length];
                        for (int i = 0; i < argsToPass.length; i++) {
                            argsToPass[i] = args[i];
                        }

                        method.invoke(instance, argsToPass);
                    }
                } finally {
                    if (!isAccessible) {
                        method.setAccessible(false);
                    }
                }
            }
        }
    }
}

Related

  1. invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)
  2. invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
  3. invokeMethodOnObject(Object target, String methodName)
  4. invokeMethodQuietly(Object receiver, Method method, Object... args)
  5. invokeMethods(final Object target, final List methods)
  6. invokeMethodWithArray2(Object target, Method method, Object[] args)
  7. invokeMethodWithKnownParamTypes(Object object, String methodName, Class[] methodParamTypes, Object... args)
  8. invokeMethodWithNoArgs(Object o, Method method)
  9. invokeMethodWithoutException(Object object, String methodName, Class[] parameterTypes, Object[] parameters)