Interface ClassMaker

All Superinterfaces:
Maker

public interface ClassMaker extends Maker
Allows new classes and interfaces to be defined dynamically.
See Also:
  • Method Details

    • begin

      static ClassMaker begin()
      Begin defining a class with an automatically assigned name.
    • begin

      static ClassMaker begin(String className)
      Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.
      Parameters:
      className - fully qualified class name; pass null to automatically assign a name
    • begin

      static ClassMaker begin(String className, ClassLoader parentLoader)
      Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.
      Parameters:
      className - fully qualified class name; pass null to automatically assign a name
      parentLoader - parent class loader; pass null to use default
    • begin

      static ClassMaker begin(String className, ClassLoader parentLoader, Object key)
      Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.
      Parameters:
      className - fully qualified class name; pass null to automatically assign a name
      parentLoader - parent class loader; pass null to use default
      key - an opaque key used for creating distinct class loaders; can be null
    • begin

      static ClassMaker begin(String className, MethodHandles.Lookup lookup)
      Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.
      Parameters:
      className - fully qualified class name; pass null to automatically assign a name
      lookup - finish loading the class using this lookup object
    • beginExplicit

      static ClassMaker beginExplicit(String className, ClassLoader parentLoader, Object key)
      Begin defining a class with an explicitly specified name.
      Parameters:
      className - fully qualified class name
      parentLoader - parent class loader; pass null to use default
      key - an opaque key used for creating distinct class loaders; can be null
    • beginExplicit

      static ClassMaker beginExplicit(String className, MethodHandles.Lookup lookup)
      Begin defining a class with an explicitly specified name.
      Parameters:
      className - fully qualified class name
      lookup - finish loading the class using this lookup object
    • beginExternal

      static ClassMaker beginExternal(String className)
      Begin defining a class intended to be loaded from a file. The class name exactly matches the one given, and setExact is unsupported. All classes defined from this maker will also be external.
      Parameters:
      className - fully qualified class name
      See Also:
    • another

      ClassMaker another(String className)
      Begin defining another class with the same loader and lookup as this one. The actual class name will have a suffix applied to ensure uniqueness, unless this maker creates explicit or external classes.

      The returned ClassMaker instance isn't attached to this maker, and so it can be acted upon by a different thread.

      Parameters:
      className - fully qualified class name; pass null to automatically assign a name (unless explicit or external)
      See Also:
    • type

      Type type()
      Returns the type of this class being made.
    • name

      String name()
      Returns the tentative name of the class being made. If finishHidden is called, then the actual class name will differ.
      Specified by:
      name in interface Maker
    • public_

      ClassMaker public_()
      Switch this class to be public. Classes are package-private by default.
      Specified by:
      public_ in interface Maker
      Returns:
      this
    • private_

      ClassMaker private_()
      Switch this class to be private. Classes are package-private by default.
      Specified by:
      private_ in interface Maker
      Returns:
      this
    • protected_

      ClassMaker protected_()
      Switch this class to be protected. Classes are package-private by default.
      Specified by:
      protected_ in interface Maker
      Returns:
      this
    • static_

      ClassMaker static_()
      Switch this class to be static. Classes are non-static by default.
      Specified by:
      static_ in interface Maker
      Returns:
      this
    • final_

      ClassMaker final_()
      Switch this class to be final. Classes are non-final by default.
      Specified by:
      final_ in interface Maker
      Returns:
      this
    • interface_

      ClassMaker interface_()
      Switch this class to be an interface. Classes are classes by default.
      Returns:
      this
    • abstract_

      ClassMaker abstract_()
      Switch this class to be abstract. Classes are non-abstract by default.
      Returns:
      this
    • synthetic

      ClassMaker synthetic()
      Indicate that this class is synthetic. Classes are non-synthetic by default.
      Specified by:
      synthetic in interface Maker
      Returns:
      this
    • enum_

      ClassMaker enum_()
      Indicate that this class is defining an enum. No checks or modifications are performed to ensure that the enum class is defined correctly.
      Returns:
      this
    • annotation

      ClassMaker annotation()
      Indicate that this class is defining an annotation interface.
      Returns:
      this
    • extend

      ClassMaker extend(Object superClass)
      Set a class that this class extends.
      Parameters:
      superClass - non-null type, specified by a Class or a String, etc.
      Returns:
      this
      Throws:
      IllegalArgumentException - if the type is unsupported
      IllegalStateException - if already assigned
    • implement

      ClassMaker implement(Object iface)
      Add an interface that this class or interface implements. Call this method multiple times to implement more interfaces.
      Parameters:
      iface - non-null type, specified by a Class or a String, etc.
      Returns:
      this
      Throws:
      IllegalArgumentException - if the type is unsupported
    • signature

      ClassMaker signature(Object... components)
      Define a signature for this member, which is a string for supporting generic types. The components can be strings or types (class, ClassMaker, etc.), which are concatenated into a single string. Consult the JVM specification for the signature syntax.
      Specified by:
      signature in interface Maker
      Returns:
      this
    • permitSubclass

      ClassMaker permitSubclass(Object subclass)
      Convert this class into a sealed class by permitting a subclass. Call this method multiple times to permit more subclasses.
      Parameters:
      subclass - non-null type, specified by a Class or a String, etc.
      Returns:
      this
      Throws:
      IllegalArgumentException - if the type is unsupported
    • addField

      FieldMaker addField(Object type, String name)
      Add a field to the class.
      Parameters:
      type - a class or name
      Throws:
      IllegalArgumentException - if the type is unsupported
      IllegalStateException - if field is already defined
    • addMethod

      MethodMaker addMethod(Object retType, String name, Object... paramTypes)
      Add a method to this class.
      Parameters:
      retType - a class or name; can be null if method returns void
      paramTypes - classes or names; can be null if method accepts no parameters
      Throws:
      IllegalArgumentException - if a type is unsupported
    • addMethod

      default MethodMaker addMethod(String name, MethodType type)
      Add a method to this class.
      Parameters:
      type - defines the return type and parameter types
    • addConstructor

      MethodMaker addConstructor(Object... paramTypes)
      Add a constructor to this class.
      Parameters:
      paramTypes - classes or names; can be null if constructor accepts no parameters
      Throws:
      IllegalArgumentException - if a type is unsupported
    • addConstructor

      default MethodMaker addConstructor(MethodType type)
      Add a constructor to this class.
      Parameters:
      type - defines the parameter types
      Throws:
      IllegalArgumentException - if the return type isn't void
    • addClinit

      MethodMaker addClinit()
      Add code to the static initializer of this class. Multiple initializers can be added, and they're all stitched together when the class definition is finished. Returning from one initializer only breaks out of the local scope.
    • asRecord

      MethodMaker asRecord()
      Convert this class to a record, and return a newly added constructor for it. Each field which is currently defined in this class is treated as a record component, and each is also represented by a constructor parameter. The constructor shouldn't set the fields directly, since this is performed automatically at the end.

      Unless already defined, the equals, hashCode, and toString methods are automatically added. The same rule applies for the component accessor methods.

    • addInnerClass

      ClassMaker addInnerClass(String className)
      Add an inner class to this class. The actual class name will have a suitable suffix applied to ensure uniqueness.

      The returned ClassMaker instance isn't attached to this maker, and so it can be acted upon by a different thread.

      Parameters:
      className - simple class name; pass null to use default
      Throws:
      IllegalArgumentException - if not given a simple class name
      See Also:
    • sourceFile

      ClassMaker sourceFile(String fileName)
      Set the source file of this class file by adding a source file attribute.
      Returns:
      this
    • arrayType

      Type arrayType(int dimensions)
      Returns a type object which represents the class being made as an array.
      Parameters:
      dimensions - must be at least 1
      Throws:
      IllegalArgumentException - if the given dimensions is less than 1 or greater than 255
    • classLoader

      ClassLoader classLoader()
      Returns the class loader that the finished class will be loaded into.
    • installClass

      boolean installClass(Class<?> clazz)
      Directly install a dependent class into the class loader used by the finished class. This is necessary when the given class cannot be found by the normal hierarchical class loading technique.

      Note: When installed, the given class is held by a weak reference, to facilitate unloading. If the installed class can be unloaded before finishing the new class, a reachability fence might be required.

      Returns:
      true if installed, or else false if the class was already installed
      Throws:
      IllegalStateException - if this maker was begun with a lookup object, or if the class loader already refers to a different class with the same name
    • unimplementedMethods

      Set<String> unimplementedMethods()
      Returns the set of methods which would need to be implemented by this class in order for it to be non-abstract. Each method in the set is represented by a simple signature.
      Returns:
      a non-null set, possibly empty
    • finish

      Class<?> finish()
      Finishes the definition of the new class.
      Throws:
      IllegalStateException - if already finished or if the definition is broken
    • finishLookup

      MethodHandles.Lookup finishLookup()
      Finishes the definition of the new class, returning a lookup which has full privilege access to the class. Calling this method has the side effect of forcing the new class to be initialized.
      Returns:
      the lookup for the class; call lookupClass to obtain the actual class
      Throws:
      IllegalStateException - if already finished or if the definition is broken
    • finishHidden

      MethodHandles.Lookup finishHidden()
      Finishes the definition of a new hidden class, returning a lookup which has full privilege access to the class. Hidden classes are automatically unloaded when no longer referenced, even if the class loader still is. If a lookup object was passed to the begin method, the hidden class is defined in the same nest as the lookup class.
      Returns:
      the lookup for the class; call lookupClass to obtain the actual class
      Throws:
      IllegalStateException - if already finished or if the definition is broken
    • finishBytes

      byte[] finishBytes()
      Finishes the definition of the new class into a byte array.
      Throws:
      IllegalStateException - if already finished or if the definition is broken
    • finishTo

      void finishTo(OutputStream out) throws IOException
      Finishes the definition of the new class and writes it to a stream.
      Throws:
      IllegalStateException - if already finished or if the definition is broken
      IOException