8.23Java反射访问方法

8.23Java反射访问方法

获取一个对象方法的信息

动态获取一个对象方法的信息,首先需要通过下列方法之一创建一个 Method 类型的对象或者数组。

  • getMethods()

  • getMethods(String name,Class<?> …parameterTypes)

  • getDeclaredMethods()

  • getDeclaredMethods(String name,Class<?>...parameterTypes)

实例:

objectClass.getDeclaredConstructor("max",int.class,String.class);
objectClass.getDeclaredConstructor("max",new Class[]{int.class,String.class});
静态方法名称说明
getName() 获取该方法的名称
getParameterType() 按照声明顺序以 Class 数组的形式返回该方法各个参数的类型
getReturnType() 以 Class 对象的形式获得该方法的返回值类型
getExceptionTypes() 以 Class 数组的形式获得该方法可能抛出的异常类型
invoke(Object obj,Object...args) 利用 args 参数执行指定对象 obj 中的该方法,返回值为 Object 类型
isVarArgs() 查看该方法是否允许带有可变数量的参数,如果允许返回 true,否则返回 false
getModifiers() 获得可以解析出该方法所采用修饰符的整数

目标类

package PracticeReview.Reflect;

public class ReflectPracticeDemoNo2 {
   // static 作用域方法
   static void staticMethod() {
       System.out.println("执行staticMethod()方法");
  }
   // public 作用域方法
   public int publicMethod(int i) {
       System.out.println("执行publicMethod()方法");
       return 100 + i;
  }
   // protected 作用域方法
   protected int protectedMethod(String s, int i) throws NumberFormatException {
       System.out.println("执行protectedMethod()方法");
       return Integer.valueOf(s) + i;
  }
   // private 作用域方法
   private String privateMethod(String... strings) {
       System.out.println("执行privateMethod()方法");
       StringBuffer sb = new StringBuffer();
       for (int i = 0; i < sb.length(); i++) {
           sb.append(strings[i]);
      }
       return sb.toString();
  }
}

测试类:

package PracticeReview.Reflect;

import java.lang.reflect.Method;

public class ReflectTestNo2 {
   public static void main(String[] args) {
       // 获取动态类ReflectPracticeDemoNo2
       ReflectPracticeDemoNo2 book = new ReflectPracticeDemoNo2();
       Class class1 = book.getClass();
       // 获取ReflectPracticeDemoNo2类的所有方法
       Method[] declaredMethods = class1.getDeclaredMethods();
       for (int i = 0; i < declaredMethods.length; i++) {
           Method method = declaredMethods[i];
           System.out.println("方法名称为:" + method.getName());
           System.out.println("方法是否带有可变数量的参数:" + method.isVarArgs());
           System.out.println("方法的参数类型依次为:");
           // 获取所有参数类型
           Class[] methodType = method.getParameterTypes();
           for (int j = 0; j < methodType.length; j++) {
               System.out.println(" " + methodType[j]);
          }
           // 获取返回值类型
           System.out.println("方法的返回值类型为:" + method.getReturnType());
           System.out.println("方法可能抛出的异常类型有:");
           // 获取所有可能抛出的异常
           Class[] methodExceptions = method.getExceptionTypes();
           for (int j = 0; j < methodExceptions.length; j++) {
               System.out.println(" " + methodExceptions[j]);
          }
           boolean isTurn = true;
           while (isTurn) {
               try { // 如果该成员变量的访问权限为private,则抛出异常
                   isTurn = false;
                   if (method.getName().equals("staticMethod")) { // 调用没有参数的方法
                       method.invoke(book);
                  } else if (method.getName().equals("publicMethod")) { // 调用一个参数的方法
                       System.out.println("publicMethod(10)的返回值为:" + method.invoke(book, 10));
                  } else if (method.getName().equals("protectedMethod")) { // 调用两个参数的方法
                       System.out.println("protectedMethod("10",15)的返回值为:" + method.invoke(book, "10", 15));
                  } else if (method.getName().equals("privateMethod")) { // 调用可变数量参数的方法
                       Object[] parameters = new Object[] { new String[] { "J", "A", "V", "A" } };
                       System.out.println("privateMethod()的返回值为:" + method.invoke(book, parameters));
                  }
              } catch (Exception e) {
                   System.out.println("在设置成员变量值时抛出异常,下面执行setAccessible()方法");
                   method.setAccessible(true); // 设置为允许访问private方法
                   isTurn = true;
              }
          }
           System.out.println("============================= ");
      }
  }
}

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/15177397.html