The basic description of ASM Learning 2-java class in ASM

Source: Internet
Author: User
Tags java reference

There are some basic concepts about ASM in the previous section, such as: The first class version in visit, the description of the method, the description of the reference, etc., which are described below.

Class version

A Java binary class file that has a version that corresponds to a different JDK version. Therefore, ASM provides several constants to specify the version of a class, which are defined in the Org.objectweb.asm.Opcodes interface as follows:

Class version identification in ASM corresponding JDK version
public static final int v1_1 = 196653;
public static final int v1_2 = 46; 1.2
public static final int v1_3 = 47; 1.3
public static final int v1_4 = 48; 1.4
public static final int v1_5 = 49; 1.5
public static final int v1_6 = 50; 1.6
public static final int v1_7 = 51; 1.7
public static final int v1_8 = 52; 1.8
Internal name

The internal name of the JVM is used in the Java binaries, not the "." That we are familiar with. The fully qualified name of the split, and the internal name is replaced with "/". 's full name. For example, the internal name of java.lang.String in the JVM is java/lang/string.

The static method Getinternalname (final Class C) in the Org.objectweb.asm.Type class can be used in ASM to obtain the following:

Package Net.zq.test;import Org.objectweb.asm.type;public class Ttt{public static void Main (string[] args) { System.out.println (Type.getinternalname (String.class)); System.out.println (Type.getinternalname (Integer.class)); System.out.println (Type.getinternalname (Ttt.class));}}

Operation Result:

Java/lang/stringjava/lang/integernet/zq/test/ttt
Type description

We know that Java types are classified as primitive types and reference types, and each type has a type description in the JVM that corresponds to it.

The basic types are described below:

Java Basic Types
Description in the JVM
Boolean Z
Byte B
Char C
Short S
Int I
Long J
Float F
Double D

The reference type is described as follows:

The general reference type is: "L this type class descriptor; ”。 Such as: The field descriptor of String type is ljava/lang/string;

For an array, it is: "[the domain descriptor of its type;". such as: int[] whose descriptor is [I

Multidimensional arrays are: n [a domain descriptor of that type, n represents a few-dimensional array. such as: int [] [] whose descriptor is [[I

Java reference types Description in the JVM
String ljava/lang/string;
Object Ljava/lang/object;
Int[] [I
Float[] [F
String[] [Ljava/lang/string;
Object[] [Ljava/lang/object;
int[[] [[I
float[[] [[F

To get the internal description of the class in ASM, you can use the GetDescriptor (final Class C) method in the Org.objectweb.asm.Type class, as follows:

Package Net.zq.test;import Org.objectweb.asm.type;public class Ttt{public static void Main (string[] args) {                  System.out.println (Type.getdescriptor (Ttt.class));  System.out.println (Type.getdescriptor (String.class)); }}

Operation Result:

LNET/ZQ/TEST/TTT; ljava/lang/string;
Method description

Divided into two parts:

    1. Parameter part in parentheses: Enclose the parameter description in parentheses .

    2. Return value type part: The descriptor of the return value value type

    3. The combination is: (the descriptor of the parameter-in the original order) returns the type descriptor

      1. such as: void a (int i,float f) is expressed as (if) V

    4. For no return value, use V (for void) to indicate

as follows:

method description for Java layer
method description in binary file
string test ()
ljava/lang/string;
void A (int i,float f) (IF) V
void A (Object o) (ljava/lang/object;) V
int A (int i,string s) (iljava/lang/string;) I
int[] A (int[] I) ([i) [i
void set (byte[] bytes) ([B) V

Get a description of a method, obtained in ASM with Org.objectweb.asm.Type.getMethodDescriptor, as follows:

Package Net.zq.test;import Org.objectweb.asm.type;public class Ttt{public static void Main (string[] args) throws Nosuchmethodexception, Securityexception{java.lang.reflect.method m = String.class.getMethod ("substring", int.class                  );   System.out.println (Type.getmethoddescriptor (m)); }}

The results of the operation are as follows:

(I) ljava/lang/string;

In the Org.objectweb.asm.Type class, there are many other ways to get to know a class that is interesting to look at and can be helpful for understanding the class.

The basic description of ASM Learning 2-java class in ASM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.