Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASM - How can I convert Java class name from Java bytecode name?

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?

like image 485
mahonya Avatar asked Jul 22 '11 12:07

mahonya


2 Answers

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;".

like image 60
zacheusz Avatar answered Oct 25 '22 18:10

zacheusz


You can use org.objectweb.asm.Type.getInternalName(java.lang.Class).

like image 40
Slonopotamus Avatar answered Oct 25 '22 16:10

Slonopotamus