Class AbstractClassFileWriter

java.lang.Object
io.micronaut.inject.writer.AbstractClassFileWriter
All Implemented Interfaces:
ClassOutputWriter, OriginatingElements, org.objectweb.asm.Opcodes
Direct Known Subclasses:
AbstractAnnotationMetadataWriter, AnnotationMetadataWriter, AopProxyWriter, BeanDefinitionWriter, DispatchWriter, ExecutableMethodsDefinitionWriter

@Internal public abstract class AbstractClassFileWriter extends Object implements org.objectweb.asm.Opcodes, OriginatingElements, ClassOutputWriter
Abstract class that writes generated classes to disk and provides convenience methods for building classes.
Since:
1.0
Author:
Graeme Rocher
  • Field Details

    • TYPE_ARGUMENT

      protected static final org.objectweb.asm.Type TYPE_ARGUMENT
    • TYPE_ARGUMENT_ARRAY

      protected static final org.objectweb.asm.Type TYPE_ARGUMENT_ARRAY
    • ZERO_ARGUMENTS_CONSTANT

      protected static final String ZERO_ARGUMENTS_CONSTANT
      See Also:
    • CONSTRUCTOR_NAME

      protected static final String CONSTRUCTOR_NAME
      See Also:
    • DESCRIPTOR_DEFAULT_CONSTRUCTOR

      protected static final String DESCRIPTOR_DEFAULT_CONSTRUCTOR
      See Also:
    • METHOD_DEFAULT_CONSTRUCTOR

      protected static final org.objectweb.asm.commons.Method METHOD_DEFAULT_CONSTRUCTOR
    • TYPE_OBJECT

      protected static final org.objectweb.asm.Type TYPE_OBJECT
    • TYPE_CLASS

      protected static final org.objectweb.asm.Type TYPE_CLASS
    • DEFAULT_MAX_STACK

      protected static final int DEFAULT_MAX_STACK
      See Also:
    • TYPE_GENERATED

      protected static final org.objectweb.asm.Type TYPE_GENERATED
    • ARRAY_PATTERN

      protected static final Pattern ARRAY_PATTERN
    • METHOD_CREATE_ARGUMENT_SIMPLE

      protected static final org.objectweb.asm.commons.Method METHOD_CREATE_ARGUMENT_SIMPLE
    • METHOD_GENERIC_PLACEHOLDER_SIMPLE

      protected static final org.objectweb.asm.commons.Method METHOD_GENERIC_PLACEHOLDER_SIMPLE
    • METHOD_CREATE_TYPE_VARIABLE_SIMPLE

      protected static final org.objectweb.asm.commons.Method METHOD_CREATE_TYPE_VARIABLE_SIMPLE
    • originatingElements

      protected final OriginatingElements originatingElements
  • Constructor Details

    • AbstractClassFileWriter

      protected AbstractClassFileWriter(Element... originatingElements)
      Parameters:
      originatingElements - The originating elements
    • AbstractClassFileWriter

      protected AbstractClassFileWriter(OriginatingElements originatingElements)
      Parameters:
      originatingElements - The originating elements
  • Method Details

    • getOriginatingElements

      @NonNull public Element[] getOriginatingElements()
      Specified by:
      getOriginatingElements in interface OriginatingElements
      Returns:
      The elements where the bean definition originated from as an array.
    • addOriginatingElement

      public void addOriginatingElement(@NonNull Element element)
      Description copied from interface: OriginatingElements
      Add another element that should be included in the originating elements.
      Specified by:
      addOriginatingElement in interface OriginatingElements
      Parameters:
      element - The element to add
    • pushTypeArgumentElements

      protected static void pushTypeArgumentElements(org.objectweb.asm.Type owningType, org.objectweb.asm.ClassWriter owningTypeWriter, org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String declaringElementName, Map<String,ClassElement> types, Map<String,Integer> defaults, Map<String,org.objectweb.asm.commons.GeneratorAdapter> loadTypeMethods)
      Pushes type arguments onto the stack.
      Parameters:
      owningType - The owning type
      owningTypeWriter - The declaring class writer
      generatorAdapter - The generator adapter
      declaringElementName - The declaring class element of the generics
      types - The type references
      defaults - The annotation defaults
      loadTypeMethods - The load type methods
    • buildArgument

      protected static void buildArgument(org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String argumentName, org.objectweb.asm.Type objectType)
      Builds an argument instance.
      Parameters:
      generatorAdapter - The generator adapter.
      argumentName - The argument name
      objectType - The object type
    • buildArgument

      protected static void buildArgument(org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String argumentName, ClassElement objectType)
      Builds an argument instance.
      Parameters:
      generatorAdapter - The generator adapter.
      argumentName - The argument name
      objectType - The object type
    • buildArgumentWithGenerics

      protected static void buildArgumentWithGenerics(org.objectweb.asm.Type owningType, org.objectweb.asm.ClassWriter owningClassWriter, org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String argumentName, org.objectweb.asm.Type typeReference, ClassElement classElement, Map<String,ClassElement> typeArguments, Set<String> visitedTypes, Map<String,Integer> defaults, Map<String,org.objectweb.asm.commons.GeneratorAdapter> loadTypeMethods)
      Builds generic type arguments recursively.
      Parameters:
      owningType - The owning type
      owningClassWriter - The declaring writer
      generatorAdapter - The generator adapter to use
      argumentName - The argument name
      typeReference - The type name
      classElement - The class element that declares the generics
      typeArguments - The nested type arguments
      visitedTypes - The visited types
      defaults - The annotation defaults
      loadTypeMethods - The load type methods
    • buildArgumentWithGenerics

      protected static void buildArgumentWithGenerics(org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, org.objectweb.asm.Type type, AnnotationMetadataReference annotationMetadata, ClassElement[] generics)
      Builds generic type arguments recursively.
      Parameters:
      generatorAdapter - The generator adapter to use
      type - The type that declares the generics
      annotationMetadata - The annotation metadata reference
      generics - The generics
      Since:
      3.0.0
    • pushBuildArgumentsForMethod

      protected static void pushBuildArgumentsForMethod(String declaringElementName, org.objectweb.asm.Type owningType, org.objectweb.asm.ClassWriter declaringClassWriter, org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, Collection<ParameterElement> argumentTypes, Map<String,Integer> defaults, Map<String,org.objectweb.asm.commons.GeneratorAdapter> loadTypeMethods)
      Parameters:
      declaringElementName - The declaring element name
      owningType - The owning type
      declaringClassWriter - The declaring class writer
      generatorAdapter - The GeneratorAdapter
      argumentTypes - The argument types
      defaults - The annotation defaults
      loadTypeMethods - The load type methods
    • pushReturnTypeArgument

      protected void pushReturnTypeArgument(org.objectweb.asm.Type owningType, org.objectweb.asm.ClassWriter classWriter, org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String declaringTypeName, ClassElement argument, Map<String,Integer> defaults, Map<String,org.objectweb.asm.commons.GeneratorAdapter> loadTypeMethods)
      Pushes an argument.
      Parameters:
      owningType - The owning type
      classWriter - The declaring class writer
      generatorAdapter - The generator adapter
      declaringTypeName - The declaring type name
      argument - The argument
      defaults - The annotation defaults
      loadTypeMethods - The load type methods
    • pushCreateArgument

      protected static void pushCreateArgument(String declaringTypeName, org.objectweb.asm.Type owningType, org.objectweb.asm.ClassWriter declaringClassWriter, org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, String argumentName, TypedElement typedElement, AnnotationMetadata annotationMetadata, Map<String,ClassElement> typeArguments, Map<String,Integer> defaults, Map<String,org.objectweb.asm.commons.GeneratorAdapter> loadTypeMethods)
      Pushes a new Argument creation.
      Parameters:
      declaringTypeName - The declaring type name
      owningType - The owning type
      declaringClassWriter - The declaring class writer
      generatorAdapter - The generator adapter
      argumentName - The argument name
      typedElement - The typed element
      annotationMetadata - The annotation metadata
      typeArguments - The type arguments
      defaults - The annotation defaults
      loadTypeMethods - The load type methods
    • writeTo

      public void writeTo(File targetDir) throws IOException
      Write the class to the target directory.
      Parameters:
      targetDir - The target directory
      Throws:
      IOException - if there is an error writing the file
    • writeBooleanMethod

      protected void writeBooleanMethod(org.objectweb.asm.ClassWriter classWriter, String methodName, Supplier<Boolean> valueSupplier)
      Writes a method that returns a boolean value with the value supplied by the given supplier.
      Parameters:
      classWriter - The class writer
      methodName - The method name
      valueSupplier - The supplier
    • getOriginatingElement

      @Nullable public Element getOriginatingElement()
      Returns:
      The originating element
    • implementInterceptedTypeMethod

      protected void implementInterceptedTypeMethod(org.objectweb.asm.Type interceptedType, org.objectweb.asm.ClassWriter classWriter)
      Implements a method called "getInterceptedType" for the given type and class writer.
      Parameters:
      interceptedType - The intercepted type
      classWriter - The class writer
    • getTypeDescriptor

      protected static String getTypeDescriptor(TypedElement type)
      Returns the descriptor corresponding to the given class.
      Parameters:
      type - The type
      Returns:
      The descriptor for the class
    • getTypeDescriptor

      protected static String getTypeDescriptor(Class<?> type)
      Returns the descriptor corresponding to the given class.
      Parameters:
      type - The type
      Returns:
      The descriptor for the class
    • getTypeDescriptor

      protected static String getTypeDescriptor(String type)
      Returns the descriptor corresponding to the given class.
      Parameters:
      type - The type
      Returns:
      The descriptor for the class
    • getTypeReferenceForName

      protected static org.objectweb.asm.Type getTypeReferenceForName(String className, String... genericTypes)
      Returns the Type reference corresponding to the given class.
      Parameters:
      className - The class name
      genericTypes - The generic types
      Returns:
      The Type
    • getTypeReference

      protected static org.objectweb.asm.Type getTypeReference(TypedElement type)
      Return the type reference for a class.
      Parameters:
      type - The type
      Returns:
      The Type
    • pushBoxPrimitiveIfNecessary

      protected static void pushBoxPrimitiveIfNecessary(org.objectweb.asm.Type fieldType, org.objectweb.asm.MethodVisitor injectMethodVisitor)
      Parameters:
      fieldType - The field type
      injectMethodVisitor - The MethodVisitor
    • pushBoxPrimitiveIfNecessary

      protected static void pushBoxPrimitiveIfNecessary(Class<?> fieldType, org.objectweb.asm.MethodVisitor injectMethodVisitor)
      Parameters:
      fieldType - The field type
      injectMethodVisitor - The MethodVisitor
    • pushBoxPrimitiveIfNecessary

      protected static void pushBoxPrimitiveIfNecessary(TypedElement fieldType, org.objectweb.asm.MethodVisitor injectMethodVisitor)
      Parameters:
      fieldType - The field type
      injectMethodVisitor - The MethodVisitor
    • pushCastToType

      protected static void pushCastToType(org.objectweb.asm.MethodVisitor methodVisitor, org.objectweb.asm.Type type)
      Parameters:
      methodVisitor - The MethodVisitor
      type - The type
    • pushCastToType

      protected static void pushCastToType(org.objectweb.asm.MethodVisitor methodVisitor, TypedElement type)
      Parameters:
      methodVisitor - The MethodVisitor
      type - The type
    • pushCastToType

      protected static void pushCastToType(org.objectweb.asm.MethodVisitor methodVisitor, Class<?> type)
      Parameters:
      methodVisitor - The MethodVisitor
      type - The type
    • pushReturnValue

      protected static void pushReturnValue(org.objectweb.asm.MethodVisitor methodVisitor, TypedElement type)
      Parameters:
      methodVisitor - The MethodVisitor
      type - The type
    • pushMethodNameAndTypesArguments

      protected static void pushMethodNameAndTypesArguments(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, String methodName, Collection<ClassElement> argumentTypes)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      methodName - The method name
      argumentTypes - The argument types
    • pushNewArray

      protected static void pushNewArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, Class<?> arrayType, int size)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      arrayType - The array class
      size - The size
    • pushNewArray

      protected static void pushNewArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, org.objectweb.asm.Type arrayType, int size)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      arrayType - The array class
      size - The size
    • pushStoreStringInArray

      protected static void pushStoreStringInArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, int index, int size, String string)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      index - The index
      size - The size
      string - The string
    • pushStoreInArray

      protected static void pushStoreInArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, int index, int size, Runnable runnable)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      index - The index
      size - The size
      runnable - The runnable
    • pushStoreInArray

      protected static void pushStoreInArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, org.objectweb.asm.Type type, int index, int size, Runnable runnable)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      type - The type of the array
      index - The index
      size - The size
      runnable - The runnable
    • pushStoreTypeInArray

      protected static void pushStoreTypeInArray(org.objectweb.asm.commons.GeneratorAdapter methodVisitor, int index, int size, ClassElement type)
      Parameters:
      methodVisitor - The method visitor as GeneratorAdapter
      index - The index
      size - The size
      type - The type
    • getTypes

      protected org.objectweb.asm.Type[] getTypes(Collection<ClassElement> types)
      Parameters:
      types - The types
      Returns:
      An array with the Type of the objects
    • getObjectType

      protected static org.objectweb.asm.Type getObjectType(Object type)
      Parameters:
      type - The type
      Returns:
      The Type for the object type
    • getTypeDescriptor

      protected static String getTypeDescriptor(String className, String... genericTypes)
      Parameters:
      className - The class name
      genericTypes - The generic types
      Returns:
      The type descriptor as String
    • getMethodDescriptor

      protected static String getMethodDescriptor(String returnType, String... argumentTypes)
      Parameters:
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The method descriptor
    • getMethodDescriptor

      protected static String getMethodDescriptor(TypedElement returnType, Collection<? extends TypedElement> argumentTypes)
      Parameters:
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The method descriptor
    • getMethodDescriptorForReturnType

      protected static String getMethodDescriptorForReturnType(org.objectweb.asm.Type returnType, Collection<? extends TypedElement> argumentTypes)
      Parameters:
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The method descriptor
    • getMethodDescriptor

      protected static String getMethodDescriptor(Class<?> returnType, Collection<Class<?>> argumentTypes)
      Parameters:
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The method descriptor
    • getMethodDescriptor

      protected static String getMethodDescriptor(org.objectweb.asm.Type returnType, Collection<org.objectweb.asm.Type> argumentTypes)
      Parameters:
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The method descriptor
    • getMethodSignature

      protected static String getMethodSignature(String returnTypeReference, String... argReferenceTypes)
      Parameters:
      returnTypeReference - The return type reference
      argReferenceTypes - The argument reference types
      Returns:
      The method signature
    • getConstructorDescriptor

      protected static String getConstructorDescriptor(Class<?>... argumentTypes)
      Parameters:
      argumentTypes - The argument types
      Returns:
      The constructor descriptor
    • getConstructorDescriptor

      protected static String getConstructorDescriptor(org.objectweb.asm.Type[] argumentTypes)
      Parameters:
      argumentTypes - The argument types
      Returns:
      The constructor descriptor
    • getConstructorDescriptor

      protected static String getConstructorDescriptor(Collection<ParameterElement> argList)
      Parameters:
      argList - The argument list
      Returns:
      The constructor descriptor
    • writeClassToDisk

      protected void writeClassToDisk(File targetDir, org.objectweb.asm.ClassWriter classWriter, String className) throws IOException
      Writes the class file to disk in the given directory.
      Parameters:
      targetDir - The target directory
      classWriter - The current class writer
      className - The class name
      Throws:
      IOException - if there is a problem writing the class to disk
    • writeClassToDisk

      protected void writeClassToDisk(OutputStream out, org.objectweb.asm.ClassWriter classWriter) throws IOException
      Parameters:
      out - The output stream
      classWriter - The current class writer
      Throws:
      IOException - if there is a problem writing the class to disk
    • startConstructor

      protected org.objectweb.asm.commons.GeneratorAdapter startConstructor(org.objectweb.asm.ClassVisitor classWriter)
      Parameters:
      classWriter - The current class writer
      Returns:
      The GeneratorAdapter for the constructor
    • startConstructor

      protected org.objectweb.asm.commons.GeneratorAdapter startConstructor(org.objectweb.asm.ClassVisitor classWriter, Class<?>... argumentTypes)
      Parameters:
      classWriter - The current class writer
      argumentTypes - The argument types
      Returns:
      The GeneratorAdapter for the constructor
    • startClass

      protected void startClass(org.objectweb.asm.ClassVisitor classWriter, String className, org.objectweb.asm.Type superType)
      Parameters:
      classWriter - The current class writer
      className - The class name
      superType - The super type
    • startPublicClass

      protected void startPublicClass(org.objectweb.asm.ClassVisitor classWriter, String className, org.objectweb.asm.Type superType)
      Parameters:
      classWriter - The current class writer
      className - The class name
      superType - The super type
    • startService

      protected void startService(org.objectweb.asm.ClassVisitor classWriter, Class<?> serviceType, String internalClassName, org.objectweb.asm.Type superType)
      Parameters:
      classWriter - The current class writer
      serviceType - The service type
      internalClassName - The class name
      superType - The super type
    • startService

      protected void startService(org.objectweb.asm.ClassVisitor classWriter, String serviceName, String internalClassName, org.objectweb.asm.Type superType, String... interfaces)
      Parameters:
      classWriter - The current class writer
      serviceName - The service name
      internalClassName - The class name
      superType - The super type
      interfaces - The interfaces
    • startFinalClass

      protected void startFinalClass(org.objectweb.asm.ClassVisitor classWriter, String className, org.objectweb.asm.Type superType)
      Parameters:
      classWriter - The current class writer
      className - The class name
      superType - The super type
    • startPublicFinalClass

      protected void startPublicFinalClass(org.objectweb.asm.ClassVisitor classWriter, String className, org.objectweb.asm.Type superType)
      Starts a public final class.
      Parameters:
      classWriter - The current class writer
      className - The class name
      superType - The super type
    • startClass

      protected void startClass(org.objectweb.asm.ClassWriter classWriter, String className, org.objectweb.asm.Type superType, String genericSignature)
      Parameters:
      classWriter - The current class writer
      className - The class name
      superType - The super type
      genericSignature - The generic signature
    • invokeConstructor

      protected void invokeConstructor(org.objectweb.asm.MethodVisitor cv, Class<?> superClass, Class<?>... argumentTypes)
      Parameters:
      cv - The constructor visitor
      superClass - The super class
      argumentTypes - The argument types
    • invokeInterfaceStaticMethod

      protected static void invokeInterfaceStaticMethod(org.objectweb.asm.MethodVisitor visitor, Class<?> targetType, org.objectweb.asm.commons.Method method)
      Parameters:
      visitor - The interface visitor
      targetType - The target type
      method - The method
    • startPublicMethodZeroArgs

      protected org.objectweb.asm.commons.GeneratorAdapter startPublicMethodZeroArgs(org.objectweb.asm.ClassWriter classWriter, Class<?> returnType, String methodName)
      Parameters:
      classWriter - The current class writer
      returnType - The return type
      methodName - The method name
      Returns:
      TheThe GeneratorAdapter for the method
    • startPublicFinalMethodZeroArgs

      protected org.objectweb.asm.commons.GeneratorAdapter startPublicFinalMethodZeroArgs(org.objectweb.asm.ClassWriter classWriter, Class<?> returnType, String methodName)
      Parameters:
      classWriter - The current class writer
      returnType - The return type
      methodName - The method name
      Returns:
      TheThe GeneratorAdapter for the method
    • getInternalName

      protected static String getInternalName(String className)
      Parameters:
      className - The class name
      Returns:
      The internal name
    • getInternalNameForCast

      protected static String getInternalNameForCast(TypedElement type)
      Parameters:
      type - The type
      Returns:
      the internal name for cast
    • getInternalNameForCast

      protected static String getInternalNameForCast(Class<?> typeClass)
      Parameters:
      typeClass - The type
      Returns:
      the internal name for cast
    • getInternalNameForCast

      protected static String getInternalNameForCast(org.objectweb.asm.Type type)
      Parameters:
      type - The type
      Returns:
      the internal name for cast
    • getClassFileName

      protected String getClassFileName(String className)
      Parameters:
      className - The class name
      Returns:
      The class file name
    • newClassWriterOutputVisitor

      protected ClassWriterOutputVisitor newClassWriterOutputVisitor(File compilationDir)
      Parameters:
      compilationDir - The compilation directory
      Returns:
      The directory class writer output visitor
    • returnVoid

      protected void returnVoid(org.objectweb.asm.commons.GeneratorAdapter overriddenMethodGenerator)
      Parameters:
      overriddenMethodGenerator - The overridden method generator
    • visitStaticInitializer

      protected org.objectweb.asm.commons.GeneratorAdapter visitStaticInitializer(org.objectweb.asm.ClassVisitor classWriter)
      Parameters:
      classWriter - The current class writer
      Returns:
      The GeneratorAdapter
    • startPublicMethod

      protected org.objectweb.asm.commons.GeneratorAdapter startPublicMethod(org.objectweb.asm.ClassWriter writer, String methodName, String returnType, String... argumentTypes)
      Parameters:
      writer - The class writer
      methodName - The method name
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The GeneratorAdapter
    • startPublicMethod

      protected org.objectweb.asm.commons.GeneratorAdapter startPublicMethod(org.objectweb.asm.ClassWriter writer, org.objectweb.asm.commons.Method asmMethod)
      Parameters:
      writer - The class writer
      asmMethod - The asm method
      Returns:
      The GeneratorAdapter
      Since:
      2.3.0
    • startProtectedMethod

      protected org.objectweb.asm.commons.GeneratorAdapter startProtectedMethod(org.objectweb.asm.ClassWriter writer, String methodName, String returnType, String... argumentTypes)
      Parameters:
      writer - The class writer
      methodName - The method name
      returnType - The return type
      argumentTypes - The argument types
      Returns:
      The GeneratorAdapter
    • generateServiceDescriptor

      protected void generateServiceDescriptor(String className, GeneratedFile generatedFile) throws IOException
      Generates a service discovery for the given class name and file.
      Parameters:
      className - The class name
      generatedFile - The generated file
      Throws:
      IOException - An exception if an error occurs
    • pushNewInstance

      protected void pushNewInstance(org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, org.objectweb.asm.Type typeToInstantiate)
      Push the instantiation of the given type.
      Parameters:
      generatorAdapter - The generator adaptor
      typeToInstantiate - The type to instantiate.
    • invokeMethod

      @NonNull protected ClassElement invokeMethod(@NonNull org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, @NonNull MethodElement method)
      Invokes the given method.
      Parameters:
      generatorAdapter - The generator adapter
      method - The method to invoke
      Returns:
      The return type
    • pushStringMapOf

      public static <T> void pushStringMapOf(org.objectweb.asm.commons.GeneratorAdapter generatorAdapter, Map<? extends CharSequence,T> annotationData, boolean skipEmpty, T empty, Consumer<T> pushValue)