javap in a programmable way

我怕爱的太早我们不能终老 提交于 2019-11-28 13:57:48

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.
    }

There is no API for javap internals, but you can look for the source code of javap, which is in the package com.sun.tools.javap. The entry class is com.sun.tools.javap.Main. So another way to run javap is java -cp $JAVA_HOME/lib/tools.jar com.sun.tools.javap.Main YourTestClass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!