Class AbstractByteCodeWriterTck

java.lang.Object
io.micronaut.sourcegen.bytecode.tck.AbstractByteCodeWriterTck
Direct Known Subclasses:
BridgeTck, ByteCodeWriterTck, ControlFlowTck, ConversionTck, ExpressionTck, InferenceTck, LambdaAndReferenceTck, MemberTypeTck, OverloadSelectionTck, ReceiverTck, SignatureTck

public abstract class AbstractByteCodeWriterTck extends Object
The support shared by every topic of the bytecode writer TCK: writing definitions with the backend under test, loading them, and calling them next to javac's own call of a compiled fixture.

A backend runs a topic by extending it and implementing write(ObjectDef).

Since:
2.3
  • Constructor Details

    • AbstractByteCodeWriterTck

      public AbstractByteCodeWriterTck()
  • Method Details

    • write

      protected abstract byte[] write(ObjectDef definition)
      Writes one definition to class file bytes with the backend under test.

      An implementation should assert whatever its backend guarantees about how the bytes were produced, such as verifying them or refusing a fallback path, so that a backend cannot pass the TCK by quietly declining to write the definition itself.

      Parameters:
      definition - The definition to write
      Returns:
      The class file bytes
    • define

      protected final Class<?> define(ObjectDef definition) throws ClassNotFoundException
      Writes and loads one definition in a class loader of its own.
      Parameters:
      definition - The definition to write and load
      Returns:
      The loaded class
      Throws:
      ClassNotFoundException - If the class cannot be loaded
    • load

      protected final ClassLoader load(ObjectDef... definitions)
      Writes definitions into one class loader, so that they can refer to each other.
      Parameters:
      definitions - The definitions to write
      Returns:
      The class loader that defines them
    • defineInPackage

      protected final Class<?> defineInPackage(ObjectDef definition) throws IllegalAccessException
      Defines a class in the package and the class loader of the fixtures, which reaches their package-private members. The definition must be named in this package.
      Parameters:
      definition - The definition to write and define
      Returns:
      The defined class
      Throws:
      IllegalAccessException - If the class cannot be defined in this package
    • assertCall

      protected final void assertCall(Object expected, List<TypeDef> types, List<?> values, Function<List<ExpressionDef>, ExpressionDef> body) throws Exception
      Generates a static call method taking parameters of the given types, invokes it with the given values and asserts its result. The expected value is usually javac's own call of the same fixture, so that the generated call must choose what javac chooses.
      Parameters:
      expected - The expected result
      types - The parameter types of the generated method
      values - The arguments to call it with
      body - The expression the method returns, given its parameters
      Throws:
      Exception - If the class cannot be written, loaded or called
    • run

      protected final Object run(ExpressionDef expression, ObjectDef... dependencies) throws Exception
      Generates a static call method that returns the expression, loads it next to its dependencies and calls it.
      Parameters:
      expression - The expression to return
      dependencies - The definitions the expression refers to
      Returns:
      The value of the expression
      Throws:
      Exception - If the classes cannot be written, loaded or called
    • run

      protected final Object run(ClassDef caller, ObjectDef... dependencies) throws Exception
      Loads a caller made by caller(ExpressionDef) next to its dependencies and calls it.
      Parameters:
      caller - The caller
      dependencies - The definitions the caller refers to
      Returns:
      The result of the call
      Throws:
      Exception - If the classes cannot be written, loaded or called
    • run

      protected final Method run(String name, TypeDef returns, List<TypeDef> parameters, BiFunction<VariableDef.This, List<VariableDef.MethodParameter>, StatementDef> body) throws Exception
      Writes and loads a class with one instance method run.
      Parameters:
      name - The class name
      returns - The return type of the method
      parameters - The parameter types of the method
      body - The body of the method
      Returns:
      The loaded method
      Throws:
      Exception - If the class cannot be written or loaded
    • invoke

      protected static Object invoke(Method run, Object... arguments) throws Exception
      Invokes a method loaded by run(String, TypeDef, List, BiFunction) on a new instance.
      Parameters:
      run - The method
      arguments - The arguments
      Returns:
      The result
      Throws:
      Exception - If the call fails
    • caller

      protected static ClassDef caller(ExpressionDef expression)
      A class with a static call method that returns the expression.
      Parameters:
      expression - The expression
      Returns:
      The class
    • callWithParameter

      protected static ClassDef callWithParameter(TypeDef parameter, Function<ExpressionDef, ExpressionDef> body)
      A class with a static call method that takes one parameter and returns the body.
      Parameters:
      parameter - The parameter type
      body - The returned expression, given the parameter
      Returns:
      The class
    • selection

      protected static MethodDef selection(String name, TypeDef parameter, String value)
      A public static overload that identifies itself by the value it returns.
      Parameters:
      name - The method name
      parameter - The parameter type
      value - The value the overload returns
      Returns:
      The method
    • selection

      protected static MethodDef selection(String name, TypeDef parameter, String value, Modifier access)
      A static overload that identifies itself by the value it returns.
      Parameters:
      name - The method name
      parameter - The parameter type
      value - The value the overload returns
      access - The access modifier of the overload
      Returns:
      The method
    • list

      protected static ClassTypeDef list(TypeDef argument)
      Parameters:
      argument - The type argument
      Returns:
      List<argument>
    • shape

      protected static List<String> shape(Class<?> type)
      The declared methods of a class as javac writes them: name, erased descriptor, access, bridges and defaults.
      Parameters:
      type - The class
      Returns:
      One line per method, sorted
    • genericShape

      protected static List<String> genericShape(Class<?> type)
      The generic declarations of the methods of a class, its own name read as SELF.
      Parameters:
      type - The class
      Returns:
      One line per method, sorted
    • classShape

      protected static String classShape(Class<?> type)
      The generic supertypes of a class, its own name read as SELF.
      Parameters:
      type - The class
      Returns:
      The superclass and interfaces
    • outcome

      protected static Object outcome(Callable<?> call)
      What a call returns, or the name of the exception it throws.
      Parameters:
      call - The call
      Returns:
      The result or the exception class name
    • declaredMethods

      protected static Method[] declaredMethods(Class<?> type)
      The methods a class declares, without the members a coverage agent adds to it ($jacocoInit), which a class compiled by javac has when the tests run with coverage and a written class does not.
      Parameters:
      type - The class
      Returns:
      The methods
    • declaredFields

      protected static Field[] declaredFields(Class<?> type)
      The fields a class declares, without the fields a coverage agent adds to it ($jacocoData).
      Parameters:
      type - The class
      Returns:
      The fields