ASM - 如何从 Java 字节码名称转换 Java 类名称?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道任何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;".
您可以使用
org.objectweb.asm.Type.getInternalName(java.lang.Class)
。You can use
org.objectweb.asm.Type.getInternalName(java.lang.Class)
.使用
示例:for
Ljava/lang/Deprecated;
返回java.lang.Deprecated
。感谢@FlightOfStairs 的提示,他指出了正确的班级,所以找到了这个。
Use
Example: for
Ljava/lang/Deprecated;
returnsjava.lang.Deprecated
.Found this thanks to hint from @FlightOfStairs who pointed at right class.