Class AsmUtil


  • public class AsmUtil
    extends Object
    A collection of ASM and Jandex utilities. NOTE: this has a copy in AsmUtilCopy in arc-processor with some extra methods for knowing if we need a signature and getting the signature of a class.
    • Constructor Summary

      Constructors 
      Constructor Description
      AsmUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.objectweb.asm.Type autobox​(org.objectweb.asm.Type primitive)  
      static void boxIfRequired​(org.objectweb.asm.MethodVisitor mv, org.jboss.jandex.Type jandexType)
      Calls the right boxing method for the given Jandex Type if it is a primitive.
      static void copyParameterNames​(org.objectweb.asm.MethodVisitor mv, org.jboss.jandex.MethodInfo method)
      Copy the parameter names to the given MethodVisitor, unless we don't have parameter name info
      static String getDescriptor​(org.jboss.jandex.MethodInfo method, Function<String,​String> typeArgMapper)
      Returns the Java bytecode descriptor of a given Jandex MethodInfo using the given type argument mappings.
      static String getDescriptor​(org.jboss.jandex.Type type, Function<String,​String> typeArgMapper)
      Returns the Java bytecode descriptor of a given Jandex Type using the given type argument mappings.
      static int getLoadOpcode​(org.jboss.jandex.Type jandexType)
      Returns the bytecode instruction to load the given Jandex Type.
      static int getParameterSize​(org.jboss.jandex.Type paramType)
      Returns the number of underlying bytecode parameters taken by the given Jandex parameter Type.
      static org.jboss.jandex.Type[] getParameterTypes​(String methodDescriptor)
      Returns the Jandex Types of the parameters of the given method descriptor.
      static int getReturnInstruction​(String typeDescriptor)
      Returns a return bytecode instruction suitable for the given return type descriptor.
      static int getReturnInstruction​(org.jboss.jandex.Type jandexType)
      Returns a return bytecode instruction suitable for the given return Jandex Type.
      static String getSignature​(org.jboss.jandex.MethodInfo method, Function<String,​String> typeArgMapper)
      Returns the Java bytecode signature of a given Jandex MethodInfo using the given type argument mappings.
      static String getSignature​(org.jboss.jandex.Type type, Function<String,​String> typeArgMapper)
      Returns the Java bytecode signature of a given Jandex Type using the given type argument mappings.
      static String getSignatureIfRequired​(org.jboss.jandex.MethodInfo method)
      Returns the Java bytecode signature of a given Jandex MethodInfo.
      static String getSignatureIfRequired​(org.jboss.jandex.MethodInfo method, Function<String,​String> typeArgMapper)
      Returns the Java bytecode signature of a given Jandex MethodInfo using the given type argument mappings.
      static void printValueOnStderr​(org.objectweb.asm.MethodVisitor mv, Runnable valuePusher)
      Prints the value pushed on the stack (must be an Object) by the given valuePusher to STDERR.
      static void unboxIfRequired​(org.objectweb.asm.MethodVisitor mv, org.jboss.jandex.Type jandexType)
      Calls the right unboxing method for the given Jandex Type if it is a primitive.
      static void unboxIfRequired​(org.objectweb.asm.MethodVisitor mv, org.objectweb.asm.Type type)
      Calls the right unboxing method for the given Jandex Type if it is a primitive.
      static void visitLdc​(org.objectweb.asm.MethodVisitor mv, org.jboss.jandex.Type jandexType)
      Invokes the proper LDC Class Constant instructions for the given Jandex Type.
    • Field Detail

      • PRIMITIVES

        public static final List<org.objectweb.asm.Type> PRIMITIVES
      • WRAPPERS

        public static final List<org.objectweb.asm.Type> WRAPPERS
      • WRAPPER_TO_PRIMITIVE

        public static final Map<org.objectweb.asm.Type,​org.objectweb.asm.Type> WRAPPER_TO_PRIMITIVE
      • PRIMITIVE_DESCRIPTOR_TO_PRIMITIVE_CLASS_LITERAL

        public static final Map<Character,​String> PRIMITIVE_DESCRIPTOR_TO_PRIMITIVE_CLASS_LITERAL
    • Constructor Detail

      • AsmUtil

        public AsmUtil()
    • Method Detail

      • autobox

        public static org.objectweb.asm.Type autobox​(org.objectweb.asm.Type primitive)
      • getSignatureIfRequired

        public static String getSignatureIfRequired​(org.jboss.jandex.MethodInfo method)
        Returns the Java bytecode signature of a given Jandex MethodInfo. If the Java compiler doesn't have to emit a signature for the method, null is returned instead.
        Parameters:
        method - the method you want the signature for
        Returns:
        a bytecode signature for that method, or null if signature is not required
      • getSignatureIfRequired

        public static String getSignatureIfRequired​(org.jboss.jandex.MethodInfo method,
                                                    Function<String,​String> typeArgMapper)
        Returns the Java bytecode signature of a given Jandex MethodInfo using the given type argument mappings. If the Java compiler doesn't have to emit a signature for the method, null is returned instead.
        Parameters:
        method - the method you want the signature for
        typeArgMapper - a mapping between type argument names and their bytecode signatures
        Returns:
        a bytecode signature for that method, or null if signature is not required
      • getSignature

        public static String getSignature​(org.jboss.jandex.MethodInfo method,
                                          Function<String,​String> typeArgMapper)
        Returns the Java bytecode signature of a given Jandex MethodInfo using the given type argument mappings. For example, given this method:
         
         public class Foo<T> {
          public <R> List<R> method(int a, T t){...}
         }
         
         
        This will return <R:Ljava/lang/Object;>(ILjava/lang/Integer;)Ljava/util/List<TR;>; if your typeArgMapper contains T=Ljava/lang/Integer;.
        Parameters:
        method - the method you want the signature for.
        typeArgMapper - a mapping between type argument names and their bytecode signature.
        Returns:
        a bytecode signature for that method.
      • getDescriptor

        public static String getDescriptor​(org.jboss.jandex.MethodInfo method,
                                           Function<String,​String> typeArgMapper)
        Returns the Java bytecode descriptor of a given Jandex MethodInfo using the given type argument mappings. For example, given this method:
         
         public class Foo<T> {
          public <R> List<R> method(int a, T t){...}
         }
         
         
        This will return (ILjava/lang/Integer;)Ljava/util/List; if your typeArgMapper contains T=Ljava/lang/Integer;.
        Parameters:
        method - the method you want the descriptor for.
        typeArgMapper - a mapping between type argument names and their bytecode descriptor.
        Returns:
        a bytecode descriptor for that method.
      • getDescriptor

        public static String getDescriptor​(org.jboss.jandex.Type type,
                                           Function<String,​String> typeArgMapper)
        Returns the Java bytecode descriptor of a given Jandex Type using the given type argument mappings. For example, given this type: List<T>, this will return Ljava/util/List; if your typeArgMapper contains T=Ljava/lang/Integer;.
        Parameters:
        type - the type you want the descriptor for.
        typeArgMapper - a mapping between type argument names and their bytecode descriptor.
        Returns:
        a bytecode descriptor for that type.
      • getSignature

        public static String getSignature​(org.jboss.jandex.Type type,
                                          Function<String,​String> typeArgMapper)
        Returns the Java bytecode signature of a given Jandex Type using the given type argument mappings. For example, given this type: List<T>, this will return Ljava/util/List<Ljava/lang/Integer;>; if your typeArgMapper contains T=Ljava/lang/Integer;.
        Parameters:
        type - the type you want the signature for.
        typeArgMapper - a mapping between type argument names and their bytecode descriptor.
        Returns:
        a bytecode signature for that type.
      • getReturnInstruction

        public static int getReturnInstruction​(String typeDescriptor)
        Returns a return bytecode instruction suitable for the given return type descriptor. This will return specialised return instructions IRETURN, LRETURN, FRETURN, DRETURN, RETURN for primitives/void, and ARETURN otherwise;
        Parameters:
        typeDescriptor - the return type descriptor.
        Returns:
        the correct bytecode return instruction for that return type descriptor.
      • getReturnInstruction

        public static int getReturnInstruction​(org.jboss.jandex.Type jandexType)
        Returns a return bytecode instruction suitable for the given return Jandex Type. This will return specialised return instructions IRETURN, LRETURN, FRETURN, DRETURN, RETURN for primitives/void, and ARETURN otherwise;
        Parameters:
        jandexType - the return Jandex Type.
        Returns:
        the correct bytecode return instruction for that return type descriptor.
      • visitLdc

        public static void visitLdc​(org.objectweb.asm.MethodVisitor mv,
                                    org.jboss.jandex.Type jandexType)
        Invokes the proper LDC Class Constant instructions for the given Jandex Type. This will properly create LDC instructions for array types, class/parameterized classes, and primitive types by loading their equivalent TYPE constants in their box types, as well as type variables (using the first bound or Object) and Void.
        Parameters:
        mv - The MethodVisitor on which to visit the LDC instructions
        jandexType - the Jandex Type whose Class Constant to load.
      • boxIfRequired

        public static void boxIfRequired​(org.objectweb.asm.MethodVisitor mv,
                                         org.jboss.jandex.Type jandexType)
        Calls the right boxing method for the given Jandex Type if it is a primitive.
        Parameters:
        mv - The MethodVisitor on which to visit the boxing instructions
        jandexType - The Jandex Type to box if it is a primitive.
      • getLoadOpcode

        public static int getLoadOpcode​(org.jboss.jandex.Type jandexType)
        Returns the bytecode instruction to load the given Jandex Type. This returns the specialised bytecodes ILOAD, DLOAD, FLOAD and LLOAD for primitives, or ALOAD otherwise.
        Parameters:
        jandexType - The Jandex Type whose load instruction to return.
        Returns:
        The bytecode instruction to load the given Jandex Type.
      • unboxIfRequired

        public static void unboxIfRequired​(org.objectweb.asm.MethodVisitor mv,
                                           org.jboss.jandex.Type jandexType)
        Calls the right unboxing method for the given Jandex Type if it is a primitive.
        Parameters:
        mv - The MethodVisitor on which to visit the unboxing instructions
        jandexType - The Jandex Type to unbox if it is a primitive.
      • unboxIfRequired

        public static void unboxIfRequired​(org.objectweb.asm.MethodVisitor mv,
                                           org.objectweb.asm.Type type)
        Calls the right unboxing method for the given Jandex Type if it is a primitive.
        Parameters:
        mv - The MethodVisitor on which to visit the unboxing instructions
        type - The Jandex Type to unbox if it is a primitive.
      • getParameterTypes

        public static org.jboss.jandex.Type[] getParameterTypes​(String methodDescriptor)
        Returns the Jandex Types of the parameters of the given method descriptor.
        Parameters:
        methodDescriptor - a method descriptor
        Returns:
        the list of Jandex Type objects representing the parameters of the given method descriptor.
      • getParameterSize

        public static int getParameterSize​(org.jboss.jandex.Type paramType)
        Returns the number of underlying bytecode parameters taken by the given Jandex parameter Type. This will be 2 for doubles and longs, 1 otherwise.
        Parameters:
        paramType - the Jandex parameter Type
        Returns:
        the number of underlying bytecode parameters required.
      • printValueOnStderr

        public static void printValueOnStderr​(org.objectweb.asm.MethodVisitor mv,
                                              Runnable valuePusher)
        Prints the value pushed on the stack (must be an Object) by the given valuePusher to STDERR.
        Parameters:
        mv - The MethodVisitor to forward printing to.
        valuePusher - The function to invoke to push an Object to print on the stack.
      • copyParameterNames

        public static void copyParameterNames​(org.objectweb.asm.MethodVisitor mv,
                                              org.jboss.jandex.MethodInfo method)
        Copy the parameter names to the given MethodVisitor, unless we don't have parameter name info
        Parameters:
        mv - the visitor to copy to
        method - the method to copy from