ASM - 如何从 Java 字节码名称转换 Java 类名称?

发布于 2024-11-25 12:13:43 字数 180 浏览 6 评论 0原文

我正在使用 ASM(字节码修改库),它提供对字节码命名格式中的类型名称的访问,例如,据报告,字符串字段具有描述:Ljava/lang/String

我需要为某些对象调用 Class.forName类,但我需要类型名称的源代码形式,例如 java.lang.String。

有没有办法将内部名称转换为Java源格式?

I'm using ASM (a bytecode modification library) and it provides access to type names in the bytecode naming format, for example a String field is reported to have the description: Ljava/lang/String

I need to invoke Class.forName for some classes, but I need the source code form of the type names for that, e.g. java.lang.String.

Is there a way of converting from internal name to Java source format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-12-02 12:13:43

我不知道任何API方法,但是转换很简单。您可以在此处的 JVM 规范
基本类型由一个字符表示:

B = byte
C = 字符
D = 双
F = 浮动
我=整数
J = 长
S = 短
Z = boolean

类和接口类型由完全限定名称表示,带有一个
'L' 前缀和 ';'后缀。点“.”在完全限定的类名中是
替换为 '/'(对于内部类,分隔外部类名和内部类名的 '.' 替换为 '$')。因此 String 类的内部名称将是“Ljava/lang/String;”内部类“java.awt.geom.Arc2D.Float”的内部名称将为“Ljava/awt/geom/Arc2D$Float;”。

数组名称以左括号“[”开头,后跟组件类型名称(原始或引用)。因此,“int[]”变为“[I”,“javax.swing.JFrame[][]”变为“[[Ljavax.swing.JFrame;”。

I don't know any API method, but the conversion is quite simple. You cand find details in JVM spec here.
Primitive types are represented by one character:

B = byte
C = char
D = double
F = float
I = int
J = long
S = short
Z = boolean

Class and interface types are represented by the fully qualified name, with an
'L' prefix and a ';' suffix. The dots '.' in the fully qualified class name are
replaced by '/' (for inner classes, the '.' separating the outer class name from the inner class name is replaced by a '$'). So the internal name of the String class would be "Ljava/lang/String;" and the internal name of the inner class "java.awt.geom.Arc2D.Float" would be "Ljava/awt/geom/Arc2D$Float;".

Array names begin with an opening bracket '[' followed by the component type name (primitive or reference). An "int[]" thus becomes "[I" and a "javax.swing.JFrame[][]" becomes "[[Ljavax.swing.JFrame;".

献世佛 2024-12-02 12:13:43

使用

org.objectweb.asm.Type.getType(typeDescriptor).getClassName()

示例:for Ljava/lang/Deprecated; 返回 java.lang.Deprecated
感谢@FlightOfStairs 的提示,他指出了正确的班级,所以找到了这个。

Use

org.objectweb.asm.Type.getType(typeDescriptor).getClassName()

Example: for Ljava/lang/Deprecated; returns java.lang.Deprecated.
Found this thanks to hint from @FlightOfStairs who pointed at right class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文