ASM - How to convert Java class name from Java bytecode name?

I use ASM (bytecode modification library) and provide access to type names in the bytecode naming format, for example, the String field contains a description: Ljava / lang / String

I need to call Class.forName for some classes, but I need a source code form for type names for this, for example. java.lang.String.

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

+6
source share
2 answers

I do not know any API method, but the conversion is quite simple. You can find the details in the JVM spec here . Primitive types are represented by a single character:

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

The types of classes and interfaces are represented by the full name, with the Prefix 'L' and ''; suffix. Points. in the name of a fully qualified class is replaced by "/" (for inner classes ".", which separates the name of the outer class from the inner class name, is replaced by "$"). Thus, the internal name of the String class will be "Ljava / lang / String;" and the inner name of the inner class "java.awt.geom.Arc2D.Float" will be "Ljava / awt / geom / Arc2D $ Float;".

Array names begin with an opening parenthesis '[', followed by the name of the component type (primitive or reference). Thus, "int []" becomes "[I" and "javax.swing.JFrame [] []" becomes "[[Ljavax.swing.JFrame;".

+7
source

Source: https://habr.com/ru/post/893353/


All Articles