Class TestClassMaker

java.lang.Object
io.quarkus.gizmo2.testing.TestClassMaker

public final class TestClassMaker extends Object
A utility for testing generated classes which may be used in a unit test. The functions of this class are not thread-safe and are not usable for concurrency testing. By default, defined classes have assertions enabled.
  • Method Details

    • create

      public static TestClassMaker create(Gizmo gizmo)
      Construct a new instance. The given gizmo is used, except for its class output configuration which is ignored.
      Parameters:
      gizmo - the configured ase Gizmo instance to use (must not be null)
      Returns:
      a new test class maker instance (not null)
    • create

      public static TestClassMaker create()
      Construct a new instance.
      Returns:
      a new test class maker instance (not null)
    • gizmo

      public Gizmo gizmo()
      Returns the configured Gizmo instance (not null). The given instance can be used to create new classes and interfaces.
      Returns:
      the configured Gizmo instance (not null)
    • classLoader

      public ClassLoader classLoader()
      Returns the class loader associated with this instance. All test classes are defined to this loader.
      Returns:
      the class loader associated with this instance
    • registerResource

      public void registerResource(String path, List<String> lines)
      Register a new resource with this loader. The lines of text are encoded using the UTF-8 character encoding. Each given line of text is terminated with a newline character (NL aka 0x0D (12)).
      Parameters:
      path - the resource path (must not be null)
      lines - the lines of text for the resource (must not be null)
    • registerResource

      public void registerResource(String path, String data)
      Register a new resource with this loader. The text is encoded using the UTF-8 character encoding.
      Parameters:
      path - the resource path (must not be null)
      data - the text data of the resource (must not be null)
    • registerResource

      public void registerResource(String path, byte[] data)
      Register a new resource with this loader.
      Parameters:
      path - the resource path (must not be null)
      data - the binary data of the resource (must not be null)
    • loadClass

      public Class<?> loadClass(String className)
      Load a class from this loader with the given name.
      Parameters:
      className - the name of the class to load (must not be null)
      Returns:
      the loaded class (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader
    • loadClass

      public Class<?> loadClass(ClassDesc classDesc)
      Load a class from this loader with the given descriptor.
      Parameters:
      classDesc - the descriptor of the class to load (must not be null)
      Returns:
      the loaded class (not null)
      Throws:
      NoSuchElementException - if there is no class with that descriptor defined to this class loader
    • loadClass

      public <T> Class<? extends T> loadClass(String className, Class<T> expectedSupertype)
      Load a class from this loader with the given name.
      Parameters:
      className - the name of the class to load (must not be null)
      expectedSupertype - the expected supertype of the class (must not be null)
      Returns:
      the loaded class (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader
      ClassCastException - if the loaded class is not of the given type
    • loadClass

      public <T> Class<? extends T> loadClass(ClassDesc classDesc, Class<T> expectedSupertype)
      Load a class from this loader with the given descriptor.
      Parameters:
      classDesc - the descriptor of the class to load (must not be null)
      expectedSupertype - the expected supertype of the class (must not be null)
      Returns:
      the loaded class (not null)
      Throws:
      NoSuchElementException - if there is no class with that descriptor defined to this class loader
      ClassCastException - if the loaded class is not of the given type
    • readClass

      public <T> T readClass(String className, Function<byte[],T> func)
      Read the bytes of a class and apply a transformation to them.
      Type Parameters:
      T - the result type
      Parameters:
      className - the name of the class to read (must not be null)
      func - the function to apply (must not be null)
      Returns:
      the interpreted class
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader
    • readClass

      public <T> T readClass(ClassDesc classDesc, Function<byte[],T> func)
      Read the bytes of a class and apply a transformation to them.
      Type Parameters:
      T - the result type
      Parameters:
      classDesc - the descriptor of the class to read (must not be null)
      func - the function to apply (must not be null)
      Returns:
      the interpreted class
      Throws:
      NoSuchElementException - if there is no class with that descriptor defined to this class loader
    • initializeClass

      public void initializeClass(Class<?> clazz)
      Initialize a class defined by this test class maker.
      Parameters:
      clazz - the class to initialize (must not be null)
      Throws:
      IllegalArgumentException - if this test class maker does not own the given class
    • staticMethod

      public <T> T staticMethod(String className, String methodName, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • staticMethod

      public <T> T staticMethod(String className, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the static class (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • staticMethod

      public <T> T staticMethod(ClassDesc classDesc, String methodName, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • staticMethod

      public <T> T staticMethod(ClassDesc classDesc, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the static class (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • staticMethod

      public <T> T staticMethod(Class<?> clazz, String methodName, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • staticMethod

      public <T> T staticMethod(Class<?> clazz, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a static method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the static method (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(String className, String methodName, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(String className, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the static method (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(ClassDesc classDesc, String methodName, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(ClassDesc classDesc, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the virtual method (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(Class<?> clazz, String methodName, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      methodName - the method name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • virtualMethod

      public <T> T virtualMethod(Class<?> clazz, String methodName, MethodType methodType, Class<T> proxyType)
      Look up a virtual method on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      methodName - the method name (must not be null)
      methodType - the type of the virtual method (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target method (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no method which matches the expected name and type
    • constructor

      public <T> T constructor(String className, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • constructor

      public <T> T constructor(String className, MethodType methodType, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      className - the owner class binary name (must not be null)
      methodType - the type of the constructor (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • constructor

      public <T> T constructor(ClassDesc classDesc, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • constructor

      public <T> T constructor(ClassDesc classDesc, MethodType methodType, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      methodType - the type of the constructor (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • constructor

      public <T> T constructor(Class<?> clazz, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • constructor

      public <T> T constructor(Class<?> clazz, MethodType methodType, Class<T> proxyType)
      Look up a constructor on a class defined to this test class maker.
      Type Parameters:
      T - the type of the proxy interface
      Parameters:
      clazz - the owner class (must not be null)
      methodType - the type of the constructor (must not be null)
      proxyType - the Class of the proxy interface (must not be null)
      Returns:
      the proxy instance which invokes the target constructor (not null)
      Throws:
      NoSuchElementException - if there is no class with that name defined to this class loader, or if there is no constructor which matches the expected type
    • newInstance

      public <T> T newInstance(String className, Class<T> expectedType)
      Construct a new instance using a no-argument constructor on the class.
      Type Parameters:
      T - the expected type
      Parameters:
      className - the owner class binary name (must not be null)
      expectedType - the expected type of the instance (must not be null)
      Returns:
      the new instance, cast to the given expected type
    • newInstance

      public <T> T newInstance(ClassDesc classDesc, Class<T> expectedType)
      Construct a new instance using a no-argument constructor on the class.
      Type Parameters:
      T - the expected type
      Parameters:
      classDesc - the owner class descriptor (must not be null)
      expectedType - the expected type of the instance (must not be null)
      Returns:
      the new instance, cast to the given expected type
    • newInstance

      public <T> T newInstance(Class<T> clazz)
      Construct a new instance using a no-argument constructor on the class.
      Type Parameters:
      T - the expected type
      Parameters:
      clazz - the owner class (must not be null)
      Returns:
      the new instance