Class ConstructorWrapper<T>

java.lang.Object
io.github.valentinherrmann.levenshtein.Wrapper<T>
io.github.valentinherrmann.levenshtein.ConstructorWrapper<T>
Type Parameters:
T - the type of the class whose constructor is being wrapped

public class ConstructorWrapper<T> extends Wrapper<T>
Wrapper for class constructors that verifies their existence, parameter types, and modifiers. Provides methods to invoke constructors and create instances.
  • Constructor Details

    • ConstructorWrapper

      public ConstructorWrapper(ClassWrapper<T> parentClass, Class<?>[] paramTypes, String... modifiers)
      Constructs a new ConstructorWrapper for verifying a class constructor.
      Parameters:
      parentClass - the class wrapper containing this constructor
      paramTypes - the expected parameter types for the constructor
      modifiers - the expected modifiers (e.g., "public", "protected")
    • ConstructorWrapper

      public ConstructorWrapper(ClassWrapper<T> parentClass, Object[] paramTypes, String... modifiers)
      Constructs a new ConstructorWrapper whose parameter types may include student classes. Each parameter type is a Class, a ClassWrapper (e.g. ClassWrapper.reference("Guitarist", "band")) or a Supplier of a class wrapper (needs a cast in an array: (Supplier<ClassWrapper<?>>) () -> x). Student classes are only looked up when the constructor is checked; if one is missing, only the parameters count as missing.
      Parameters:
      parentClass - the class wrapper containing this constructor
      paramTypes - the expected parameter types for the constructor
      modifiers - the expected modifiers (e.g., "public", "protected")
      Throws:
      IllegalArgumentException - if a parameter type is null or of another kind
    • ConstructorWrapper

      public ConstructorWrapper(ClassWrapper<T> parentClass, String modifiers)
      Constructs a new ConstructorWrapper for a no-argument constructor.
      Parameters:
      parentClass - the class wrapper containing this constructor
      modifiers - the expected modifiers (e.g., "public")
  • Method Details

    • verifyExistence

      public void verifyExistence(boolean throwAssertion)
      Description copied from class: Wrapper
      Verifies that the wrapped element exists in the actual class. Subclasses must implement this to perform element-specific existence checks.
      Specified by:
      verifyExistence in class Wrapper<T>
      Parameters:
      throwAssertion - if true, throws an AssertJ assertion failure when the element is missing. Usually set to true for behavioral tests, and false for structural tests to allow further deviation analysis.
    • findWithDeviation

      protected void findWithDeviation()
      Looks up the declared constructor (any visibility). If no constructor with exactly the expected parameter types exists, a constructor whose parameters only differ in primitive/wrapper types counts as a deviation.
      Specified by:
      findWithDeviation in class Wrapper<T>
    • invoke

      public T invoke(Object... args)
      Invokes the constructor with the specified arguments to create a new instance. For abstract classes, uses ByteBuddy to create a dynamic subclass instance. An exception thrown by the student code fails the test with the exception type, its message and the argument values; use invokeExpectingException(Class, Object...) if an exception is the expected outcome.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array (e.g. String[]) into several arguments. A wrong number of arguments is reported as IllegalArgumentException.

      Parameters:
      args - the arguments to pass to the constructor
      Returns:
      a new instance of the class
    • invokeExpectingException

      public <X extends Throwable> X invokeExpectingException(Class<X> expected, Object... args)
      Invokes the constructor and expects the student code to throw an exception of the given type.

      Pass a single array argument as (Object) array: otherwise Java spreads an object array into several arguments.

      Type Parameters:
      X - the exception type
      Parameters:
      expected - the expected exception type (subclasses are accepted)
      args - the arguments to pass to the constructor
      Returns:
      the thrown exception, e.g. for further assertions on its message
    • relevantModifierMask

      protected int relevantModifierMask()
      Description copied from class: Wrapper
      The modifier bits that are meaningful for this kind of element. Other bits of the bitmask are ignored: for methods, the bits of volatile and transient mean bridge and varargs. The default fits classes; methods, attributes and constructors override it.
      Overrides:
      relevantModifierMask in class Wrapper<T>
      Returns:
      the mask of relevant modifier bits
    • parseExistence

      protected void parseExistence()
      Description copied from class: Wrapper
      Parses and updates the existence state based on all properties. Subclasses must implement this to define how to aggregate property states.
      Specified by:
      parseExistence in class Wrapper<T>
    • expectedToString

      public String expectedToString()
      Description copied from class: Wrapper
      Returns a string representation of the expected element signature. Subclasses must implement this to provide element-specific formatting.
      Specified by:
      expectedToString in class Wrapper<T>
      Returns:
      a string showing the expected element details
    • actualToString

      public String actualToString()
      Description copied from class: Wrapper
      Returns a string representation of the actual element signature found. Subclasses must implement this to provide element-specific formatting.
      Specified by:
      actualToString in class Wrapper<T>
      Returns:
      a string showing the actual element details
    • getParamTypes

      public Class<?>[] getParamTypes()
      Retrieves the parameter types expected for this constructor, resolving student classes given as types.
      Returns:
      an array of parameter types; a student class that is missing is null
    • getActualParamTypes

      public Class<?>[] getActualParamTypes()
      Retrieves the parameter types of the constructor actually found (they may differ in primitive/wrapper types).
      Returns:
      the actual parameter types, or the expected ones if the constructor is missing