java.lang.Object
ru.progrm_jarvis.javacommons.bytecode.asm.AsmUtil

@UsesBytecodeModification(ASM) public final class AsmUtil extends Object
Utility for common ObjectWeb ASM tasks.
  • Field Details

    • OBJECT_TYPE

      public static final org.objectweb.asm.Type OBJECT_TYPE
      ASM type of Object
    • OBJECT_ARRAY_TYPE

      public static final org.objectweb.asm.Type OBJECT_ARRAY_TYPE
      ASM type of Object array
    • STRING_TYPE

      public static final org.objectweb.asm.Type STRING_TYPE
      ASM type of String
    • METHOD_HANDLES_TYPE

      public static final org.objectweb.asm.Type METHOD_HANDLES_TYPE
      ASM type of MethodHandles
    • LOOKUP_TYPE

      public static final org.objectweb.asm.Type LOOKUP_TYPE
    • CALL_SITE_TYPE

      public static final org.objectweb.asm.Type CALL_SITE_TYPE
      ASM type of CallSite
    • METHOD_TYPE_TYPE

      public static final org.objectweb.asm.Type METHOD_TYPE_TYPE
      ASM type of MethodType
    • STATIC_INITIALIZER_METHOD_NAME

      public static final String STATIC_INITIALIZER_METHOD_NAME
      Name of static initializer method
      See Also:
    • CONSTRUCTOR_METHOD_NAME

      public static final String CONSTRUCTOR_METHOD_NAME
      Name of constructor method
      See Also:
    • LOOKUP_INNER_CLASS_NAME

      public static final String LOOKUP_INNER_CLASS_NAME
      Name of inner lookup class.
      See Also:
    • HASH_CODE_METHOD_NAME

      public static final String HASH_CODE_METHOD_NAME
      Name of Object.hashCode() method
      See Also:
    • EQUALS_METHOD_NAME

      public static final String EQUALS_METHOD_NAME
      Name of Object.equals(Object) method
      See Also:
    • TO_STRING_METHOD_NAME

      public static final String TO_STRING_METHOD_NAME
      Name of Object.toString() method
      See Also:
    • OBJECT_INTERNAL_NAME

      public static final String OBJECT_INTERNAL_NAME
      Internal name of Object
    • OBJECT_ARRAY_INTERNAL_NAME

      public static final String OBJECT_ARRAY_INTERNAL_NAME
      Internal name of Object array
    • STRING_INTERNAL_NAME

      public static final String STRING_INTERNAL_NAME
      Internal name of String
    • METHOD_HANDLES_INTERNAL_NAME

      public static final String METHOD_HANDLES_INTERNAL_NAME
      Internal name of MethodHandles
    • LOOKUP_INTERNAL_NAME

      public static final String LOOKUP_INTERNAL_NAME
      Internal name of MethodHandles.Lookup
    • CALL_SITE_INTERNAL_NAME

      public static final String CALL_SITE_INTERNAL_NAME
      Internal name of CallSite
    • METHOD_TYPE_INTERNAL_NAME

      public static final String METHOD_TYPE_INTERNAL_NAME
      Internal name of MethodType
    • OBJECT_DESCRIPTOR

      public static final String OBJECT_DESCRIPTOR
      Descriptor of Object
    • OBJECT_ARRAY_DESCRIPTOR

      public static final String OBJECT_ARRAY_DESCRIPTOR
      Descriptor of Object array
    • STRING_DESCRIPTOR

      public static final String STRING_DESCRIPTOR
      Descriptor of String
    • METHOD_HANDLES_DESCRIPTOR

      public static final String METHOD_HANDLES_DESCRIPTOR
      Descriptor of MethodHandles
    • LOOKUP_DESCRIPTOR

      public static final String LOOKUP_DESCRIPTOR
      Descriptor of MethodHandles.Lookup
    • CALL_SITE_DESCRIPTOR

      public static final String CALL_SITE_DESCRIPTOR
      Descriptor of CallSite
    • METHOD_TYPE_DESCRIPTOR

      public static final String METHOD_TYPE_DESCRIPTOR
      Descriptor of MethodType
    • VOID_METHOD_DESCRIPTOR

      public static final String VOID_METHOD_DESCRIPTOR
      Signature of void() method
    • BOOLEAN_METHOD_DESCRIPTOR

      public static final String BOOLEAN_METHOD_DESCRIPTOR
      Signature of boolean() method
    • BYTE_METHOD_DESCRIPTOR

      public static final String BYTE_METHOD_DESCRIPTOR
      Signature of byte() method
    • SHORT_METHOD_DESCRIPTOR

      public static final String SHORT_METHOD_DESCRIPTOR
      Signature of short() method
    • INT_METHOD_DESCRIPTOR

      public static final String INT_METHOD_DESCRIPTOR
      Signature of int() method
    • LONG_METHOD_DESCRIPTOR

      public static final String LONG_METHOD_DESCRIPTOR
      Signature of long() method
    • FLOAT_METHOD_DESCRIPTOR

      public static final String FLOAT_METHOD_DESCRIPTOR
      Signature of float() method
    • DOUBLE_METHOD_DESCRIPTOR

      public static final String DOUBLE_METHOD_DESCRIPTOR
      Signature of double() method
    • CHAR_METHOD_DESCRIPTOR

      public static final String CHAR_METHOD_DESCRIPTOR
      Signature of char() method
    • OBJECT_METHOD_DESCRIPTOR

      public static final String OBJECT_METHOD_DESCRIPTOR
      Signature of Object() method
    • STRING_METHOD_DESCRIPTOR

      public static final String STRING_METHOD_DESCRIPTOR
      Signature of String() method
    • VOID_BOOLEAN_METHOD_DESCRIPTOR

      public static final String VOID_BOOLEAN_METHOD_DESCRIPTOR
      Signature of void(boolean) method
    • VOID_BYTE_METHOD_DESCRIPTOR

      public static final String VOID_BYTE_METHOD_DESCRIPTOR
      Signature of void(byte) method
    • VOID_SHORT_METHOD_DESCRIPTOR

      public static final String VOID_SHORT_METHOD_DESCRIPTOR
      Signature of void(short) method
    • VOID_INT_METHOD_DESCRIPTOR

      public static final String VOID_INT_METHOD_DESCRIPTOR
      Signature of void(int) method
    • VOID_LONG_METHOD_DESCRIPTOR

      public static final String VOID_LONG_METHOD_DESCRIPTOR
      Signature of void(long) method
    • VOID_FLOAT_METHOD_DESCRIPTOR

      public static final String VOID_FLOAT_METHOD_DESCRIPTOR
      Signature of void(float) method
    • VOID_DOUBLE_METHOD_DESCRIPTOR

      public static final String VOID_DOUBLE_METHOD_DESCRIPTOR
      Signature of void(double) method
    • VOID_CHAR_METHOD_DESCRIPTOR

      public static final String VOID_CHAR_METHOD_DESCRIPTOR
      Signature of void(char) method
    • VOID_STRING_METHOD_DESCRIPTOR

      public static final String VOID_STRING_METHOD_DESCRIPTOR
      Signature of void(String) method
    • OPCODES_ACC_PUBLIC_FINAL

      public static final int OPCODES_ACC_PUBLIC_FINAL
      Result of Opcodes.ACC_PUBLIC and Opcodes.ACC_FINAL flags disjunction
      See Also:
    • OPCODES_ACC_PUBLIC_STATIC_FINAL

      public static final int OPCODES_ACC_PUBLIC_STATIC_FINAL
      Result of Opcodes.ACC_PUBLIC, Opcodes.ACC_STATIC and Opcodes.ACC_FINAL flags disjunction
      See Also:
    • OPCODES_ACC_PUBLIC_FINAL_SUPER

      public static final int OPCODES_ACC_PUBLIC_FINAL_SUPER
      Result of Opcodes.ACC_PUBLIC, Opcodes.ACC_FINAL and Opcodes.ACC_SUPER flags disjunction
      See Also:
  • Method Details

    • classNameToInternalName

      @NotNull public static @NotNull String classNameToInternalName(@NonNull @NonNull String className)
      Gets the internal name (aka VM-name) for the given ClassLoader-unique name.
      Parameters:
      className - name of the class
      Returns:
      internal name corresponding to the given class name
      See Also:
    • internalNameOf

      @NotNull public static @NotNull String internalNameOf(@NonNull @NonNull Class<?> type)
      Gets the internal name (aka VM-name) of the given class.
      Parameters:
      type - class whose internal name should be resolved
      Returns:
      internal name of the given class
    • visitStaticInitializer

      public static org.objectweb.asm.MethodVisitor visitStaticInitializer(@NonNull @NonNull org.objectweb.asm.ClassVisitor clazz)
      Visits the class's static initializer method.
      Parameters:
      clazz - visitor of the class
      Returns:
      static initializer block of the class
    • addLookup

      public static void addLookup(@NonNull @NonNull org.objectweb.asm.ClassVisitor clazz)
      Adds the inner lookup class to the class needed by the JVM via its visitor.
      Parameters:
      clazz - visitor of the class
    • addEmptyConstructor

      public static void addEmptyConstructor(@NonNull @NonNull org.objectweb.asm.ClassVisitor classVisitor, @NonNull @NonNull String superClassInternalName)
      Adds an empty constructor to the class via its visitor.
      Parameters:
      classVisitor - visitor of the class modified
      superClassInternalName - internal name of the super-class whose constructor should be invoked
    • addEmptyConstructor

      public static void addEmptyConstructor(@NonNull @NonNull org.objectweb.asm.ClassVisitor classVisitor)
      Adds an empty constructor to the class via its visitor.
      Parameters:
      classVisitor - visitor of the class modified
      See Also:
    • returnOpcode

      public static int returnOpcode(Class<?> returnType)
      Gets the return-opcode corresponding to the given type.
      Parameters:
      returnType - type of the value for which to get the return-opcode
      Returns:
      return-opcode corresponding to the given type
    • nonVoidReturnOpcode

      public static int nonVoidReturnOpcode(Class<?> returnType)
      Gets the return-opcode corresponding to the given non-void type.
      Parameters:
      returnType - type of the value for which to get the return-opcode
      Returns:
      return-opcode corresponding to the given type
      Throws:
      IllegalArgumentException - if returnType is of void type
    • loadOpcode

      public static int loadOpcode(Class<?> loadedType)
      Gets the load-opcode corresponding to the given type.
      Parameters:
      loadedType - type of the value for which to get the load-opcode
      Returns:
      load-opcode corresponding to the given type
    • arrayLoadOpcode

      public static int arrayLoadOpcode(Class<?> loadedComponentType)
      Gets the array-load-opcode corresponding to the array of given component type.
      Parameters:
      loadedComponentType - component type of the array for which to get the array-load-opcode
      Returns:
      array-load-opcode corresponding to the given array component type
    • storeOpcode

      public static int storeOpcode(Class<?> storedType)
      Gets the store-opcode corresponding to the given type
      Parameters:
      storedType - type of the value for which to get the store-opcode
      Returns:
      store-opcode corresponding to the given type
    • arrayStoreOpcode

      public static int arrayStoreOpcode(Class<?> storedComponentType)
      Gets the array-store-opcode corresponding to the array of given component type.
      Parameters:
      storedComponentType - component type of the array for which to get the array-store-opcode
      Returns:
      array-store-opcode corresponding to the given array component type
    • pushInt

      public static void pushInt(@NonNull @NonNull org.objectweb.asm.MethodVisitor method, int value)
      Adds code to the method which pushes the given int value onto the stack.
      Parameters:
      method - method visitor used for appending code to the method
      value - value to get pushed onto the stack
    • pushLong

      public static void pushLong(@NonNull @NonNull org.objectweb.asm.MethodVisitor method, long value)
      Adds code to the method which pushes the given long value onto the stack.
      Parameters:
      method - method visitor used for appending code to the method
      value - value to get pushed onto the stack
    • pushFloat

      public static void pushFloat(@NonNull @NonNull org.objectweb.asm.MethodVisitor method, float value)
      Adds code to the method which pushes the given float value onto the stack.
      Parameters:
      method - method visitor used for appending code to the method
      value - value to get pushed onto the stack
    • pushDouble

      public static void pushDouble(@NonNull @NonNull org.objectweb.asm.MethodVisitor method, double value)
      Adds code to the method which pushes the given double value onto the stack.
      Parameters:
      method - method visitor used for appending code to the method
      value - value to get pushed onto the stack
    • pushCharUnsafely

      public static void pushCharUnsafely(@NonNull @NonNull org.objectweb.asm.MethodVisitor method, char value)
      Adds code to the method which unsafely pushes the given char value onto the stack. The unsafety of this operation is in the fact that for some chars ([(char) 128; (char) 255] and [(char) 32768; (char) 65535) negative ints will be pushed onto the stack thus resulting incorrect values when casting those to signed numeric types. This yet might be a micro-optimization in some cases when there is a guarantee that the pushed value will only be treated as char (such as passing it to StringBuilder.append(char).
      Parameters:
      method - method visitor used for appending code to the method
      value - value to get pushed onto the stack
      See Also: