为什么要用spring框架的ReflectionTestUtils工具类的invokeMethod方法?
当我们想要调用一个实例对象的私有方法时,我们可以利用反射机制去调用该私有方法。
含有私有方法的类,
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.test.util.ReflectionTestUtils;
-
- import java.lang.reflect.Field;
-
- public final class DemoClass {
- /**
- * 私有无参构造函数,避免调用方直接new DemoClass()
- */
- private DemoClass() {
- }
-
- /**
- * 利用静态内部类Holder创建单实例对象DemoClass
- */
- private static final class Holder {
- private static final DemoClass instance = new DemoClass();
- }
-
- private String key;
-
- private void privateMethod(String input) {
- System.out.println("this is my private method! input parameter is :" + input);
- }
-
- /**
- * 获取单实例对象方法
- *
- * @return 单实例对象
- */
- public static DemoClass getInstance() {
- return Holder.instance;
- }
- }
测试类,
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.test.util.ReflectionTestUtils;
-
- import java.lang.reflect.Field;
-
- public class ReflectionTestUtilsDemo {
-
- @Before
- public void beforeClassInit() {
- try {
- // 支持获取private属性
- Field declaredField = DemoClass.getInstance().getClass().getDeclaredField("key");
- System.out.println("getDeclaredField method can get private field, field is :" + declaredField.getName());
- // 仅可以获取public属性
- Field field = DemoClass.getInstance().getClass().getField("key");
- System.out.println("getField method can get private field, field is :" + field.getName());
- } catch (NoSuchFieldException e) {
- System.out.println("no such field exception");
- }
- }
-
- @Test
- public void test() {
- // 通过反射调用私有的非静态方法,第一个参数必须是对象实例
- ReflectionTestUtils.invokeMethod(DemoClass.getInstance(), "privateMethod", "ok");
- System.out.println("this is my test");
- }
- }
实际效果如下,
注意Class类的getDeclaredField才可以获取私有成员变量,getField方法只能获取公有成员变量。
直接看org.springframework.test.util.ReflectionTestUtils类下面的invokeMethod方法,如下,
首先对目标对象断言不为空,只有对象不为null才会继续执行invokeMethod方法。
- @Nullable
- public static
T invokeMethod(Object target, String name, Object... args) { - Assert.notNull(target, "Target object must not be null");
- return invokeMethod(target, (Class)null, name, args);
- }
invokeMethod的重载方法步骤如下,
- @Nullable
- public static
T invokeMethod(@Nullable Object targetObject, @Nullable Class> targetClass, String name, Object... args) { - Assert.isTrue(targetObject != null || targetClass != null, "Either 'targetObject' or 'targetClass' for the method must be specified");
- Assert.hasText(name, "Method name must not be empty");
-
- try {
- MethodInvoker methodInvoker = new MethodInvoker();
- methodInvoker.setTargetObject(targetObject);
- if (targetClass != null) {
- methodInvoker.setTargetClass(targetClass);
- }
-
- methodInvoker.setTargetMethod(name);
- methodInvoker.setArguments(args);
- methodInvoker.prepare();
- if (logger.isDebugEnabled()) {
- logger.debug(String.format("Invoking method '%s' on %s or %s with arguments %s", name, safeToString(targetObject), safeToString(targetClass), ObjectUtils.nullSafeToString(args)));
- }
-
- return methodInvoker.invoke();
- } catch (Exception var5) {
- ReflectionUtils.handleReflectionException(var5);
- throw new IllegalStateException("Should never get here");
- }
- }
下面看看prepare方法做了什么,
- public void prepare() throws ClassNotFoundException, NoSuchMethodException {
- if (this.staticMethod != null) {
- int lastDotIndex = this.staticMethod.lastIndexOf('.');
- if (lastDotIndex == -1 || lastDotIndex == this.staticMethod.length()) {
- throw new IllegalArgumentException(
- "staticMethod must be a fully qualified class plus method name: " +
- "e.g. 'example.MyExampleClass.myExampleMethod'");
- }
- String className = this.staticMethod.substring(0, lastDotIndex);
- String methodName = this.staticMethod.substring(lastDotIndex + 1);
- this.targetClass = resolveClassName(className);
- this.targetMethod = methodName;
- }
-
- Class> targetClass = getTargetClass();
- String targetMethod = getTargetMethod();
- Assert.notNull(targetClass, "Either 'targetClass' or 'targetObject' is required");
- Assert.notNull(targetMethod, "Property 'targetMethod' is required");
-
- Object[] arguments = getArguments();
- Class>[] argTypes = new Class>[arguments.length];
- for (int i = 0; i < arguments.length; ++i) {
- argTypes[i] = (arguments[i] != null ? arguments[i].getClass() : Object.class);
- }
-
- // Try to get the exact method first.
- try {
- this.methodObject = targetClass.getMethod(targetMethod, argTypes);
- }
- catch (NoSuchMethodException ex) {
- // Just rethrow exception if we can't get any match.
- this.methodObject = findMatchingMethod();
- if (this.methodObject == null) {
- throw ex;
- }
- }
- }
继续往下挖,看看targetClass.getMethod怎么获取方法对象的,
- @CallerSensitive
- public Method getMethod(String name, Class>... parameterTypes)
- throws NoSuchMethodException, SecurityException {
- checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
- Method method = getMethod0(name, parameterTypes, true);
- if (method == null) {
- throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
- }
- return method;
- }
MethodInvoker对象的findMatchingMethod方法,
- @Nullable
- protected Method findMatchingMethod() {
- String targetMethod = getTargetMethod();
- Object[] arguments = getArguments();
- int argCount = arguments.length;
-
- Class> targetClass = getTargetClass();
- Assert.state(targetClass != null, "No target class set");
- Method[] candidates = ReflectionUtils.getAllDeclaredMethods(targetClass);
- int minTypeDiffWeight = Integer.MAX_VALUE;
- Method matchingMethod = null;
-
- for (Method candidate : candidates) {
- if (candidate.getName().equals(targetMethod)) {
- if (candidate.getParameterCount() == argCount) {
- Class>[] paramTypes = candidate.getParameterTypes();
- int typeDiffWeight = getTypeDifferenceWeight(paramTypes, arguments);
- if (typeDiffWeight < minTypeDiffWeight) {
- minTypeDiffWeight = typeDiffWeight;
- matchingMethod = candidate;
- }
- }
- }
- }
-
- return matchingMethod;
- }
再看方法的调用是如何实现的,找到MethodInvoker对象的invoke方法,
1)获取目标对象和提前准备好的方法;
2)如果目标对象为null或者目标方法是静态方法则抛出IllegalArgumentException异常;
3)使给定的非静态方法可访问setAccessible=true;
4)调用目标方法Method的invoke方法;
- @Nullable
- public Object invoke() throws InvocationTargetException, IllegalAccessException {
- // In the static case, target will simply be {@code null}.
- Object targetObject = getTargetObject();
- Method preparedMethod = getPreparedMethod();
- if (targetObject == null && !Modifier.isStatic(preparedMethod.getModifiers())) {
- throw new IllegalArgumentException("Target method must not be non-static without a target");
- }
- ReflectionUtils.makeAccessible(preparedMethod);
- return preparedMethod.invoke(targetObject, getArguments());
- }
Method的invoke方法如下,
- @CallerSensitive
- public Object invoke(Object obj, Object... args)
- throws IllegalAccessException, IllegalArgumentException,
- InvocationTargetException
- {
- if (!override) {
- if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
- Class> caller = Reflection.getCallerClass();
- checkAccess(caller, clazz, obj, modifiers);
- }
- }
- MethodAccessor ma = methodAccessor; // read volatile
- if (ma == null) {
- ma = acquireMethodAccessor();
- }
- return ma.invoke(obj, args);
- }
@CallerSensitive注解作用:
jvm的开发者认为Reflection.getCallerClass()方法危险,不希望开发者调用,就把这种危险的方法用 @CallerSensitive修饰,并在JVM级别检查,参考文末链接3。
ReflectionFactory的newMethodAccessor方法如下,其中isAnonymousClass方法检查基础类是否为匿名类。
- public MethodAccessor newMethodAccessor(Method var1) {
- checkInitted();
- if (noInflation && !ReflectUtil.isVMAnonymousClass(var1.getDeclaringClass())) {
- return (new MethodAccessorGenerator()).generateMethod(var1.getDeclaringClass(), var1.getName(), var1.getParameterTypes(), var1.getReturnType(), var1.getExceptionTypes(), var1.getModifiers());
- } else {
- NativeMethodAccessorImpl var2 = new NativeMethodAccessorImpl(var1);
- DelegatingMethodAccessorImpl var3 = new DelegatingMethodAccessorImpl(var2);
- var2.setParent(var3);
- return var3;
- }
- }
参考链接:
1、Java ReflectionTestUtils.invokeMethod方法代码示例 - 纯净天空