模块 jmc.utils

类 Reflects

java.lang.Object
com.jmc.lang.reflect.Reflects

public class Reflects extends Object
反射增强类
从以下版本开始:
1.0
  • 方法详细资料

    • getField

      public static Field getField(Class<?> c, String fieldName)
      获取类中指定名称的成员变量
      参数:
      c - 类的Class对象
      fieldName - 成员变量名称
      返回:
      指定的成员变量
      从以下版本开始:
      1.5
      API Note:
      
       class Student {
           private String name;
       }
      
       // 获取Student类的name属性
       Field nameField = Reflects.getField(Student.class, "name");
       
    • getMethod

      public static Method getMethod(Class<?> c, String methodName, Class<?>... parameterTypes)
      获取指定的方法
      参数:
      c - 类的Class对象
      methodName - 方法名称
      parameterTypes - 参数类型
      返回:
      方法对象
      从以下版本开始:
      1.5
      API Note:
      
       class Student {
           private Long id;
           private String name;
           void print(Long id, String name) {}
       }
      
       // 获取Student类的print方法(需要指定方法名称和所有参数类型)
       Method printMethod = Reflects.getMethod(Student.class, "print", Long.class, String.class);
       
    • newInstance

      public static <T> T newInstance(Class<T> c, Object... args)
      动态创建一个类实例
      类型参数:
      T - 类对象的类型
      参数:
      c - 类的Class对象
      args - 类的构造器参数
      返回:
      构建的类实例
      从以下版本开始:
      2.7
      API Note:
      
       // 创建一个StringBuilder对象实例(需要传入构造器参数)
       var instance = Reflects.newInstance(StringBuilder.class, "ABC");
       
    • getFieldValue

      public static <T> T getFieldValue(Object instance, String fieldName)
      获取类中成员变量的值
      类型参数:
      T - 类的类型
      参数:
      instance - 类的实例
      fieldName - 成员变量名称
      返回:
      成员变量的值
      从以下版本开始:
      2.7
      API Note:
      
       class Student {
           private Long id;
           public Student(Long id) { this.id = id; }
       }
      
       // 创建对象
       var stu = new Student(3);
      
       // 获取其成员变量id的值(需要指定变量类型Long)
       Long idValue = Reflects.getFieldValue(stu, "id");
       
    • getFieldValue

      public static <T> T getFieldValue(Class<?> c, String fieldName)
      获取类中静态成员变量的值
      类型参数:
      T - 类的类型
      参数:
      c - 类的Class对象
      fieldName - 静态成员变量名称
      返回:
      静态成员变量的值
      从以下版本开始:
      2.7
      API Note:
      
       class Student {
           private static final Long id = 3;
       }
      
       // 获取其静态变量id的值(需要指定变量类型Long)
       Long idValue = Reflects.getFieldValue(Student.class, "id");
       
    • invokeMethod

      public static <R> R invokeMethod(Object instance, String methodName, Object... args)
      执行指定的方法(不支持参数存在基本数据类型的方法)
      类型参数:
      R - 返回值类型
      参数:
      instance - 类的实例
      methodName - 方法名称
      args - 方法参数
      返回:
      方法返回值
      API Note:
      
       class Student {
           private Long id;
           private String name;
           String setName(String name) {
               var oldName = this.name;
               this.name = name;
               return oldName;
           }
       }
      
       // 创建对象
       var stu = new Student(1, "Jmc");
      
       // 执行setName方法(需要传入参数)并获取返回值(需要指定返回值类型String)
       String oldName = Reflects.invokeMethod(stu, "setName", "Lucy");
       
    • invokeMethod

      public static <R> R invokeMethod(Class<?> c, String methodName, Object... args)
      执行指定的静态方法(不支持参数存在基本数据类型的方法)
      类型参数:
      R - 返回值类型
      参数:
      c - 类的Class对象
      methodName - 方法名称
      args - 方法参数
      返回:
      方法返回值
      从以下版本开始:
      1.5
      API Note:
      
       class Student {
           private Long id;
           private String name;
           static String info(Long id) { return ...; }
       }
      
       // 执行静态方法info(需要传入参数)并获取返回值(需要指定返回值类型String)
       String info = Reflects.invokeMethod(Student.class, "info", 3);
       
    • loadClass

      public static Class<?> loadClass(String className, byte[] classFileBytes)
      从class文件加载Class对象
      参数:
      className - 类名
      classFileBytes - class文件的byte数组
      返回:
      Class对象
      从以下版本开始:
      3.2
      API Note:
      
       // 从classFileBytes字节数组中加载类名为com.jmc.Student的Class对象
       Class<?> studentClass = Reflects.loadClass("com.jmc.Student", classFileBytes);
       
    • loadClass

      public static Class<?> loadClass(String className, String classFilePath)
      从class文件加载Class对象
      参数:
      className - 类名
      classFilePath - class文件路径
      返回:
      Class对象
      从以下版本开始:
      3.2
      API Note:
      
       // 从Student.class文件中加载类名为com.jmc.Student的Class对象
       Class<?> studentClass = Reflects.loadClass("com.jmc.Student", "/path/to/Student.class");
       
    • loadClassInJar

      public static Class<?> loadClassInJar(String className, String jarFilePath)
      从jar文件加载Class对象
      参数:
      className - 类名
      jarFilePath - jar文件路径
      返回:
      Class对象
      从以下版本开始:
      3.2
      API Note:
      
       // 从test.jar文件中加载类名为com.jmc.Student的Class对象
       Class<?> studentClass = Reflects.loadClassInJar("com.jmc.Student", "/path/to/test.jar");
       
    • outObj

      public static byte[] outObj(Object o)
      将一个对象实例写入byte数组
      参数:
      o - 对象实例
      返回:
      结果byte数组
      API Note:
      
       // 将String对象序列化到byte数组
       byte[] bytes = Reflects.outObj("666");
       
    • readObj

      public static <T> T readObj(byte[] bs)
      从byte数组中读取一个对象实例
      类型参数:
      T - 对象类型
      参数:
      bs - byte数组
      返回:
      读取的对象
      API Note:
      
       // 从byte数组(bytes)读取String对象(需要指定对象类型String)
       String obj = Reflects.readObj(bytes);
       
    • isClassInJar

      public static boolean isClassInJar(Class<?> c)
      判断类是否在jar包中
      参数:
      c - 类的Class对象
      返回:
      类是否在jar包中
      从以下版本开始:
      2.6
      API Note:
      
       class Student {}
       // 判断Student类是否在jar中
       boolean res = Reflects.isClassInJar(Student.class);
       
    • getJarPath

      public static String getJarPath(Class<?> jarClass)
      获取jar的绝对路径
      参数:
      jarClass - jar中的任意一个Class
      返回:
      jar的绝对路径
      从以下版本开始:
      3.0
      API Note:
      
       class Student {}
       // 获取Student类的jar的系统绝对路径
       String path = Reflects.getJarPath(Student.class);
       
    • getClassPath

      public static String getClassPath(Class<?> c)
      获取指定类的类加载路径
      参数:
      c - 类的Class对象
      返回:
      类加载路径
      从以下版本开始:
      3.4
      API Note:
      
       class Student {}
       // 获取Student类的类加载路径
       String classPath = Reflects.getClassPath(Student.class);
       
    • getClassPathURL

      public static Optional<URL> getClassPathURL(Class<?> c)
      获取指定类的类加载路径的URL
      参数:
      c - 类的Class对象
      返回:
      类加载路径
      从以下版本开始:
      2.6
      API Note:
      
       class Student {}
       // 获取Student类的类加载路径
       URL classPathUrl = Reflects.getClassPathURL(Student.class).orElseThrow();
       
    • listResources

      public static List<Reflects.URLInfo> listResources(Class<?> c, String path)
      获取类路径下指定路径的一级文件/文件夹的URL信息列表
      参数:
      c - 类的Class对象
      path - 指定的路径
      返回:
      一级文件/文件夹的URL信息对象列表
      从以下版本开始:
      2.6
      API Note:
      
       // 获取Reflect类路径的/com/jmc文件夹下的一级文件和文件夹
       // 这个方法对jar、非jar都适用
       Reflects.listResources(Reflects.class, "/com/jmc")
               .forEach(urlInfo -> {
                   // 获取文件/文件夹url
                   URL url = urlInfo.getUrl();
                   // 获取文件/文件夹名称
                   String name = urlInfo.getName();
                   // 是否为文件
                   boolean isFile = urlInfo.isFile();
               });