In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what is the use of the ASM framework in java", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of the ASM framework in java" this article.
Brief introduction
ASM is a java bytecode operation framework. Using this framework, we can not only dynamically modify the java running object code, but also realize the functions such as dynamic proxy.
Thread and stack frame
To understand ASM bytecode operation, you must first be familiar with jvm thread and stack frame structure. If jvm opens up a thread, it will open up the virtual machine stack, local method stack and program counter belonging to this thread. Its main functions are as follows:
Virtual machine stack: take the stack frame as the basic unit, the start address of a stack frame represents the entrance of a method, and the stack frame contains Operand stack, local variable table, dynamic link, method exit, and other information; it is described as follows:
Operand stack: used to store basic types and object references that need to be calculated by cpu, using 4-byte slot as the basic unit
Local variable table: stores the variable table involved in the current stack frame, with 4 bytes of slot as the basic unit
Dynamic link: stores the reference of the execution constant pool, which can realize the dynamic link of the current method
Method exit: used for method return, can be exception occurrence, return operation instruction, etc.
Local method stack: similar to the virtual machine stack, but holds some information about the java local code base
Program counter: save the execution location of the thread instruction, which can be executed next time when scheduling.
It is shown in the following figure:
Thread virtual machine stack structure. PNG
Basic type
The type expression in java bytecode is different from that in java code, and the following symbols are used to express basic types in java bytecode:
Java type type meaning booleanZ Boolean charC character byteB byte shortS short integer intI long integer floatF floating point referenceL class reference voidV empty doubleD double precision floating point ObjectLjava/lang/Object; object int [] [I integer array Object [] [[Ljava/lang/Object; object array "
Note: a reference (";" cannot be omitted) that represents a certain type of reference.
Bytecode example:
Java code bytecode indicates comments double [] [] [[D]
Object run (int iMagnum double ddirection Thread t) (IDLjava/lang/Thread) Ljava/lang/Object; (method parameter bytecode type) method returns parameter type bytecode instruction operation
In fact, the bytecode instruction operation mainly operates the local variable table and Operand stack, the specific process is: load local variables to the Operand stack, then give execution instructions to cpu, and then pop up the elements at the top of the Operand stack, so as to achieve an operation
All bytecode instructions have a certain format: [type+] op ["_" + value]
According to the basic type, type can be: I (int integer), s (short short integer), b (byte byte), c (char character), l (long long integer), d (double double precision floating point number), f (float floating point number), a (reference reference)
Value refers to operands. If the Operand is negative, you need to add an'm "prefix. For example, iconstant_m1 means to stack-1. If the Operand exceeds a certain size, the Operand will be stored in the constant pool with its location indicated by # indexbyte.
Op refers to the opcode, usually represented by one byte
Bytecode instruction operations are mainly divided into nine instructions:
Load and store instructions: used to transfer data back and forth between Operand stack and local variable table
Operation instruction: used to calculate the two values at the top of the Operand stack and then put them back into the top of the Operand stack
Type conversion instruction: used to convert two different numeric types to each other
Object creation and access instructions: for creating and accessing objects
Operand stack management instructions: used to manage Operand stacks, similar to normal stack management
Control transfer instruction: used to allow jvm to start execution from an instruction at a specified location instead of controlling the position of the next instruction
Method calls and return instructions: for method calls and method returns
Exception handling instructions: handling instructions for exceptions thrown by jvm
Method synchronization instruction: used to control the synchronization control of different threads to the method
Load and store instructions
The loading instruction is mainly to press local variables and constants into the Operand stack. The specific instructions are:
Constant pressure stack instruction, constant stack instruction is divided according to the size of bytes occupied by the constant, the instructions are as follows:
Constant: a value with a byte size of-1 to 5. For example, iconstant_0 stacks integers 0 and lconstant_5 stacks long integers 5.
Bipush: converts a value with a byte value of type byte to an integer, and then stacks (byte value size is-128127)
Sipush: converts a value with a byte value of type short to an integer, and then stacks (the value is-32768-32767)
Ldc: fetches a constant value with a size of-2147483648 from the constant pool according to the specified index value (indexbyte that requires one byte of storage), such as int,float, reference constant
Ldc_w: take constant values such as int,float,Reference from the constant pool to stack according to the specified wide index value (the index value that needs to be stored in two bytes)
Ldc2_w: take constant values such as long,double from the constant pool to stack according to the specified wide index value
As shown below:
Constant stack .png
As shown in the figure above, where 32767 represents the operation value, which will not be saved to the constant pool between [- 32768 and 32767]. If you exceed this value, you need to look it up in the constant pool using the index represented by indexbyte (# 30recoery 35).
Load: stack the object at the specified position (specific value or index) of the local variable; aload_0 stacks the reference type at the local variable table 0
Iload indexbyte stacks the int type represented by indexbyte in the local variable table; caload loads the value of the specified item from the char type array (first converted to int type value, then stack)
Store: pop up the top value of the Operand stack and save it to the local variable table; for example, istore_3 saves the short,byte,char,int type to the local variable table 3 according to type conversion, lstore [opNum] (opNum needs to be greater than 3) saves the long type to the local variable opNum; dstore is used to save the double type at the top of the stack, and fstore is used to save the float type at the top of the stack.
As shown in the following figure:
Load storage instruction .png
Operation instruction
There are several kinds of operation instructions:
(t) add: add two values of type T at the top of the stack to the stack.
(t) sub: subtract two values of type T at the top of the stack into the stack.
(t) mul: multiplies two values of type T at the top of the stack into the stack.
(t) div: divides two values of type T at the top of the stack into the stack.
(t) rem: two values of type T at the top of the stack are modularized and put into the stack.
(t) neg: the T type at the top of the stack is negative and put into the stack.
(t) iinc [indexbyte,constantbyte]: adds the integer value constbyte to a local variable of type int specified by indexbyte
Operation instruction. PNG
(t) shl: move the arithmetic to the left and enter the stack. T is the basic type of non-floating point type.
(t) shr: move the arithmetic to the left and enter the stack. T is the basic type of non-floating point type.
(t) ushl: logic moves left and enters the stack. T is the basic type of non-floating point type.
(t) ushr: move the logic to the right and enter the stack. T is the basic type of non-floating point type.
(t) and: and operation, T is the basic type of non-floating point type
(t) or: or operation, T is the basic type of non-floating point type
(t) xor: XOR operation, T is the basic type of non-floating point type
Type conversion instruction
There are several types of type conversion instructions:
(t) 2 (V): convert T basic type to V basic type. If long byte type converts short byte type, high-order bytes need to be truncated; for example, L2i: converting long to int will convert the remaining four bytes truncated to int.
Object creation and access instructions
Object creation and access instructions usually require two operands indexbyte1 and indexbyte2
New: create a new object instance
Checkcast: forcing type conversion
Instanceof: determine whether it is a class instance
GetField: gets the value of the class instance field
PutField: assign values to class instance fields
GetStatic: get the static variable value of the class
PutStatic: assign values to class static variables
Newarray: create an array of primitive types
Anewarray: creating an array of reference types
Arraylength: get the length of one-dimensional array
Operand stack management instruction
A word equals two bytes and half a slot,16 bit
Nop: null operation
Pop: one word length data at the top of the pop-up stack
Pop2: two words of data at the top of the pop-up stack
Dup: copy the data of one word at the top of the stack and put it on the stack at the same time
Dup_x1: copy the data of one word at the top of the stack, pop up the data of two words at the top of the stack at the same time, then put the copied data into the stack, and then put the two words that pop up into the stack
Dup_x2: copy the data of one word at the top of the stack, pop up the data of three words at the top of the stack at the same time, then put the copied data into the stack, and then put the three words that pop up into the stack.
Dup2: copy the two-word-long data at the top of the stack and put it on the stack at the same time
Dup2_x1: copy the two-word-long data at the top of the stack, pop up the three-word-long data at the top of the stack at the same time, then put the copied data into the stack, and then put the three words that pop up into the stack.
Dup2_x2: copy the two-word-long data at the top of the stack, pop up the four-word-long data at the top of the stack at the same time, then put the copied data into the stack, and then put the four words that pop up into the stack
Swap: two-word-long data at the top of the exchange stack. The Java instruction does not provide an exchange instruction in units of two-word length
Control transfer instruction:
Control transfer instructions are divided into jump instructions and comparison instructions, unconditional jump instructions, table jump instructions, abnormal jump instructions.
Jump instruction:
Ifeq: jump if the top int type is 0
Ifne: jump if the top int type is not 0
Iflt: jump if the top int type is less than 0
Ifle: jump if the top int type is less than or equal to 0
Ifgt: jump if the int type at the top of the stack is greater than 0
Ifge: jump if the top int type is greater than or equal to 0
If_icmpeq: jump if two int types at the top of the stack are equal
If_icmpne: jump if two int types at the top of the stack are equal
If_icmplt: jump if the top int of the stack is less than the back.
If_icpmle: jump if the top int of the stack is less than or equal to
If_icpmgt: jump if the int at the top of the stack is greater than the back
If_icpmge: jump if the top int is greater than or equal to
Ifnull: jump if the top reference of the stack is empty
Ifnonnull: jump if the top reference of the stack is not empty
If_acmpeq: jump if two references at the top of the stack are equal
If_acmpne: jump if two references at the top of the stack are not equal
Compare instructions:
(t) cmp: compare the size of the two T types at the top of the stack. If the former is large, it will enter the stack by 1; if it is equal, it will enter the stack by 0; and the latter will enter the stack by 1.
(t) cmpl: compare the size of the two T types at the top of the stack. If the former is large, 1 will enter the stack; if the former is equal, 0 will enter the stack; if the latter is large,-1 will enter the stack; if NAN exists,-1 will enter the stack.
(t) cmpg: compare the size of the two T types at the top of the stack. If the former is large, 1 will enter the stack; if the former is equal, 0 will enter the stack; if the latter is large,-1 will enter the stack; if NAN exists,-1 will enter the stack.
Unconditional transfer instruction:
Goto: transfer to a specified location unconditionally
Goto_w: unconditional transfer to a specified location (wide index)
Table jump instruction:
Tableswitch: access the jump table through the index and jump
Lookupswitch: access the jump table through healthy values, and jump to
Exception jump instruction:
Athrow: throwing an exception
Jsr: jump to the specified program
Jsr_w: jump to the specified program (wide index)
Ret: return to the specified program
Method calls and return instructions:
Invokerspecial: directive is used to call instance methods that require special handling, including instance initialization methods, private methods, and parent methods, and compile-time binding
Invokevirtual: directive is used to call the instance method of an object, dispatch according to the actual type of the object, and bind at run time
Invokestatic: calling static methods
Invokeinterface: used to call the interface method, search for an object that implements the interface method at run time, and find out the appropriate method to call
Invokedynamic: used to handle new method dispatch: it allows application-level code to determine which method call to execute, and this judgment is made only when the call is to be executed, thus achieving dynamic language support, and the lambda method implementation relies on this instruction.
(t) return: method exit instruction, T represents the return type
For invokespecial and invokevirtual, please see the following figure:
Invokespecial and invokevirtual.png
Synchronous method instruction:
Monitorenter: enter and get the object monitor
Monitorexit: exit and release the object monitor
The above is all the content of the article "what is the use of ASM Framework in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.