反射调用方法

工具类

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

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


public class EvolutionMethodUtils extends MethodUtils {

    public static Object invokeMethod(final Object object,
            final String methodName, Object... args)
            throws NoSuchMethodException, IllegalAccessException,
            InvocationTargetException {
        args = ArrayUtils.nullToEmpty(args);
        final Class<?>[] parameterTypes = toClass(args);
        return invokeMethod(object, methodName, args, parameterTypes);
    }

    private static Class<?>[] toClass(final Object... array) {
        if (array == null) {
            return null;
        } else if (array.length == 0) {
            return ArrayUtils.EMPTY_CLASS_ARRAY;
        }
        final Class<?>[] classes = new Class[array.length];
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                classes[i] = null;
                continue;
            }
            if (array[i] instanceof Integer) {
                classes[i] = Integer.TYPE;
            } else if (array[i] instanceof Byte) {
                classes[i] = Byte.TYPE;
            } else if (array[i] instanceof Short) {
                classes[i] = Short.TYPE;
            } else if (array[i] instanceof Float) {
                classes[i] = Float.TYPE;
            } else if (array[i] instanceof Double) {
                classes[i] = Double.TYPE;
            } else if (array[i] instanceof Character) {
                classes[i] = Character.TYPE;
            } else if (array[i] instanceof Long) {
                classes[i] = Long.TYPE;
            } else if (array[i] instanceof Boolean) {
                classes[i] = Boolean.TYPE;
            } else {
                classes[i] = array[i].getClass();
            }
        }
        return classes;
    }

    public static Object forceInvokeMethod(final Object object, String methodName, boolean force) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return forceInvokeMethod(object, methodName, force, ArrayUtils.EMPTY_OBJECT_ARRAY, null);
    }

    private static Object forceInvokeMethod(final Object object, final String methodName, boolean force,
                                            Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (!force) {
            return invokeMethod(object, methodName, args, parameterTypes);
        }
        parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
        args = ArrayUtils.nullToEmpty(args);
        Class<?> cls = object.getClass();
        Method method = null;
        try {
            method = cls.getDeclaredMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e) {
            // ignore
        }
        if (method == null) {
            method = cls.getMethod(methodName, parameterTypes);
        }
        if (method == null) {
            throw new NoSuchMethodException("No such accessible method: "
                    + methodName + "() on object: "
                    + cls.getName());
        }
        method.setAccessible(true);
        return method.invoke(object, args);
    }
}

执行方法调用

// wokeStep方法名称,AbstractCrawler.this 对象实例
EvolutionMethodUtils.forceInvokeMethod(AbstractCrawler.this, workStep, true);

转载于:https://my.oschina.net/ChiLin/blog/3062498

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值