Class MethodWrapper<T,R>
java.lang.Object
io.github.valentinherrmann.levenshtein.Wrapper<T>
io.github.valentinherrmann.levenshtein.MethodWrapper<T,R>
- Type Parameters:
T- the type of the class containing the methodR- the expected return type of the method
Wrapper for class methods that verifies their existence, name, return type, parameter types, and modifiers
using Levenshtein distance for fuzzy name matching.
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionMethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, Object[] paramTypes, String... modifiers) Constructs a new MethodWrapper whose return type is a student class, e.g.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, String... modifiers) LikeMethodWrapper(ClassWrapper, String, ClassWrapper, Object[], String...)for a method with no parameters.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Class<?>[] paramTypes, String... modifiers) Constructs a new MethodWrapper for verifying a class method.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Object[] paramTypes, String... modifiers) Constructs a new MethodWrapper whose parameter types may include student classes.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, String... modifiers) Constructs a new MethodWrapper for a method with no parameters.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, Object[] paramTypes, String... modifiers) LikeMethodWrapper(ClassWrapper, String, ClassWrapper, Object[], String...), but the wrapper of the return type is only taken from the supplier when the method is checked, e.g.MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, String... modifiers) LikeMethodWrapper(ClassWrapper, String, Supplier, Object[], String...)for a method with no parameters. -
Method Summary
Modifier and TypeMethodDescriptionReturns a string representation of the actual element signature found.Returns a string representation of the expected element signature.protected voidSearches for the wrapped element using deviation detection (e.g., Levenshtein distance).Class<?>[]Class<?> Resolves a student class given as return type.Class<?>[]Resolves student classes given as parameter types.protected intAbstract methods of interfaces are implicitlyabstract.Invokes the method on the class's default instance (or null for static methods).<X extends Throwable>
XinvokeExpectingException(Class<X> expected, Object obj, Object... params) Invokes the method and expects the student code to throw an exception of the given type.invokeOnSpecificObject(Object obj, Object... params) Invokes the method on a specific object instance.invokeOnSpecificObjectWithContext(Supplier<String> context, Object obj, Object... params) LikeinvokeOnSpecificObject(Object, Object...), but if the student code throws, the text ofcontextis put in front of the failure message (e.g. the scenario or the seed of generated inputs).invokeWithContext(Supplier<String> context, Object... params) Likeinvoke(Object...), but if the student code throws, the text ofcontextis put in front of the failure message, e.g. the scenario or the seed of generated inputs:protected booleanWhether an unexpectedstaticis reported withLevenshteinSettings.UnexpectedModifiers.IGNORE(the behaviour before unexpected modifiers were reported in general).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, isModifierExpected, parseExistence, toString, usability, verifyExistence, verifyModifiers
-
Constructor Details
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Class<?>[] paramTypes, String... modifiers) Constructs a new MethodWrapper for verifying a class method.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- the expected return type of the methodparamTypes- the expected parameter types for the methodmodifiers- the expected modifiers (e.g., "public", "static")
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, Object[] paramTypes, String... modifiers) Constructs a new MethodWrapper 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 method is checked; if one is missing, only the parameters count as missing.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- the expected return type of the methodparamTypes- the expected parameter types for the methodmodifiers- the expected modifiers (e.g., "public", "static")- Throws:
IllegalArgumentException- if a parameter type isnullor of another kind
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, Object[] paramTypes, String... modifiers) Constructs a new MethodWrapper whose return type is a student class, e.g.Drummer getDrummer(). The return type is only looked up when the method is checked; if the student class is missing, only the return type counts as missing.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- the wrapper of the expected return typeparamTypes- the expected parameter types, seeMethodWrapper(ClassWrapper, String, Class, Object[], String...)modifiers- the expected modifiers (e.g., "public", "static")
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends R> expectedReturnType, String... modifiers) LikeMethodWrapper(ClassWrapper, String, ClassWrapper, Object[], String...)for a method with no parameters.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- the wrapper of the expected return typemodifiers- the expected modifiers (e.g., "public", "static")
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, Object[] paramTypes, String... modifiers) LikeMethodWrapper(ClassWrapper, String, ClassWrapper, Object[], String...), but the wrapper of the return type is only taken from the supplier when the method is checked, e.g.() -> TestManager.drummer.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- supplies the wrapper of the expected return typeparamTypes- the expected parameter types, seeMethodWrapper(ClassWrapper, String, Class, Object[], String...)modifiers- the expected modifiers (e.g., "public", "static")
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends R>> expectedReturnType, String... modifiers) LikeMethodWrapper(ClassWrapper, String, Supplier, Object[], String...)for a method with no parameters.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- supplies the wrapper of the expected return typemodifiers- the expected modifiers (e.g., "public", "static")
-
MethodWrapper
public MethodWrapper(ClassWrapper<T> parentClass, String expectedName, Class<R> expectedReturnType, String... modifiers) Constructs a new MethodWrapper for a method with no parameters.- Parameters:
parentClass- the class wrapper containing this methodexpectedName- the expected name of the methodexpectedReturnType- the expected return type of the methodmodifiers- the expected modifiers (e.g., "public", "static")
-
-
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.
-
legacyReportsUnexpectedStatic
protected boolean legacyReportsUnexpectedStatic()Description copied from class:WrapperWhether an unexpectedstaticis reported withLevenshteinSettings.UnexpectedModifiers.IGNORE(the behaviour before unexpected modifiers were reported in general). Attributes and methods turn it on.- Overrides:
legacyReportsUnexpectedStaticin classWrapper<T>- Returns:
- true if an unexpected
staticis reported in the legacy mode
-
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
-
implicitModifiers
protected int implicitModifiers()Abstract methods of interfaces are implicitlyabstract.- Overrides:
implicitModifiersin classWrapper<T>- Returns:
- the mask of implicit modifier bits
-
findWithDeviation
protected void findWithDeviation()Description copied from class:WrapperSearches for the wrapped element using deviation detection (e.g., Levenshtein distance). Subclasses must implement this to perform element-specific searches and update the property states. Called at most once per wrapper, seeWrapper.ensureChecked().- Specified by:
findWithDeviationin classWrapper<T>
-
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>
-
invoke
Invokes the method on the class's default instance (or null for static methods). An exception thrown by the student code fails the test with the exception type, its message and the argument values.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:
params- values for the method's parameters- Returns:
- the return value of the method, safely cast to R
- See Also:
-
invokeWithContext
Likeinvoke(Object...), but if the student code throws, the text ofcontextis put in front of the failure message, e.g. the scenario or the seed of generated inputs:
The supplier is only evaluated if the call fails.car.calculateCost().invokeWithContext(() -> "Scenario " + i + ", seed " + seed, years);Pass a single array argument as
(Object) array: otherwise Java spreads an object array into several arguments.- Parameters:
context- supplies the text shown in front of the failure message (may benull)params- values for the method's parameters- Returns:
- the return value of the method, safely cast to R
-
invokeOnSpecificObject
Invokes the method on a specific object instance. An exception thrown by the student code fails the test with the exception type, its message and the argument values; useinvokeExpectingException(Class, Object, 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:
obj- the object to invoke the method on (null uses default instance or null for static methods)params- values for the method's parameters- Returns:
- the return value of the method, safely cast to R
- See Also:
-
invokeOnSpecificObjectWithContext
LikeinvokeOnSpecificObject(Object, Object...), but if the student code throws, the text ofcontextis put in front of the failure message (e.g. the scenario or the seed of generated inputs). The supplier is only evaluated if the call fails.Pass a single array argument as
(Object) array: otherwise Java spreads an object array into several arguments.- Parameters:
context- supplies the text shown in front of the failure message (may benull)obj- the object to invoke the method on (null uses default instance or null for static methods)params- values for the method's parameters- Returns:
- the return value of the method, safely cast to R
-
invokeExpectingException
public <X extends Throwable> X invokeExpectingException(Class<X> expected, Object obj, Object... params) Invokes the method 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)obj- the object to invoke the method on (null uses default instance or null for static methods)params- values for the method's parameters- Returns:
- the thrown exception, e.g. for further assertions on its message
-
getParamTypes
Resolves student classes given as parameter types.- Returns:
- the expected parameter types; a student class that is missing is
null
-
getExpectedReturnType
Resolves a student class given as return type.- Returns:
- the expected return type, or
nullif none was given or the student class is missing
-
getActualParamTypes
- Returns:
- the parameter types of the student's method, or the expected ones if the method is missing
-
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
-