Class AttributeWrapper<T,V>
java.lang.Object
io.github.valentinherrmann.levenshtein.Wrapper<T>
io.github.valentinherrmann.levenshtein.AttributeWrapper<T,V>
- Type Parameters:
T- the type of the class containing the attributeV- the expected type of the attribute value
Wrapper for class attributes (fields) that verifies their existence, name, type, and modifiers
using Levenshtein distance for fuzzy matching.
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionAttributeWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends V> expectedType, String... modifiers) Constructs a new AttributeWrapper for an attribute whose type is a student class, e.g. an attributedrummerof typeDrummer.AttributeWrapper(ClassWrapper<T> parentClass, String expectedName, Class<V> expectedType, String... modifiers) Constructs a new AttributeWrapper for verifying a class attribute.AttributeWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends V>> expectedType, String... modifiers) LikeAttributeWrapper(ClassWrapper, String, ClassWrapper, String...), but the wrapper of the type is only taken from the supplier when the attribute is checked, e.g. -
Method Summary
Modifier and TypeMethodDescriptionReturns a string representation of the actual element signature found.Returns a string representation of the expected element signature.protected voidAttempts to find a field in a class, allowing for deviation in the attribute name.Class<?> Resolves a student class given as type (seeAttributeWrapper(ClassWrapper, String, ClassWrapper, String...)).getValue()Retrieves the value of this attribute from a default instance.Retrieves the value of this attribute from a specific object instance.protected intConstants of interfaces and enum constants are implicitlystatic final, the component fields of records implicitlyfinal.protected booleanFields of interfaces are implicitly static, so the legacy mode only reports an unexpectedstaticfor classes.protected voidParses and updates the existence state based on all properties.protected intThe modifier bits that are meaningful for this kind of element.voidSets the value of this attribute on a default instance.voidSets the value of this attribute on a specific object instance.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
-
AttributeWrapper
public AttributeWrapper(ClassWrapper<T> parentClass, String expectedName, Class<V> expectedType, String... modifiers) Constructs a new AttributeWrapper for verifying a class attribute.- Parameters:
parentClass- the class wrapper containing this attributeexpectedName- the expected name of the attributeexpectedType- the expected type of the attributemodifiers- the expected modifiers (e.g., "public", "static", "final")
-
AttributeWrapper
public AttributeWrapper(ClassWrapper<T> parentClass, String expectedName, ClassWrapper<? extends V> expectedType, String... modifiers) Constructs a new AttributeWrapper for an attribute whose type is a student class, e.g. an attributedrummerof typeDrummer. The type is only looked up when the attribute is checked, so the wrappers can be built in any order. If the student class is missing, only the attribute's type counts as missing.- Parameters:
parentClass- the class wrapper containing this attributeexpectedName- the expected name of the attributeexpectedType- the wrapper of the expected type, e.g.ClassWrapper.reference("Drummer", "band")modifiers- the expected modifiers (e.g., "public", "static", "final")
-
AttributeWrapper
public AttributeWrapper(ClassWrapper<T> parentClass, String expectedName, Supplier<? extends ClassWrapper<? extends V>> expectedType, String... modifiers) LikeAttributeWrapper(ClassWrapper, String, ClassWrapper, String...), but the wrapper of the type is only taken from the supplier when the attribute is checked, e.g.() -> TestManager.drummerfor a wrapper that is created later.- Parameters:
parentClass- the class wrapper containing this attributeexpectedName- the expected name of the attributeexpectedType- supplies the wrapper of the expected typemodifiers- the expected modifiers (e.g., "public", "static", "final")
-
-
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()Fields of interfaces are implicitly static, so the legacy mode only reports an unexpectedstaticfor classes.- 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()Constants of interfaces and enum constants are implicitlystatic final, the component fields of records implicitlyfinal.- Overrides:
implicitModifiersin classWrapper<T>- Returns:
- the mask of implicit modifier bits
-
getValue
Retrieves the value of this attribute from a default instance. Uses the parent class's default object instance. To get the value from a specific instance, usegetValue(Object). UsesAssertions.fail(String)to report access failures.- Returns:
- the attribute value
-
getValue
Retrieves the value of this attribute from a specific object instance. If obj is null, creates or uses a default instance from the parent class. Attempts direct field access, falling back to a getter (get<ActualName>()oris<ActualName>()) if the field cannot be accessed. UsesAssertions.fail(String)to report access failures.- Parameters:
obj- the object instance to get the value from (null for static fields or default instance)- Returns:
- the attribute value, safely cast to type V
-
getExpectedType
Resolves a student class given as type (seeAttributeWrapper(ClassWrapper, String, ClassWrapper, String...)).- Returns:
- the expected type of the attribute, or
nullif it is a student class that is missing
-
setValue
Sets the value of this attribute on a default instance. Uses the parent class's default object instance. To set the value on a specific instance, usesetValue(Object, Object). Fails if the attribute is declared final. UsesAssertions.fail(String)to report access failures.- Parameters:
value- the value to set
-
setValue
Sets the value of this attribute on a specific object instance. If obj is null, creates or uses a default instance from the parent class. Fails if the attribute is declared final. A number is converted to the type of the student's field (e.g. adoublevalue into afloatfield); a narrowing conversion may lose precision. UsesAssertions.fail(String)to report access failures.- Parameters:
obj- the object instance to set the value on (null for static fields or default instance)value- the value to set
-
findWithDeviation
protected void findWithDeviation()Attempts to find a field in a class, allowing for deviation in the attribute name. The closest field (Levenshtein distance, ties broken by name) wins; synthetic fields and fields that another wrapper of the same class expects exactly are ignored.- 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>
-
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
-