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
Wrapper for class constructors that verifies their existence, parameter types, and modifiers.
Provides methods to invoke constructors and create instances.
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructorWrapper(ClassWrapper<T> parentClass, Class<?>[] paramTypes, String... modifiers) Constructs a new ConstructorWrapper for verifying a class constructor.ConstructorWrapper(ClassWrapper<T> parentClass, Object[] paramTypes, String... modifiers) Constructs a new ConstructorWrapper whose parameter types may include student classes.ConstructorWrapper(ClassWrapper<T> parentClass, String modifiers) Constructs a new ConstructorWrapper for a no-argument constructor. -
Method Summary
Modifier and TypeMethodDescriptionReturns a string representation of the actual element signature found.Returns a string representation of the expected element signature.protected voidLooks up the declared constructor (any visibility).Class<?>[]Retrieves the parameter types of the constructor actually found (they may differ in primitive/wrapper types).Class<?>[]Retrieves the parameter types expected for this constructor, resolving student classes given as types.Invokes the constructor with the specified arguments to create a new instance.<X extends Throwable>
XinvokeExpectingException(Class<X> expected, Object... args) Invokes the constructor and expects the student code to throw an exception of the given type.protected voidParses and updates the existence state based on all properties.protected intThe modifier bits that are meaningful for this kind of element.voidverifyExistence(boolean throwAssertion) Verifies that the wrapped element exists in the actual class.Methods inherited from class Wrapper
ensureChecked, getActualModifiers, getActualName, getExpectedName, getMissingModifiers, getOverallExistence, getParentClassWrapper, getUnexpectedModifiers, hasUnexpectedModifier, implicitModifiers, isModifierExpected, legacyReportsUnexpectedStatic, parseExistence, toString, usability, verifyExistence, verifyModifiers
-
Constructor Details
-
ConstructorWrapper
Constructs a new ConstructorWrapper for verifying a class constructor.- Parameters:
parentClass- the class wrapper containing this constructorparamTypes- the expected parameter types for the constructormodifiers- the expected modifiers (e.g., "public", "protected")
-
ConstructorWrapper
Constructs a new ConstructorWrapper whose parameter types may include student classes. Each parameter type is aClass, aClassWrapper(e.g.ClassWrapper.reference("Guitarist", "band")) or aSupplierof 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 constructorparamTypes- the expected parameter types for the constructormodifiers- the expected modifiers (e.g., "public", "protected")- Throws:
IllegalArgumentException- if a parameter type isnullor of another kind
-
ConstructorWrapper
Constructs a new ConstructorWrapper for a no-argument constructor.- Parameters:
parentClass- the class wrapper containing this constructormodifiers- the expected modifiers (e.g., "public")
-
-
Method Details
-
verifyExistence
public void verifyExistence(boolean throwAssertion) Description copied from class:WrapperVerifies that the wrapped element exists in the actual class. Subclasses must implement this to perform element-specific existence checks.- Specified by:
verifyExistencein classWrapper<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:
findWithDeviationin classWrapper<T>
-
invoke
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; useinvokeExpectingException(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 asIllegalArgumentException.- Parameters:
args- the arguments to pass to the constructor- Returns:
- a new instance of the class
-
invokeExpectingException
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:WrapperThe modifier bits that are meaningful for this kind of element. Other bits of the bitmask are ignored: for methods, the bits ofvolatileandtransientmean bridge and varargs. The default fits classes; methods, attributes and constructors override it.- Overrides:
relevantModifierMaskin classWrapper<T>- Returns:
- the mask of relevant modifier bits
-
parseExistence
protected void parseExistence()Description copied from class:WrapperParses and updates the existence state based on all properties. Subclasses must implement this to define how to aggregate property states.- Specified by:
parseExistencein classWrapper<T>
-
expectedToString
Description copied from class:WrapperReturns a string representation of the expected element signature. Subclasses must implement this to provide element-specific formatting.- Specified by:
expectedToStringin classWrapper<T>- Returns:
- a string showing the expected element details
-
actualToString
Description copied from class:WrapperReturns a string representation of the actual element signature found. Subclasses must implement this to provide element-specific formatting.- Specified by:
actualToStringin classWrapper<T>- Returns:
- a string showing the actual element details
-
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
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
-