• org.springframework.test.util.ReflectionTestUtils.invokeMethod方法的使用


    序言

    为什么要用spring框架的ReflectionTestUtils工具类的invokeMethod方法?

    当我们想要调用一个实例对象的私有方法时,我们可以利用反射机制去调用该私有方法。

    Demo

    含有私有方法的类,

    1. import org.junit.Before;
    2. import org.junit.Test;
    3. import org.springframework.test.util.ReflectionTestUtils;
    4. import java.lang.reflect.Field;
    5. public final class DemoClass {
    6. /**
    7. * 私有无参构造函数,避免调用方直接new DemoClass()
    8. */
    9. private DemoClass() {
    10. }
    11. /**
    12. * 利用静态内部类Holder创建单实例对象DemoClass
    13. */
    14. private static final class Holder {
    15. private static final DemoClass instance = new DemoClass();
    16. }
    17. private String key;
    18. private void privateMethod(String input) {
    19. System.out.println("this is my private method! input parameter is :" + input);
    20. }
    21. /**
    22. * 获取单实例对象方法
    23. *
    24. * @return 单实例对象
    25. */
    26. public static DemoClass getInstance() {
    27. return Holder.instance;
    28. }
    29. }

    测试类,

    1. import org.junit.Before;
    2. import org.junit.Test;
    3. import org.springframework.test.util.ReflectionTestUtils;
    4. import java.lang.reflect.Field;
    5. public class ReflectionTestUtilsDemo {
    6. @Before
    7. public void beforeClassInit() {
    8. try {
    9. // 支持获取private属性
    10. Field declaredField = DemoClass.getInstance().getClass().getDeclaredField("key");
    11. System.out.println("getDeclaredField method can get private field, field is :" + declaredField.getName());
    12. // 仅可以获取public属性
    13. Field field = DemoClass.getInstance().getClass().getField("key");
    14. System.out.println("getField method can get private field, field is :" + field.getName());
    15. } catch (NoSuchFieldException e) {
    16. System.out.println("no such field exception");
    17. }
    18. }
    19. @Test
    20. public void test() {
    21. // 通过反射调用私有的非静态方法,第一个参数必须是对象实例
    22. ReflectionTestUtils.invokeMethod(DemoClass.getInstance(), "privateMethod", "ok");
    23. System.out.println("this is my test");
    24. }
    25. }

    实际效果如下,

    注意Class类的getDeclaredField才可以获取私有成员变量,getField方法只能获取公有成员变量。

    代码解析

    直接看org.springframework.test.util.ReflectionTestUtils类下面的invokeMethod方法,如下,

    首先对目标对象断言不为空,只有对象不为null才会继续执行invokeMethod方法。

    1. @Nullable
    2. public static T invokeMethod(Object target, String name, Object... args) {
    3. Assert.notNull(target, "Target object must not be null");
    4. return invokeMethod(target, (Class)null, name, args);
    5. }

    invokeMethod的重载方法步骤如下,

    • 1)判断目标对象或对象类不为null,且传进来的方法命不为空;
    • 2)创建MethodInvoker实例对象,并把目标对象或目标类,目标方法,参数等信息set到MethodInvoker对象中;
    • 3)调用MethodInvoker的prepare方法,这样后续可以多次直接调用反射获取的方法;
    • 4)调用MethodInvoker的invoke方法,执行方法。
    1. @Nullable
    2. public static T invokeMethod(@Nullable Object targetObject, @Nullable Class targetClass, String name, Object... args) {
    3. Assert.isTrue(targetObject != null || targetClass != null, "Either 'targetObject' or 'targetClass' for the method must be specified");
    4. Assert.hasText(name, "Method name must not be empty");
    5. try {
    6. MethodInvoker methodInvoker = new MethodInvoker();
    7. methodInvoker.setTargetObject(targetObject);
    8. if (targetClass != null) {
    9. methodInvoker.setTargetClass(targetClass);
    10. }
    11. methodInvoker.setTargetMethod(name);
    12. methodInvoker.setArguments(args);
    13. methodInvoker.prepare();
    14. if (logger.isDebugEnabled()) {
    15. logger.debug(String.format("Invoking method '%s' on %s or %s with arguments %s", name, safeToString(targetObject), safeToString(targetClass), ObjectUtils.nullSafeToString(args)));
    16. }
    17. return methodInvoker.invoke();
    18. } catch (Exception var5) {
    19. ReflectionUtils.handleReflectionException(var5);
    20. throw new IllegalStateException("Should never get here");
    21. }
    22. }

    下面看看prepare方法做了什么,

    • 1)判断MethodInvoker对象传入的静态方法名是否不为空;
    • 2)拿到MethodInvoker对象传入的目标对象和目标方法,并断言不为空;
    • 3)获取参数集合和参数的类型;
    • 4)通过目标类的getMethod方法找父类方法或接口方法,找不到则通过MethodInvoker对象的findMatchingMethod方法,再找不到则抛异常;
    1. public void prepare() throws ClassNotFoundException, NoSuchMethodException {
    2. if (this.staticMethod != null) {
    3. int lastDotIndex = this.staticMethod.lastIndexOf('.');
    4. if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
    5. throw new IllegalArgumentException(
    6. "staticMethod must be a fully qualified class plus method name: " +
    7. "e.g. 'example.MyExampleClass.myExampleMethod'");
    8. }
    9. String className = this.staticMethod.substring(0, lastDotIndex);
    10. String methodName = this.staticMethod.substring(lastDotIndex + 1);
    11. this.targetClass = resolveClassName(className);
    12. this.targetMethod = methodName;
    13. }
    14. Class targetClass = getTargetClass();
    15. String targetMethod = getTargetMethod();
    16. Assert.notNull(targetClass, "Either 'targetClass' or 'targetObject' is required");
    17. Assert.notNull(targetMethod, "Property 'targetMethod' is required");
    18. Object[] arguments = getArguments();
    19. Class[] argTypes = new Class[arguments.length];
    20. for (int i = 0; i < arguments.length; ++i) {
    21. argTypes[i] = (arguments[i] != null ? arguments[i].getClass() : Object.class);
    22. }
    23. // Try to get the exact method first.
    24. try {
    25. this.methodObject = targetClass.getMethod(targetMethod, argTypes);
    26. }
    27. catch (NoSuchMethodException ex) {
    28. // Just rethrow exception if we can't get any match.
    29. this.methodObject = findMatchingMethod();
    30. if (this.methodObject == null) {
    31. throw ex;
    32. }
    33. }
    34. }

    继续往下挖,看看targetClass.getMethod怎么获取方法对象的,

    • 1)检查是否允许客户端访问成员;
    • 2)从getMethod0方法中去找父类方法或接口方法;
    • 3)找不到则抛出NoSuchMethodException异常,找到则返回方法;
    1. @CallerSensitive
    2. public Method getMethod(String name, Class... parameterTypes)
    3. throws NoSuchMethodException, SecurityException {
    4. checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
    5. Method method = getMethod0(name, parameterTypes, true);
    6. if (method == null) {
    7. throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
    8. }
    9. return method;
    10. }

    MethodInvoker对象的findMatchingMethod方法,

    • 1)获取目标方法、参数、参数长度;
    • 2)获取目标类,断言不为null,并通过getAllDeclaredMethods方法获取目标类的所有方法;
    • 3)遍历目标类中的所有方法,找到匹配的方法;
    1. @Nullable
    2. protected Method findMatchingMethod() {
    3. String targetMethod = getTargetMethod();
    4. Object[] arguments = getArguments();
    5. int argCount = arguments.length;
    6. Class targetClass = getTargetClass();
    7. Assert.state(targetClass != null, "No target class set");
    8. Method[] candidates = ReflectionUtils.getAllDeclaredMethods(targetClass);
    9. int minTypeDiffWeight = Integer.MAX_VALUE;
    10. Method matchingMethod = null;
    11. for (Method candidate : candidates) {
    12. if (candidate.getName().equals(targetMethod)) {
    13. if (candidate.getParameterCount() == argCount) {
    14. Class[] paramTypes = candidate.getParameterTypes();
    15. int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
    16. if (typeDiffWeight < minTypeDiffWeight) {
    17. minTypeDiffWeight = typeDiffWeight;
    18. matchingMethod = candidate;
    19. }
    20. }
    21. }
    22. }
    23. return matchingMethod;
    24. }

    再看方法的调用是如何实现的,找到MethodInvoker对象的invoke方法,

    1)获取目标对象和提前准备好的方法;

    2)如果目标对象为null或者目标方法是静态方法则抛出IllegalArgumentException异常;

    3)使给定的非静态方法可访问setAccessible=true;

    4)调用目标方法Method的invoke方法;

    1. @Nullable
    2. public Object invoke() throws InvocationTargetException, IllegalAccessException {
    3. // In the static case, target will simply be {@code null}.
    4. Object targetObject = getTargetObject();
    5. Method preparedMethod = getPreparedMethod();
    6. if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) {
    7. throw new IllegalArgumentException("Target method must not be non-static without a target");
    8. }
    9. ReflectionUtils.makeAccessible(preparedMethod);
    10. return preparedMethod.invoke(targetObject, getArguments());
    11. }

    Method的invoke方法如下,

    • 1)如果是覆写的方法,调用Reflection.getCallerClass()的native方法获取调用者的类,并对调用者的类和目标对象进行检查;
    • 2)获取MethodAccessor对象,如果当前Method没有,则在根节点root不为null时从根节点获取MethodAccessorImpl对象,否则调用反射工厂的newMethodAccessor;
    • 3)调用MethodAccessorImpl对象(MethodAccessor接口的实现类)的invoke方法;
    1. @CallerSensitive
    2. public Object invoke(Object obj, Object... args)
    3. throws IllegalAccessException, IllegalArgumentException,
    4. InvocationTargetException
    5. {
    6. if (!override) {
    7. if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
    8. Class caller = Reflection.getCallerClass();
    9. checkAccess(caller, clazz, obj, modifiers);
    10. }
    11. }
    12. MethodAccessor ma = methodAccessor; // read volatile
    13. if (ma == null) {
    14. ma = acquireMethodAccessor();
    15. }
    16. return ma.invoke(obj, args);
    17. }

    @CallerSensitive注解作用:

    jvm的开发者认为Reflection.getCallerClass()方法危险,不希望开发者调用,就把这种危险的方法用 @CallerSensitive修饰,并在JVM级别检查,参考文末链接3。

    ReflectionFactory的newMethodAccessor方法如下,其中isAnonymousClass方法检查基础类是否为匿名类。

    1. public MethodAccessor newMethodAccessor(Method var1) {
    2. checkInitted();
    3. if (noInflation && !ReflectUtil.isVMAnonymousClass(var1.getDeclaringClass())) {
    4. return (new MethodAccessorGenerator()).generateMethod(var1.getDeclaringClass(), var1.getName(), var1.getParameterTypes(), var1.getReturnType(), var1.getExceptionTypes(), var1.getModifiers());
    5. } else {
    6. NativeMethodAccessorImpl var2 = new NativeMethodAccessorImpl(var1);
    7. DelegatingMethodAccessorImpl var3 = new DelegatingMethodAccessorImpl(var2);
    8. var2.setParent(var3);
    9. return var3;
    10. }
    11. }

    参考链接:

    1、Java ReflectionTestUtils.invokeMethod方法代码示例 - 纯净天空

    2、LeetCode - Medium - 332. Reconstruct Itinerary-蒲公英云

    3、JEP 176: Mechanical Checking of Caller-Sensitive Methods

  • 相关阅读:
    【Method】把 arXiv论文 转换为 HTML5 网页
    ElasticSearch中批量操作(批量查询_mget、批量插入删除_bulk)
    一文总结 C++ 常量表达式、constexpr 和 const
    uniapp开发小程序-pc端小程序下载文件
    springboot打成war包
    企业自研业务系统的登录如何添加动态口令,实施MFA双因子认证?
    14.9 Socket 高效文件传输
    【Jetson】使用 Jetson 控制无人车常用指令
    【STM32】工程配置,存储空间分别情况,常用操作
    常识——手机改直供电+usb调试
  • 原文地址:https://blog.csdn.net/zkkzpp258/article/details/128064990