菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻

Java 方法反射

原创
05/13 14:22 更新

Java反射 - Java方法反射

java.lang.reflect.Method 类的实例表示一个方法。

java.lang.reflect.Constructor 类的实例表示一个构造函数。

方法构造方法继承自一个通用的抽象超类可执行

可执行文件中的参数由 Parameter 类的对象表示

Executable 类中的 ​getParameters()​ 方法获取所有参数作为 Parameter 的数组。

默认情况下,参数名称不存储在类文件中。

参数类的名称将类似于 ​arg0​,​arg1​ 等。

我们可以通过编译源来保存类文件中的实际参数名代码使用 -parameters 选项与 javac 编译器。

可执行文件中的 getExceptionTypes()方法类返回一个由 Executable 抛出的异常数组。

Executable 类的 ​getModifiers()​ 方法将修饰符作为int返回。

来自 Executable 类的 ​getTypeParameters()​ 方法返回一个 TypeVariable 数组,该数组表示通用方法或构造函数的类型参数。

例子

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;

class MyClass<T> {
  
  public MyClass(int i, int j, String s){
    
  }
  public MyClass(T t){
    
  }
  public int getInt(String a){
    return 0;
  }
}

public class Main {
  public static void main(String[] argv){
    Class<MyClass> cls = MyClass.class;
    for(Method m:cls.getMethods()){
      System.out.println(m.getName());
      System.out.println(getModifiers(m)); 
      System.out.println(getParameters(m) );
      System.out.println(getExceptionList(m));
    }
  }
  public static ArrayList<String> getParameters(Executable exec) {
    Parameter[] parms = exec.getParameters();
    ArrayList<String> parmList = new ArrayList<>();
    for (int i = 0; i < parms.length; i++) {

      int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
      String modifiers = Modifier.toString(mod);
      String parmType = parms[i].getType().getSimpleName();
      String parmName = parms[i].getName();
      String temp = modifiers + "  " + parmType + "  " + parmName;
      if(temp.trim().length() == 0){
        continue;
      }
      parmList.add(temp.trim());
    }
    return parmList;
  }

  public static ArrayList<String> getExceptionList(Executable exec) {
    ArrayList<String> exceptionList = new ArrayList<>();
    for (Class<?> c : exec.getExceptionTypes()) {
      exceptionList.add(c.getSimpleName());
    }
    return exceptionList;
  }
  public static String getModifiers(Executable exec) {
    int mod = exec.getModifiers();
    if (exec instanceof Method) {
      mod = mod & Modifier.methodModifiers();
    } else if (exec instanceof Constructor) {
      mod = mod & Modifier.constructorModifiers();
    }
    return Modifier.toString(mod);
  }
}

上面的代码生成以下结果。

图片.png

反射方法

以下四个方法在Class类中返回有关类的方法的信息:

Method[]  getMethods()
Method[]  getDeclaredMethods()
Method getMethod(String name,  Class...  parameterTypes)
Method getDeclaredMethod(String name,  Class...  parameterTypes)

getMethods()方法返回该类的所有可访问的公共方法无论从类中还是继承自超类。

getDeclaredMethods()方法返回该类所有只在声明中的方法(不包括从超类继承的方法)。

getMethod(String name,Class … parameterTypes)和getDeclaredMethod(String name,Class … parameterTypes)通过方法名和参数类型获取Method对象。

Method类中的getReturnType()方法返回包含有关返回类型信息的Class对象。

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;

class MyClass<T> {

  public MyClass(int i, int j, String s) {
  }

  public MyClass(T t) {
  }

  public int getInt(String a) {
    return 0;
  }
}

public class Main {
  public static void main(String[] args) {
    Class<MyClass> c = MyClass.class;

    ArrayList<String> methodsDesciption = getDeclaredMethodsList(c);
    System.out.println("Declared Methods  for " + c.getName());
    for (String desc : methodsDesciption) {
      System.out.println(desc);
    }
    methodsDesciption = getMethodsList(c);
    System.out.println("\nMethods for  " + c.getName());
    for (String desc : methodsDesciption) {
      System.out.println(desc);
    }

  }

  public static ArrayList<String> getMethodsList(Class c) {
    Method[] methods = c.getMethods();
    ArrayList<String> methodsList = getMethodsDesciption(methods);
    return methodsList;
  }

  public static ArrayList<String> getDeclaredMethodsList(Class c) {
    Method[] methods = c.getDeclaredMethods();
    ArrayList<String> methodsList = getMethodsDesciption(methods);
    return methodsList;
  }

  public static ArrayList<String> getMethodsDesciption(Method[] methods) {
    ArrayList<String> methodList = new ArrayList<>();

    for (Method m : methods) {
      String modifiers = getModifiers(m);

      Class returnType = m.getReturnType();
      String returnTypeName = returnType.getSimpleName();

      String methodName = m.getName();

      String params = getParameters(m).toString();

      String throwsClause = getExceptionList(m).toString();

      methodList.add(modifiers + "  " + returnTypeName + "  " + methodName
          + "(" + params + ") " + throwsClause);
    }

    return methodList;
  }

  public static ArrayList<String> getParameters(Executable exec) {
    Parameter[] parms = exec.getParameters();
    ArrayList<String> parmList = new ArrayList<>();
    for (int i = 0; i < parms.length; i++) {

      int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
      String modifiers = Modifier.toString(mod);
      String parmType = parms[i].getType().getSimpleName();
      String parmName = parms[i].getName();
      String temp = modifiers + "  " + parmType + "  " + parmName;
      if (temp.trim().length() == 0) {
        continue;
      }
      parmList.add(temp.trim());
    }
    return parmList;
  }

  public static ArrayList<String> getExceptionList(Executable exec) {
    ArrayList<String> exceptionList = new ArrayList<>();
    for (Class<?> c : exec.getExceptionTypes()) {
      exceptionList.add(c.getSimpleName());
    }
    return exceptionList;
  }

  public static String getModifiers(Executable exec) {
    int mod = exec.getModifiers();
    if (exec instanceof Method) {
      mod = mod & Modifier.methodModifiers();
    } else if (exec instanceof Constructor) {
      mod = mod & Modifier.constructorModifiers();
    }
    return Modifier.toString(mod);
  }
}

上面的代码生成以下结果。

图片.png

综合评分:9.9 评分 请对本文进行纠错,及学习过程中有困难疑惑可在此进行讨论