Javassist读取类信息非常简单,使用ClassPool对象获取到CtClass对象后就可以像使用Java反射API一样去读取类信息了。
    Javassist读取类信息示例代码:

    1. package com.anbai.sec.bytecode.javassist;
    2. import javassist.*;
    3. import java.util.Arrays;
    4. public class JavassistClassAccessTest {
    5. public static void main(String[] args) {
    6. // 创建ClassPool对象
    7. ClassPool classPool = ClassPool.getDefault();
    8. try {
    9. CtClass ctClass = classPool.get("com.anbai.sec.bytecode.TestHelloWorld");
    10. System.out.println(
    11. "解析类名:" + ctClass.getName() + ",父类:" + ctClass.getSuperclass().getName() +
    12. ",实现接口:" + Arrays.toString(ctClass.getInterfaces())
    13. );
    14. System.out.println("-----------------------------------------------------------------------------");
    15. // 获取所有的构造方法
    16. CtConstructor[] ctConstructors = ctClass.getDeclaredConstructors();
    17. // 获取所有的成员变量
    18. CtField[] ctFields = ctClass.getDeclaredFields();
    19. // 获取所有的成员方法
    20. CtMethod[] ctMethods = ctClass.getDeclaredMethods();
    21. // 输出所有的构造方法
    22. for (CtConstructor ctConstructor : ctConstructors) {
    23. System.out.println(ctConstructor.getMethodInfo());
    24. }
    25. System.out.println("-----------------------------------------------------------------------------");
    26. // 输出所有成员变量
    27. for (CtField ctField : ctFields) {
    28. System.out.println(ctField);
    29. }
    30. System.out.println("-----------------------------------------------------------------------------");
    31. // 输出所有的成员方法
    32. for (CtMethod ctMethod : ctMethods) {
    33. System.out.println(ctMethod);
    34. }
    35. } catch (NotFoundException e) {
    36. e.printStackTrace();
    37. }
    38. }
    39. }

    程序执行结果:

    1. 解析类名:com.anbai.sec.bytecode.TestHelloWorld,父类:java.lang.Object,实现接口:[javassist.CtClassType@60addb54[public abstract interface class java.io.Serializable fields= constructors= methods=]]
    2. -----------------------------------------------------------------------------
    3. <init> ()V
    4. -----------------------------------------------------------------------------
    5. com.anbai.sec.bytecode.TestHelloWorld.serialVersionUID:J
    6. com.anbai.sec.bytecode.TestHelloWorld.id:J
    7. com.anbai.sec.bytecode.TestHelloWorld.username:Ljava/lang/String;
    8. com.anbai.sec.bytecode.TestHelloWorld.password:Ljava/lang/String;
    9. -----------------------------------------------------------------------------
    10. javassist.CtMethod@ca717109[public hello (Ljava/lang/String;)Ljava/lang/String;]
    11. javassist.CtMethod@44a4fe33[public static main ([Ljava/lang/String;)V]
    12. javassist.CtMethod@fb809fd2[public getId ()J]
    13. javassist.CtMethod@5321790a[public setId (J)V]
    14. javassist.CtMethod@7a2b684d[public getUsername ()Ljava/lang/String;]
    15. javassist.CtMethod@7942008f[public setUsername (Ljava/lang/String;)V]
    16. javassist.CtMethod@3b463cd2[public getPassword ()Ljava/lang/String;]
    17. javassist.CtMethod@da549dd4[public setPassword (Ljava/lang/String;)V]
    18. javassist.CtMethod@69cb6c6d[public toString ()Ljava/lang/String;]