Class AsmHelper


  • public final class AsmHelper
    extends Object
    Utility methods to deal with ASM bytecode
    Author:
    Mark Struberg
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.apache.xbean.asm9.Attribute[] ATTRS  
      static org.apache.xbean.asm9.Type TYPE_OBJECT  
    • Field Detail

      • TYPE_OBJECT

        public static final org.apache.xbean.asm9.Type TYPE_OBJECT
      • ATTRS

        public static final org.apache.xbean.asm9.Attribute[] ATTRS
    • Method Detail

      • readClassNode

        public static org.apache.xbean.asm9.tree.ClassNode readClassNode​(Class<?> clazz)
        Read the binary bytecode from the class with the given name
        Parameters:
        clazz - the class to read into the ClassNode
        Returns:
        the ClassNode constructed from that class
      • readClassNode

        public static org.apache.xbean.asm9.tree.ClassNode readClassNode​(ClassLoader classLoader,
                                                                         String className)
                                                                  throws ClassNotFoundException
        Read the binary bytecode from the class with the given name
        Parameters:
        classLoader - the ClassLoader to use
        className - the fully qualified class name to read. e.g. "org.mycorp.mypackage.MyEntity"
        Returns:
        the ClassNode constructed from that class
        Throws:
        ClassNotFoundException
      • getClassBytes

        public static byte[] getClassBytes​(String typeName)
      • toByteArray

        public static byte[] toByteArray​(ClassNodeTracker cnt)
        Create a byte[] of that class represented by the ClassNodeTracker
      • getMethodNode

        public static Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode​(org.apache.xbean.asm9.tree.ClassNode classNode,
                                                                                    Method meth)
      • getMethodNode

        public static Optional<org.apache.xbean.asm9.tree.MethodNode> getMethodNode​(org.apache.xbean.asm9.tree.ClassNode classNode,
                                                                                    String methodName,
                                                                                    Class<?> returnType,
                                                                                    Class<?>... paramTypes)
      • getReturnInsn

        public static int getReturnInsn​(Class<?> type)
        Calclates the proper Return instruction opcode for the given class
        Parameters:
        type - the type to get returned
        Returns:
        the proper Opcode RETURN, ARETURN, IRETURN, etc
      • getLoadConstantInsn

        public static org.apache.xbean.asm9.tree.AbstractInsnNode getLoadConstantInsn​(Object val)
      • getStoreInsn

        public static int getStoreInsn​(Class<?> type)
        Calclates the proper STORE instruction opcode for the given type
        Parameters:
        type - the type to get stored
        Returns:
        the proper Opcode ISTORE, ASTORE, LSTORE, etc
      • getLoadInsn

        public static int getLoadInsn​(Class<?> type)
        Calclates the proper LOAD instruction opcode for the given type. This is the appropriate bytecode instruction to load a value from a variable to the stack.
        Parameters:
        type - the type to get loaded
        Returns:
        the proper Opcode ILOAD, ALOAD, LLOAD, etc
      • getInternalNames

        public static String[] getInternalNames​(Class<?>[] classes)
        Get the internal names for the given classes
        See Also:
        Type.getInternalName(Class)
      • getParamTypes

        public static org.apache.xbean.asm9.Type[] getParamTypes​(Class<?>[] params)
        get the ASM Types for the given classes
        See Also:
        Type.getType(Method)
      • getParamIndex

        public static int getParamIndex​(org.apache.xbean.asm9.tree.MethodNode methodNode,
                                        int varPos)
        Determine the 0-based index of the parameter of LOAD or STORE position varPos
        Parameters:
        methodNode -
        varPos - the position on the stack
        Returns:
        the index of the parameter which corresponds to this varPos
      • isLoadInsn

        public static boolean isLoadInsn​(org.apache.xbean.asm9.tree.AbstractInsnNode insn)
        Returns:
        true if the instruction is an LOAD instruction
      • isThisInsn

        public static boolean isThisInsn​(org.apache.xbean.asm9.tree.AbstractInsnNode insn)
        Returns:
        true if the instruction is an ALOAD_0
      • getCorrespondingLoadInsn

        public static int getCorrespondingLoadInsn​(int storeInsnOpcode)
        Get the corresponding LOAD instruction for the given STORE instruction.
        Parameters:
        storeInsnOpcode - e.g. Opcodes.ISTORE
        Throws:
        IllegalArgumentException - if the given opcode is not a STORE instruction
      • getCorrespondingStoreInsn

        public static int getCorrespondingStoreInsn​(int loadInsnOpcode)
        Get the corresponding STORE instruction for the given LOAD instruction.
        Parameters:
        loadInsnOpcode - e.g. Opcodes.FLOAD
        Throws:
        IllegalArgumentException - if the given opcode is not a STORE instruction
      • isPrimitive

        public static boolean isPrimitive​(String typeDesc,
                                          boolean includeVoid)
        Parameters:
        typeDesc - the internal type descriptor from the bytecode. See ClassNode.name
        includeVoid - if the Void.class type also counts as primitive
      • getDescribedClass

        public static Class<?> getDescribedClass​(ClassLoader classLoader,
                                                 String typeDesc)
        Get the class from the described type
        Parameters:
        typeDesc -
        Returns:
        described class or null if it could not be loaded
      • throwException

        public static org.apache.xbean.asm9.tree.InsnList throwException​(Class type)
        Helper method to add the code necessary to throw the given exception type, sans message.
      • throwException

        public static org.apache.xbean.asm9.tree.InsnList throwException​(Class type,
                                                                         String msg)
        Helper method to add the code necessary to throw the given exception type, sans message.
      • getLocalVarPos

        public static int getLocalVarPos​(org.apache.xbean.asm9.tree.MethodNode meth)
        Calculate the next local variable position. For a non-static method the position 0 on the stack is the this pointer. After that there are all the method parameters. For a static method the method parameters begin at position zero. This method does calculate the first unused stack position which can be used for xLOAD/xSTORE opterations, e.g. int nextVarPos = AsmHelper.getLocalVarPos(myMethodNode); instructions.add(AsmHelper.getLoadConstantInsn(4711)); instructions.add(new VarInsnNode(Opcodes.ISTORE, nextVarPos);
        Returns:
        the 0-based position on the stack at which the local variables can be located.