Java Method Call invoke(@Nonnull Object source, @Nonnull String name, Object... args)

Here you can find the source of invoke(@Nonnull Object source, @Nonnull String name, Object... args)

Description

invoke

License

Apache License

Parameter

Parameter Description
source a parameter
name a parameter
args a parameter

Declaration

public static <T> T invoke(@Nonnull Object source, @Nonnull String name, Object... args) 

Method Source Code

//package com.java2s;
/*//from  w ww. j  a  va 2 s  . c  o  m
 * Copyright 2016-2017 Daniel Siviter
 *
 * 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.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import javax.annotation.Nonnull;

public class Main {
    /**
     * 
     * @param source
     * @param name
     * @param args
     * @return
     */
    public static <T> T invoke(@Nonnull Object source, @Nonnull String name, Object... args) {
        Class<?>[] argTypes = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++) {
            argTypes[i] = args[i] == null ? Object.class : args[i].getClass();
        }

        final Method method = findMethod(source.getClass(), name, argTypes);
        if (method == null) {
            throw new IllegalArgumentException(String.format("Unable to find '%s' with '%s' on '%s'!", name,
                    Arrays.toString(argTypes), source.getClass()));
        }
        return invoke(source, method, args);
    }

    /**
     * 
     * @param source
     * @param name
     * @param args
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T invoke(@Nonnull Object source, @Nonnull Method method, Object... args) {
        try {
            setAccessible(method);
            return (T) method.invoke(source, args);
        } catch (IllegalAccessException | InvocationTargetException ex) {
            throw new IllegalStateException(
                    String.format("Unexpected reflection exception - %s", ex.getClass().getName()), ex);
        }
    }

    /**
     * 
     * @param clazz
     * @param name
     * @param params
     * @return
     */
    public static Method findMethod(@Nonnull Class<?> clazz, @Nonnull String name, Class<?>... params) {
        Class<?> searchType = clazz;
        while (!Object.class.equals(searchType) && searchType != null) {
            for (Method method : searchType.getDeclaredMethods()) {
                if (name.equals(method.getName()) && Arrays.equals(params, method.getParameterTypes())) {
                    return method;
                }
            }
            searchType = searchType.getSuperclass();
        }
        return null;
    }

    /**
     * 
     * @param field
     */
    public static void setAccessible(@Nonnull Field field) {
        if ((!Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
                || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
        }
    }

    /**
     * 
     * @param method
     */
    public static void setAccessible(@Nonnull Method method) {
        if ((!Modifier.isPublic(method.getModifiers())
                || !Modifier.isPublic(method.getDeclaringClass().getModifiers())
                || Modifier.isFinal(method.getModifiers())) && !method.isAccessible()) {
            method.setAccessible(true);
        }
    }
}

Related

  1. invoke(boolean makeAccessible, T object, Class methodClass, String methodName, Class[] paramTypes, Object... params)
  2. invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params)
  3. invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0)
  4. invoke(Class annotationClass, Object instance, Object[] parameters)