顾名思义,有没有办法查看使用反射的方法是否为空体?

最佳答案

经过反射Reflection ,我不这么认为。你可以看看BCEL ...

字节码工程库是
旨在为用户提供方便
分析
,创建和
操作(二进制)Java类文件

这是一个片段,可能使您从article开始...

public class ClassViewer{
   private JavaClass clazz;
   public ClassViewer(String clazz){
      this.clazz = Repository.lookupClass(clazz);
    }
   public static void main(String args[]){
      if(args.length != 1)
        throw new IllegalArgumentException(
          "One and only one class at a time!");
      ClassViewer viewer = new ClassViewer(args[0]);
      viewer.start();
    }
   private void start(){
      if(this.clazz != null){
        // first print the structure
        // of the class file
        System.err.println(clazz);
        // next print the methods
        Method[] methods = clazz.getMethods();
       for(int i=0; i<methods.length; i++){
          System.err.println(methods[i]);
          // now print the actual
          // byte code for each method
          Code code = methods[i].getCode();
         if(code != null)
            System.err.println(code);
       }
     }else
        throw new RuntimeException(
          "Class file is null!");
    }
}

09-10 00:54
查看更多