javap的在可编程方式 [英] javap in a programmable way

查看:141
本文介绍了javap的在可编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用javap的在我们自己的Java code在可编程的方式?

Can we use javap in our own java code in a programmable way?

例如,下面的code:

for example, the following code:

public class TestClass {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

在命令行中使用javap的,我们得到了:

using javap in command line, we got :

// Header + consts 1..22 snipped
const #22 = String      #23;    //  hello world
const #23 = Asciz       hello world;

public static void main(java.lang.String[]);
  Signature: ([Ljava/lang/String;)V
  Code:
   Stack=2, Locals=1, Args_size=1
   0:   getstatic       #16; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #22; //String hello world
   5:   invokevirtual   #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return
  // Debug info snipped
}

我可以只打印使用的javap的API常量池?

can I print only the Constant Pool using javap's API?

推荐答案

Apache的BCEL 提供的封装.class文件的解析,它提供了一整套的API。几乎在.class文件的每一个元素,有一个在BECL API相应的类重新present它。因此,在某种程度上,它不是那么简单,如果你只是想打印出来的类文件的某些部分。这里是你可以参考,留意一个简单的例子 org.apache.bcel.classfile.ClassParser

Apache BCEL provides encapsulations of .class file parsing, which provides a set of API. Almost for every element in .class file, there's a corresponding Class in BECL API to represent it. So in some way, it is not that straightforward if you just want to print out certain sections of the class file. Here is a simple example you can refer, pay attention to the org.apache.bcel.classfile.ClassParser:

    ClassParser cp = new ClassParser("TestClass.class");
    JavaClass jc = cp.parse();
    ConstantPool constantPool = jc.getConstantPool(); // Get the constant pool here.
    for (Constant c : constantPool.getConstantPool()) {
        System.out.println(c); // Do what you need to do with all the constants.
    }

这篇关于javap的在可编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆