javap 以一种可编程的方式

编程入门 行业动态 更新时间:2024-10-23 16:20:17
本文介绍了javap 以一种可编程的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们可以在我们自己的java代码中以可编程的方式使用javap吗?

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

例如以下代码:

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 中都有一个对应的 Class 来表示它.因此,在某种程度上,如果您只想打印出类文件的某些部分,就不是那么简单了.这里有一个简单的例子你可以参考,注意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 以一种可编程的方式

本文发布于:2023-06-04 08:33:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可编程   方式   javap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!