### Primitive Example with BSM Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk11.AllInstructions.txt Demonstrates a primitive example using a bootstrap method (BSM) for a constant dynamic. ```asm // access flags 0x9 public static primitiveExample()J LDC test : J [ // handle kind 0x6 : INVOKESTATIC jdk11/AllInstructions.bsm(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)J // arguments: none ] LRETURN MAXSTACK = 2 MAXLOCALS = 0 ``` -------------------------------- ### visitCode Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-9.4.txt Starts the code array of the method. ```APIDOC ## meth public void visitCode() ### Description Starts the code array of the method. ### Method public ### Signature void visitCode() ``` -------------------------------- ### Field Access and Modification Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk3.AllInstructions.txt Demonstrates instructions for getting and putting values into instance and static fields. Used for interacting with object properties and class-level variables. ```asm ALOAD 0 GETFIELD jdk3/AllInstructions.field : Ljdk3/AllInstructions; ASTORE 1 L1 LINENUMBER 193 L1 ALOAD 0 GETSTATIC jdk3/AllInstructions.staticField : Ljdk3/AllInstructions; PUTFIELD jdk3/AllInstructions.field : Ljdk3/AllInstructions; L2 LINENUMBER 194 L2 ALOAD 1 PUTSTATIC jdk3/AllInstructions.staticField : Ljdk3/AllInstructions; L3 LINENUMBER 195 L3 RETURN ``` -------------------------------- ### Gnarly Condy Pop Example Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk11.AllInstructions.txt Demonstrates the use of a constant dynamic with 'anotherBsm' and the POP2 instruction. ```asm // access flags 0x9 public static gnarlyCondyPop()V LDC test : J [ // handle kind 0x6 : INVOKESTATIC jdk11/AllInstructions.anotherBsm(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)J // arguments: none ] POP2 RETURN MAXSTACK = 2 MAXLOCALS = 0 ``` -------------------------------- ### invokedynamic Instruction Example Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk8.AllInstructions.txt Demonstrates the use of the invokedynamic instruction to create and use a lambda expression for list processing. It involves creating a list of strings and then using a Consumer to process them. ```asm // access flags 0x1 // signature (Ljava/util/List;)V // declaration: void invokedynamic(java.util.List) public invokedynamic(Ljava/util/List;)V // parameter strings L0 LINENUMBER 12 L0 ICONST_3 ANEWARRAY java/lang/String DUP ICONST_0 LDC "a" AASTORE DUP ICONST_1 LDC "b" AASTORE DUP ICONST_2 LDC "c" AASTORE INVOKESTATIC java/util/Arrays.asList ([Ljava/lang/Object;)Ljava/util/List; ASTORE 2 L1 LINENUMBER 13 L1 ALOAD 1 ALOAD 2 INVOKEDYNAMIC accept(Ljava/util/List;)Ljava/util/function/Consumer; [ // handle kind 0x6 : INVOKESTATIC java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; // arguments: (Ljava/lang/Object;)V, // handle kind 0x6 : INVOKESTATIC jdk8/AllInstructions.lambda$invokedynamic$0(Ljava/util/List;Ljava/lang/String;)V, (Ljava/lang/String;)V ] INVOKEINTERFACE java/util/List.forEach (Ljava/util/function/Consumer;)V (itf) L2 LINENUMBER 17 L2 RETURN L3 LOCALVARIABLE this Ljdk8/AllInstructions; L0 L3 0 LOCALVARIABLE strings Ljava/util/List; L0 L3 1 // signature Ljava/util/List; // declaration: strings extends java.util.List LOCALVARIABLE validStrings Ljava/util/List; L1 L3 2 // signature Ljava/util/List; // declaration: validStrings extends java.util.List MAXSTACK = 4 MAXLOCALS = 3 ``` -------------------------------- ### ASM Bytecode for TRYCATCHBLOCK Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Defines a try-catch block, specifying the start and end of the try region, the handler start, and the exception type. ```asm TRYCATCHBLOCK L0 L1 L2 java/lang/NullPointerException ``` -------------------------------- ### Creating Method Instructions via MethodVisitor Methods Source: https://github.com/asm/asm/blob/master/asm-tree/src/main/java/org/objectweb/asm/tree/package.html Shows how to use MethodNode's MethodVisitor methods to create and add instructions to its instruction list. ```java MethodNode methodNode = new MethodNode(...); methodNode.visitVarInsn(ALOAD, 0); ... ``` -------------------------------- ### Creating Method Instructions Sequentially Source: https://github.com/asm/asm/blob/master/asm-tree/src/main/java/org/objectweb/asm/tree/package.html Demonstrates creating method instructions by instantiating XxxInsnNode objects and adding them to a MethodNode's instruction list. ```java MethodNode methodNode = new MethodNode(...); methodNode.instructions.add(new VarInsnNode(ALOAD, 0)); ... ``` -------------------------------- ### Main Method with LambdaMetafactory Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk11.AllInstructions.txt The main method demonstrates creating a Runnable using LambdaMetafactory, executing a lambda expression. ```asm // access flags 0x9 public static main([Ljava/lang/String;)V LDC run : Ljava/lang/Runnable; [ // handle kind 0x6 : INVOKESTATIC java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/Object; // arguments: ()V, // handle kind 0x6 : INVOKESTATIC jdk11/AllInstructions.lambda$main$0()V, ()V ] ASTORE 1 ALOAD 1 INVOKEINTERFACE java/lang/Runnable.run ()V (itf) RETURN MAXSTACK = 1 MAXLOCALS = 2 ``` -------------------------------- ### ASM Bytecode for LOCALVARIABLE directive Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Defines local variables with their types, start and end program counters, and indices. ```asm LOCALVARIABLE t Ljava/lang/Throwable; L9 L3 3 LOCALVARIABLE this Ljdk5/AllInstructions; L6 L10 0 LOCALVARIABLE v0 I L6 L10 1 LOCALVARIABLE u0 I L0 L10 2 MAXSTACK = 2 MAXLOCALS = 6 ``` -------------------------------- ### Method Invocation and Object Instantiation Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk3.AllInstructions.txt Illustrates the process of creating a new object instance using NEW and INVOKESPECIAL, and preparing it for further operations. ```asm NEW jdk3/AllInstructions DUP INVOKESPECIAL jdk3/AllInstructions. ()V ASTORE 2 ``` -------------------------------- ### org.objectweb.asm.commons.CodeSizeEvaluator Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-5.0.4.txt Evaluates the minimum and maximum size of the bytecode instructions visited. It extends MethodVisitor and provides methods to get the computed sizes. ```APIDOC ## Class: org.objectweb.asm.commons.CodeSizeEvaluator ### Description This class evaluates the minimum and maximum size of the bytecode instructions visited. It extends `org.objectweb.asm.MethodVisitor` and provides methods to get the computed sizes. ### Methods - `getMaxSize()`: Returns the maximum size of the code in bytes. - `getMinSize()`: Returns the minimum size of the code in bytes. - `visitInvokeDynamicInsn(java.lang.String name, java.lang.String descriptor, org.objectweb.asm.Handle bootstrapMethodHandle, java.lang.Object... bootstrapMethodArguments)`: Visits an `INVOKEDYNAMIC` instruction. - `visitTableSwitchInsn(int min, int max, org.objectweb.asm.Label dflt, org.objectweb.asm.Label... labels)`: Visits a table switch instruction. - `visitFieldInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor)`: Visits a field instruction. - `visitIincInsn(int var, int increment)`: Visits an `IINC` instruction. - `visitInsn(int opcode)`: Visits an instruction without arguments. - `visitIntInsn(int opcode, int operand)`: Visits an instruction with a single integer operand. - `visitJumpInsn(int opcode, org.objectweb.asm.Label label)`: Visits a jump instruction. - `visitLdcInsn(java.lang.Object cst)`: Visits a `LDC` instruction. - `visitLookupSwitchInsn(org.objectweb.asm.Label dflt, int[] keys, org.objectweb.asm.Label[] labels)`: Visits a lookup switch instruction. - `visitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor)`: Visits a method instruction. - `visitMethodInsn(int opcode, java.lang.String owner, java.lang.String name, java.lang.String descriptor, boolean isInterface)`: Visits a method instruction. - `visitMultiANewArrayInsn(java.lang.String descriptor, int numDimensions)`: Visits a multi-dimensional array allocation instruction. - `visitTypeInsn(int opcode, java.lang.String descriptor)`: Visits a type instruction. - `visitVarInsn(int opcode, int var)`: Visits a local variable instruction. ``` -------------------------------- ### Class Initialization Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Initializes a new instance of the AllInstructions class. This is a standard constructor. ```asm // access flags 0x0 ()V L0 LINENUMBER 45 L0 ALOAD 0 INVOKESPECIAL java/lang/Object. ()V RETURN L1 LOCALVARIABLE this Ljdk5/AllInstructions; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 ``` -------------------------------- ### INVOKEVIRTUAL, INVOKESTATIC, INVOKEINTERFACE, RETURN Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk3.AllInstructions.txt Demonstrates method invocation and return instructions. ```asm INVOKEVIRTUAL jdk3/AllInstructions.fieldInstructions ()V L2 LINENUMBER 200 L2 ALOAD 2 INVOKESTATIC jdk3/AllInstructions.monitorInstructions (Ljava/lang/Object;)Ljava/lang/String; POP L3 LINENUMBER 201 L3 ALOAD 1 INVOKEINTERFACE java/lang/Runnable.run ()V (itf) L4 LINENUMBER 202 L4 RETURN L5 LOCALVARIABLE this Ljdk3/AllInstructions; L0 L5 0 LOCALVARIABLE v0 Ljava/lang/Runnable; L0 L5 1 LOCALVARIABLE c Ljdk3/AllInstructions; L1 L4 2 MAXSTACK = 2 MAXLOCALS = 3 ``` -------------------------------- ### String Initialization with Conditional Logic Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk8.AllFrames.txt Shows how to construct a String object conditionally based on a boolean flag. Handles potential UnsupportedEncodingException. ```asm // access flags 0x1 public m0([BZ)Ljava/lang/String; // parameter bytes // parameter b TRYCATCHBLOCK L0 L1 L2 java/io/UnsupportedEncodingException L0 LINENUMBER 100 L0 ALOAD 1 IFNONNULL L3 ACONST_NULL GOTO L1 L3 FRAME SAME NEW java/lang/String DUP ALOAD 1 ILOAD 2 IFEQ L4 LDC "a" GOTO L5 L4 FRAME FULL [jdk8/AllFrames [B I] [L3 L3 [B] LDC "b" L5 FRAME FULL [jdk8/AllFrames [B I] [L3 L3 [B java/lang/String] INVOKESPECIAL java/lang/String. ([BLjava/lang/String;)V L1 FRAME SAME1 java/lang/String ARETURN L2 LINENUMBER 101 L2 FRAME SAME1 java/io/UnsupportedEncodingException ASTORE 3 L6 LINENUMBER 102 L6 ACONST_NULL ARETURN L7 LOCALVARIABLE e Ljava/io/UnsupportedEncodingException; L6 L7 3 LOCALVARIABLE this Ljdk8/AllFrames; L0 L7 0 LOCALVARIABLE bytes [B L0 L7 1 LOCALVARIABLE b Z L0 L7 2 MAXSTACK = 4 MAXLOCALS = 4 ``` -------------------------------- ### Constructor for jdk11/AllInstructions Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk11.AllInstructions.txt Standard constructor for the AllInstructions class, initializing from java.lang.Object. ```asm // access flags 0x1 public ()V ALOAD 0 INVOKESPECIAL java/lang/Object. ()V RETURN MAXSTACK = 1 MAXLOCALS = 1 ``` -------------------------------- ### Constructor for AllInstructions Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk8.AllInstructions.txt Standard constructor for the AllInstructions class. Initializes the object and sets up local variables. ```asm // class version 52.0 (52) // access flags 0x20 class jdk8/AllInstructions { // compiled from: AllInstructions.java // access flags 0x19 public final static INNERCLASS java/lang/invoke/MethodHandles$Lookup java/lang/invoke/MethodHandles Lookup // access flags 0x0 ()V L0 LINENUMBER 10 L0 ALOAD 0 INVOKESPECIAL java/lang/Object. ()V RETURN L1 LOCALVARIABLE this Ljdk8/AllInstructions; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 ``` -------------------------------- ### Apply Code Formatting Source: https://github.com/asm/asm/blob/master/README.md Fix code formatting errors across all submodules using the spotlessApply task. ```shell ./gradle/gradlew spotlessApply ``` -------------------------------- ### Inserting Instructions at a Saved Pointer Source: https://github.com/asm/asm/blob/master/asm-tree/src/main/java/org/objectweb/asm/tree/package.html Illustrates using InsnList's insert() and insertBefore() methods to insert instructions at a specific point in the instruction list. ```java MethodNode methodNode = new MethodNode(...); methodNode.visitVarInsn(ALOAD, 0); AbstractInsnNode ptr = methodNode.instructions.getLast(); methodNode.visitVarInsn(ALOAD, 1); // inserts an instruction between ALOAD 0 and ALOAD 1 methodNode.instructions.insert(ptr, new VarInsnNode(ALOAD, 0)); ... ``` -------------------------------- ### Constructor with Parameters Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt A constructor for the AllInstructions class that accepts various primitive types and an Object. It calls the superclass constructor. ```asm // access flags 0x0 (IFJDLjava/lang/Object;)V L0 LINENUMBER 47 L0 ALOAD 0 INVOKESPECIAL java/lang/Object. ()V RETURN L1 LOCALVARIABLE this Ljdk5/AllInstructions; L0 L1 0 LOCALVARIABLE v0 I L0 L1 1 LOCALVARIABLE v1 F L0 L1 2 LOCALVARIABLE v2 J L0 L1 3 LOCALVARIABLE v3 D L0 L1 5 LOCALVARIABLE v4 Ljava/lang/Object; L0 L1 7 MAXSTACK = 1 MAXLOCALS = 8 ``` -------------------------------- ### Basic Usage of Analyzer Source: https://github.com/asm/asm/blob/master/asm-analysis/src/main/java/org/objectweb/asm/tree/analysis/package.html Demonstrates the fundamental steps to analyze a method's bytecode using the Analyzer and BasicInterpreter. This involves reading bytecode, parsing it into a ClassNode, and then iterating through methods to perform analysis. ```Java ClassReader classReader = new ClassReader(bytecode); ClassNode classNode = new ClassNode(); classReader.accept(classNode, ClassReader.SKIP_DEBUG); for (MethodNode method : classNode.methods) { if (method.instructions.size() > 0) { Analyzer analyzer = new Analyzer(new BasicInterpreter()); analyzer.analyze(classNode.name, method); Frame[] frames = analyzer.getFrames(); // Elements of the frames array now contains info for each instruction // from the analyzed method. BasicInterpreter creates BasicValue, that // is using simplified type system that distinguishes the UNINITIALZED, // INT, FLOAT, LONG, DOUBLE, REFERENCE and RETURNADDRESS types. ... } } ``` -------------------------------- ### Build ASM Project Source: https://github.com/asm/asm/blob/master/README.md Build the entire ASM project. Requires Java 11+. ```shell ./gradle/gradlew clean build ``` -------------------------------- ### Inserting Instructions by Iterating Over an Array Source: https://github.com/asm/asm/blob/master/asm-tree/src/main/java/org/objectweb/asm/tree/package.html Shows how to insert instructions by converting an instruction list to an array and iterating through its elements. ```java AbstractInsnNode[] insns = methodNode.instructions.toArray(); for(int i = 0; i ()V ASTORE 2 L1 LINENUMBER 209 L1 ALOAD 2 INVOKEVIRTUAL jdk5/AllInstructions.fieldInstructions ()V L2 LINENUMBER 210 L2 ALOAD 2 INVOKESTATIC jdk5/AllInstructions.monitorInstructions (Ljava/lang/Object;)Ljava/lang/String; POP L3 LINENUMBER 211 L3 ALOAD 1 INVOKEINTERFACE java/lang/Runnable.run ()V (itf) L4 LINENUMBER 212 L4 RETURN L5 LOCALVARIABLE this Ljdk5/AllInstructions; L0 L5 0 LOCALVARIABLE v0 Ljava/lang/Runnable; L0 L5 1 LOCALVARIABLE c Ljdk5/AllInstructions; L1 L5 2 MAXSTACK = 2 MAXLOCALS = 3 ``` -------------------------------- ### Method Implementation with Handles and Constants Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk11.AllInstructions.txt Implements a method using INVOKESTATIC and LDC with a constant dynamic, demonstrating handle usage. ```asm // access flags 0x1 public m()Ljava/lang/Object; LDC name : Ljava/lang/Object; [ // handle kind 0x6 : INVOKESTATIC jdk11/HandleOwner.handleField(Ljava/lang/Object;)Ljava/lang/Object; // arguments: // constant dynamic: argumentName : Ljava/lang/Object; [ // handle kind 0x2 : GETSTATIC jdk11/ArgumentHandleOwner.argumentHandleName(Ljava/lang/Object;) // arguments: none ] ] ARETURN MAXSTACK = 1 MAXLOCALS = 1 ``` -------------------------------- ### Reading and Loading a Class into a ClassNode Source: https://github.com/asm/asm/blob/master/asm-tree/src/main/java/org/objectweb/asm/tree/package.html Demonstrates how to read bytecode from a source and load it into a ClassNode object for in-memory manipulation. ```java ClassReader classReader = new ClassReader(source); ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); ``` -------------------------------- ### Loading Class Constants Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Demonstrates loading Class constants using the LDC instruction and storing them into fields. ```asm // access flags 0x1 public ldcWithClassConstant()V L0 LINENUMBER 51 L0 ALOAD 0 LDC Ljdk5/AllInstructions;.class PUTFIELD jdk5/AllInstructions.c : Ljava/lang/Class; L1 LINENUMBER 52 L1 ALOAD 0 LDC [Ljdk5/AllInstructions;.class PUTFIELD jdk5/AllInstructions.d : Ljava/lang/Class; L2 LINENUMBER 53 L2 RETURN L3 LOCALVARIABLE this Ljdk5/AllInstructions; L0 L3 0 MAXSTACK = 2 MAXLOCALS = 1 ``` -------------------------------- ### run Method for jdk3.AllStructures$1 Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk3.AllStructures$1.txt The run method creates and initializes an instance of jdk3.AllStructures$InnerClass, involving several static access methods from the outer class. ```asm // access flags 0x1 public run()V L0 LINENUMBER 42 L0 NEW jdk3/AllStructures$InnerClass DUP ALOAD 0 GETFIELD jdk3/AllStructures$1.this$0 : Ljdk3/AllStructures; NEW jdk3/AllStructures$InnerClass DUP ALOAD 0 GETFIELD jdk3/AllStructures$1.this$0 : Ljdk3/AllStructures; ALOAD 0 GETFIELD jdk3/AllStructures$1.this$0 : Ljdk3/AllStructures; INVOKESTATIC jdk3/AllStructures.access$000 (Ljdk3/AllStructures;)D ALOAD 0 GETFIELD jdk3/AllStructures$1.this$0 : Ljdk3/AllStructures; INVOKESTATIC jdk3/AllStructures.access$100 (Ljdk3/AllStructures;)D DADD ACONST_NULL INVOKESPECIAL jdk3/AllStructures$InnerClass. (Ljdk3/AllStructures;DLjdk3/AllStructures$1;)V INVOKESTATIC jdk3/AllStructures$InnerClass.access$300 (Ljdk3/AllStructures$InnerClass;)D ACONST_NULL INVOKESPECIAL jdk3/AllStructures$InnerClass. (Ljdk3/AllStructures;DLjdk3/AllStructures$1;)V POP L1 LINENUMBER 43 L1 RETURN L2 LOCALVARIABLE this Ljdk3/AllStructures$1; L0 L2 0 MAXSTACK = 10 MAXLOCALS = 1 } ``` -------------------------------- ### MethodNode Constructors Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-4.1.txt Provides information on how to create new MethodNode instances. ```APIDOC ## MethodNode Constructors ### Description Constructors for creating MethodNode objects. ### Constructors - `public init(int)` - `public init(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])` - `public init(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])` ``` -------------------------------- ### InstructionAdapter Methods Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-5.0.4.txt This section details the public methods available in the InstructionAdapter class for generating bytecode. ```APIDOC ## InstructionAdapter This class extends `org.objectweb.asm.commons.LocalVariablesSorter` and provides methods for generating bytecode instructions. ### Methods - **`aconst(java.lang.Object)`**: Pushes a constant onto the stack. - **`arrayLength()`**: Pushes the length of an array onto the stack. - **`arrayLoad(org.objectweb.asm.Type)`**: Loads an element from an array onto the stack. - **`arrayStore(org.objectweb.asm.Type)`**: Stores an element into an array. - **`box(org.objectweb.asm.Type)`**: Boxes a primitive value into its corresponding wrapper object. - **`cast(org.objectweb.asm.Type, org.objectweb.asm.Type)`**: Casts a value from one type to another. - **`catchException(org.objectweb.asm.Label, org.objectweb.asm.Label, org.objectweb.asm.Type)`**: Marks the beginning and end of an exception handler. - **`checkCast(org.objectweb.asm.Type)`**: Checks if an object can be cast to a specific type. - **`dup()`**: Duplicates the top value on the stack. - **`dup2()`**: Duplicates the top two values on the stack. - **`dup2X1()`**: Duplicates the top two values on the stack and inserts them between the top and second-top values. - **`dup2X2()`**: Duplicates the top two values on the stack and inserts them appropriately. - **`dupX1()`**: Duplicates the top value on the stack and inserts it between the top and second-top values. - **`dupX2()`**: Duplicates the top value on the stack and inserts it appropriately. - **`endMethod()`**: Marks the end of the method. - **`getField(org.objectweb.asm.Type, java.lang.String, org.objectweb.asm.Type)`**: Gets the value of an instance field. - **`getStatic(org.objectweb.asm.Type, java.lang.String, org.objectweb.asm.Type)`**: Gets the value of a static field. - **`goTo(org.objectweb.asm.Label)`**: Unconditional jump to a label. - **`ifCmp(org.objectweb.asm.Type, int, org.objectweb.asm.Label)`**: Compares two values and jumps if they are equal. - **`ifICmp(int, org.objectweb.asm.Label)`**: Compares two integers and jumps if they are equal. - **`ifNonNull(org.objectweb.asm.Label)`**: Jumps to a label if the top of the stack is not null. - **`ifNull(org.objectweb.asm.Label)`**: Jumps to a label if the top of the stack is null. - **`ifZCmp(int, org.objectweb.asm.Label)`**: Compares a value with zero and jumps if the condition is met. - **`iinc(int, int)`**: Increments a local variable by a constant value. - **`instanceOf(org.objectweb.asm.Type)`**: Checks if an object is an instance of a specific type. - **`invokeConstructor(org.objectweb.asm.Type, org.objectweb.asm.commons.Method)`**: Invokes a constructor. - **`invokeInterface(org.objectweb.asm.Type, org.objectweb.asm.commons.Method)`**: Invokes an interface method. - **`invokeStatic(org.objectweb.asm.Type, org.objectweb.asm.commons.Method)`**: Invokes a static method. - **`invokeVirtual(org.objectweb.asm.Type, org.objectweb.asm.commons.Method)`**: Invokes a virtual method. - **`loadArg(int)`**: Loads an argument of a method onto the stack. - **`loadArgArray()`**: Loads all arguments of a method onto the stack as an array. - **`loadArgs()`**: Loads all arguments of a method onto the stack. - **`loadArgs(int, int)`**: Loads a range of arguments of a method onto the stack. - **`loadLocal(int)`**: Loads a local variable onto the stack. - **`loadLocal(int, org.objectweb.asm.Type)`**: Loads a local variable of a specific type onto the stack. - **`loadThis()`**: Loads the `this` reference onto the stack. - **`mark(org.objectweb.asm.Label)`**: Marks a label in the code. - **`math(int, org.objectweb.asm.Type)`**: Performs a mathematical operation. - **`monitorEnter()`**: Enters a synchronized block. - **`monitorExit()`**: Exits a synchronized block. - **`newArray(org.objectweb.asm.Type)`**: Creates a new array. - **`newInstance(org.objectweb.asm.Type)`**: Creates a new instance of a class. - **`not()`**: Negates the top value on the stack. - **`pop()`**: Removes the top value from the stack. - **`pop2()`**: Removes the top two values from the stack. - **`push(boolean)`**: Pushes a boolean value onto the stack. - **`push(double)`**: Pushes a double value onto the stack. - **`push(float)`**: Pushes a float value onto the stack. - **`push(int)`**: Pushes an int value onto the stack. - **`push(java.lang.String)`**: Pushes a string value onto the stack. - **`push(long)`**: Pushes a long value onto the stack. - **`push(org.objectweb.asm.Handle)`**: Pushes a Handle onto the stack. - **`push(org.objectweb.asm.Type)`**: Pushes a Type onto the stack. - **`putField(org.objectweb.asm.Type, java.lang.String, org.objectweb.asm.Type)`**: Sets the value of an instance field. - **`putStatic(org.objectweb.asm.Type, java.lang.String, org.objectweb.asm.Type)`**: Sets the value of a static field. - **`ret(int)`**: Returns from a method. - **`returnValue()`**: Returns the value on top of the stack. - **`storeArg(int)`**: Stores a value from the stack into a method argument. - **`storeLocal(int)`**: Stores a value from the stack into a local variable. - **`storeLocal(int, org.objectweb.asm.Type)`**: Stores a value from the stack into a local variable of a specific type. - **`swap()`**: Swaps the top two values on the stack. - **`swap(org.objectweb.asm.Type, org.objectweb.asm.Type)`**: Swaps two values on the stack. - **`tableSwitch(int[], org.objectweb.asm.commons.TableSwitchGenerator)`**: Performs a switch operation based on an integer value. - **`tableSwitch(int[], org.objectweb.asm.commons.TableSwitchGenerator, boolean)`**: Performs a switch operation with an additional boolean parameter. - **`throwException()`**: Throws an exception. - **`throwException(org.objectweb.asm.Type, java.lang.String)`**: Throws an exception with a specific type and message. - **`unbox(org.objectweb.asm.Type)`**: Unboxes a wrapper object into its corresponding primitive value. - **`valueOf(org.objectweb.asm.Type)`**: Returns the value of a primitive type. - **`visitInvokeDynamicInsn(java.lang.String, java.lang.String, org.objectweb.asm.Handle, java.lang.Object[])`**: Visits an `INVOKEDYNAMIC` instruction. - **`visitTableSwitchInsn(int, int, org.objectweb.asm.Label, org.objectweb.asm.Label[])`**: Visits a `TABLESWITCH` instruction. ``` -------------------------------- ### Instance Method for Integer Comparison and Negation Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk8.AllFrames.txt Compares an integer input 'i' with zero. If 'i' is negative, it is negated. The method then proceeds based on this normalized value. ```asm // access flags 0x1 public m3(I)I // parameter i L0 LINENUMBER 162 L0 ILOAD 1 IFGE L1 L2 LINENUMBER 163 L2 ILOAD 1 ILOAD 1 ``` -------------------------------- ### ASM Bytecode for IINC, ILOAD, IRETURN Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Illustrates incrementing a local variable, loading its value, and returning it. ```asm LINENUMBER 275 L4 IINC 2 1 ILOAD 4 IRETURN ``` -------------------------------- ### Generic Method Signature and Body Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllStructures.txt Illustrates a complex generic method signature with various wildcard types and bounds, followed by a simple return instruction. This shows how ASM represents detailed generic information. ```asm genericMethod(Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljdk5/AllStructures$InnerClass;Ljdk5/AllStructures$GenericInnerClass;)V throws java/lang/Exception java/io/IOException L0 LINENUMBER 130 L0 RETURN L1 LOCALVARIABLE this Ljdk5/AllStructures; L0 L1 0 // signature Ljdk5/AllStructures; // declaration: this extends jdk5.AllStructures LOCALVARIABLE p0 Ljava/util/List; L0 L1 1 // signature Ljava/util/List; // declaration: p0 extends java.util.List LOCALVARIABLE p1 Ljava/util/List; L0 L1 2 // signature Ljava/util/List<[TU1;>; // declaration: p1 extends java.util.List LOCALVARIABLE p2 Ljava/util/List; L0 L1 3 // signature Ljava/util/List<[[TU2;>; // declaration: p2 extends java.util.List LOCALVARIABLE p3 Ljava/util/List; L0 L1 4 // signature Ljava/util/List; // declaration: p3 extends java.util.List LOCALVARIABLE p4 Ljava/util/List; L0 L1 5 // signature Ljava/util/List; // declaration: p4 extends java.util.List LOCALVARIABLE p5 Ljava/util/List; L0 L1 6 // signature Ljava/util/List; // declaration: p5 extends java.util.List LOCALVARIABLE p6 Ljava/util/List; L0 L1 7 // signature Ljava/util/List; // declaration: p6 extends java.util.List LOCALVARIABLE p7 Ljdk5/AllStructures$InnerClass; L0 L1 8 // signature Ljdk5/AllStructures.InnerClass; // declaration: p7 extends jdk5.AllStructures.InnerClass LOCALVARIABLE p8 Ljdk5/AllStructures$GenericInnerClass; L0 L1 9 // signature Ljdk5/AllStructures.GenericInnerClass; // declaration: p8 extends jdk5.AllStructures.GenericInnerClass MAXSTACK = 0 MAXLOCALS = 10 ``` -------------------------------- ### ASM Bytecode for ASTORE Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Demonstrates storing a value into a local variable. ```asm ASTORE 5 ``` -------------------------------- ### ASM Bytecode for jsrAndRetInstructions Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk3.AllInstructions.txt Illustrates the jsr (jump subroutine) and ret (return from subroutine) instructions, including exception handling. ```asm TRYCATCHBLOCK L0 L1 L2 java/lang/Throwable TRYCATCHBLOCK L0 L3 L3 null L4 LINENUMBER 266 L4 ILOAD 1 ICONST_1 IADD ISTORE 2 L0 LINENUMBER 268 L0 ALOAD 0 ILOAD 2 INVOKEVIRTUAL jdk3/AllInstructions.jsrAndRetInstructions (I)I ISTORE 2 L1 JSR L5 GOTO L6 L2 LINENUMBER 270 L2 ASTORE 3 ICONST_M1 ISTORE 4 JSR L5 ILOAD 4 IRETURN L3 LINENUMBER 272 L3 ASTORE 5 JSR L5 ALOAD 5 ATHROW L5 ASTORE 6 IINC 2 1 RET 6 L6 LINENUMBER 274 L6 ILOAD 2 IRETURN L7 LOCALVARIABLE this Ljdk3/AllInstructions; L4 L7 0 LOCALVARIABLE v0 I L4 L7 1 LOCALVARIABLE u0 I L0 L7 2 LOCALVARIABLE t Ljava/lang/Throwable; L2 L6 3 MAXSTACK = 2 MAXLOCALS = 7 ``` -------------------------------- ### MethodVisitor Class Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/sigtest-9.4.txt Documentation for the abstract org.objectweb.asm.MethodVisitor class. ```APIDOC ## MethodVisitor Class ### Description Documentation for the abstract org.objectweb.asm.MethodVisitor class, used for visiting methods. ### Fields - **api**: protected final int api - **mv**: protected org.objectweb.asm.MethodVisitor mv ### Constructors - **init(int)**: protected init(int) - **init(int, org.objectweb.asm.MethodVisitor)**: protected init(int, org.objectweb.asm.MethodVisitor) ### Methods - **visitInvokeDynamicInsn(java.lang.String, java.lang.String, org.objectweb.asm.Handle, java.lang.Object[])**: public !varargs void visitInvokeDynamicInsn(java.lang.String,java.lang.String,org.objectweb.asm.Handle,java.lang.Object[]) - **visitTableSwitchInsn(int, int, org.objectweb.asm.Label, org.objectweb.asm.Label[])**: public !varargs void visitTableSwitchInsn(int,int,org.objectweb.asm.Label,org.objectweb.asm.Label[]) - **visitAnnotation(java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitAnnotation(java.lang.String,boolean) - **visitAnnotationDefault()**: public org.objectweb.asm.AnnotationVisitor visitAnnotationDefault() - **visitInsnAnnotation(int, org.objectweb.asm.TypePath, java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitInsnAnnotation(int,org.objectweb.asm.TypePath,java.lang.String,boolean) - **visitLocalVariableAnnotation(int, org.objectweb.asm.TypePath, org.objectweb.asm.Label[], org.objectweb.asm.Label[], int[], java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitLocalVariableAnnotation(int,org.objectweb.asm.TypePath,org.objectweb.asm.Label[],org.objectweb.asm.Label[],int[],java.lang.String,boolean) - **visitParameterAnnotation(int, java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitParameterAnnotation(int,java.lang.String,boolean) - **visitTryCatchAnnotation(int, org.objectweb.asm.TypePath, java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitTryCatchAnnotation(int,org.objectweb.asm.TypePath,java.lang.String,boolean) - **visitTypeAnnotation(int, org.objectweb.asm.TypePath, java.lang.String, boolean)**: public org.objectweb.asm.AnnotationVisitor visitTypeAnnotation(int,org.objectweb.asm.TypePath,java.lang.String,boolean) - **getDelegate()**: public org.objectweb.asm.MethodVisitor getDelegate() - **visitAnnotableParameterCount(int, boolean)**: public void visitAnnotableParameterCount(int,boolean) - **visitAttribute(org.objectweb.asm.Attribute)**: public void visitAttribute(org.objectweb.asm.Attribute) - **visitCode()**: public void visitCode() - **visitEnd()**: public void visitEnd() ### Superclass java.lang.Object ``` -------------------------------- ### Local Class Constructor Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllStructures.txt Shows the bytecode for a constructor of a local class within another method. It involves creating a new instance of the local class and initializing it with specific arguments. ```asm localClassConstructor(Ljava/lang/String;)V L0 LINENUMBER 146 L0 NEW jdk5/AllStructures$1LocalClass DUP ALOAD 0 BIPUSH 42 ALOAD 1 INVOKESPECIAL jdk5/AllStructures$1LocalClass. (Ljdk5/AllStructures;ILjava/lang/String;)V POP L1 LINENUMBER 147 L1 RETURN L2 LOCALVARIABLE this Ljdk5/AllStructures; L0 L2 0 ``` -------------------------------- ### Monitor Enter and Exit Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Demonstrates the use of monitorenter and monitorexit for synchronized blocks. ```asm // access flags 0x9 public static monitorInstructions(Ljava/lang/Object;)Ljava/lang/String; TRYCATCHBLOCK L0 L1 L2 null TRYCATCHBLOCK L2 L3 L2 null L4 LINENUMBER 241 L4 ALOAD 0 DUP ASTORE 1 MONITORENTER L0 LINENUMBER 242 L0 ALOAD 0 INVOKEVIRTUAL java/lang/Object.toString ()Ljava/lang/String; ALOAD 1 MONITOREXIT L1 ARETURN L2 LINENUMBER 243 L2 ASTORE 2 ALOAD 1 MONITOREXIT L3 ALOAD 2 ATHROW L5 LOCALVARIABLE v0 Ljava/lang/Object; L4 L5 0 MAXSTACK = 2 MAXLOCALS = 3 ``` -------------------------------- ### ASM Bytecode for ATHROW Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk5.AllInstructions.txt Shows the instruction for throwing an exception. ```asm ATHROW ``` -------------------------------- ### accept Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-9.4.txt Accepts a visitor to visit this class. This method must be implemented in order to support the visitor pattern. ```APIDOC ## meth public void accept(org.objectweb.asm.ClassVisitor) ### Description Accepts a visitor to visit this class. This method must be implemented in order to support the visitor pattern. ### Method public ### Signature void accept(org.objectweb.asm.ClassVisitor) ``` ```APIDOC ## meth public void accept(org.objectweb.asm.MethodVisitor) ### Description Accepts a visitor to visit this method. This method must be implemented in order to support the visitor pattern. ### Method public ### Signature void accept(org.objectweb.asm.MethodVisitor) ``` -------------------------------- ### visitJumpInsn Source: https://github.com/asm/asm/blob/master/asm-commons/src/test/resources/sigtest-9.4.txt Visits a jump instruction. ```APIDOC ## meth public void visitJumpInsn(int,org.objectweb.asm.Label) ### Description Visits a jump instruction. ### Method public ### Signature void visitJumpInsn(int,org.objectweb.asm.Label) ``` -------------------------------- ### MethodVisitor Methods Source: https://github.com/asm/asm/blob/master/asm/src/test/resources/sigtest-9.4.txt This section lists the methods available on the MethodVisitor class for visiting various aspects of a method. ```APIDOC ## visitInvokeDynamicInsn ### Description Visits an instruction that invokes a dynamic method. This method is used for invoke dynamic instructions. ### Method INVOKEDYNAMIC ### Endpoint visitInvokeDynamicInsn(java.lang.String name, java.lang.String descriptor, org.objectweb.asm.Handle bootstrapMethodHandle, java.lang.Object[] bootstrapMethodArguments) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ## visitTableSwitchInsn ### Description Visits a table switch instruction. ### Method TABLESWITCH ### Endpoint visitTableSwitchInsn(int min, int max, org.objectweb.asm.Label defaultLabel, org.objectweb.asm.Label[] labels) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ## visitAnnotation ### Description Visits an annotation. This method can be called only once. ### Method ANNOTATION ### Endpoint visitAnnotation(java.lang.String descriptor, boolean visible) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ## visitAnnotationDefault ### Description Visits the default value of an annotation. This method can be called only once. ### Method ANNOTATION_DEFAULT ### Endpoint visitAnnotationDefault() ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ## visitInsnAnnotation ### Description Visits an instruction annotation. This method can be called only once. ### Method INS_ANNOTATION ### Endpoint visitInsnAnnotation(int typeRef, org.objectweb.asm.TypePath typePath, java.lang.String descriptor, boolean visible) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ## visitLocalVariableAnnotation ### Description Visits a local variable annotation. This method can be called only once. ### Method LOCAL_VARIABLE_ANNOTATION ### Endpoint visitLocalVariableAnnotation(int typeRef, org.objectweb.asm.TypePath typePath, org.objectweb.asm.Label[] start, org.objectweb.asm.Label[] end, int[] index, java.lang.String descriptor, boolean visible) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Static create Method for AllFrames Source: https://github.com/asm/asm/blob/master/asm-util/src/test/resources/jdk8.AllFrames.txt A static factory method to create an AllFrames instance. It handles null input for the String parameter by defaulting to an empty string. ```java public static create(Ljava/lang/String;)Ljdk8/AllFrames; // parameter s L0 LINENUMBER 56 L0 NEW jdk8/AllFrames DUP ACONST_NULL ALOAD 0 IFNONNULL L1 LDC "" GOTO L2 L1 FRAME FULL [java/lang/String] [L0 L0 N] ALOAD 0 L2 FRAME FULL [java/lang/String] [L0 L0 N java/lang/String] INVOKESPECIAL jdk8/AllFrames. (Ljava/lang/Object;Ljava/lang/String;)V ARETURN L3 LOCALVARIABLE s Ljava/lang/String; L0 L3 0 MAXSTACK = 4 MAXLOCALS = 1 ```