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 attribute
V - the expected type of the attribute value

public class AttributeWrapper<T,V> extends Wrapper<T>
Wrapper for class attributes (fields) that verifies their existence, name, type, and modifiers using Levenshtein distance for fuzzy matching.
  • 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 attribute
      expectedName - the expected name of the attribute
      expectedType - the expected type of the attribute
      modifiers - 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 attribute drummer of type Drummer. 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 attribute
      expectedName - the expected name of the attribute
      expectedType - 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)
      Like AttributeWrapper(ClassWrapper, String, ClassWrapper, String...), but the wrapper of the type is only taken from the supplier when the attribute is checked, e.g. () -> TestManager.drummer for a wrapper that is created later.
      Parameters:
      parentClass - the class wrapper containing this attribute
      expectedName - the expected name of the attribute
      expectedType - supplies the wrapper of the expected type
      modifiers - the expected modifiers (e.g., "public", "static", "final")
  • 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.
    • legacyReportsUnexpectedStatic

      protected boolean legacyReportsUnexpectedStatic()
      Fields of interfaces are implicitly static, so the legacy mode only reports an unexpected static for classes.
      Overrides:
      legacyReportsUnexpectedStatic in class Wrapper<T>
      Returns:
      true if an unexpected static is reported in the legacy mode
    • 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
    • implicitModifiers

      protected int implicitModifiers()
      Constants of interfaces and enum constants are implicitly static final, the component fields of records implicitly final.
      Overrides:
      implicitModifiers in class Wrapper<T>
      Returns:
      the mask of implicit modifier bits
    • getValue

      public V 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, use getValue(Object). Uses Assertions.fail(String) to report access failures.
      Returns:
      the attribute value
    • getValue

      public V getValue(Object obj)
      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>() or is<ActualName>()) if the field cannot be accessed. Uses Assertions.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

      public Class<?> getExpectedType()
      Resolves a student class given as type (see AttributeWrapper(ClassWrapper, String, ClassWrapper, String...)).
      Returns:
      the expected type of the attribute, or null if it is a student class that is missing
    • setValue

      public void setValue(Object value)
      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, use setValue(Object, Object). Fails if the attribute is declared final. Uses Assertions.fail(String) to report access failures.
      Parameters:
      value - the value to set
    • setValue

      public void setValue(Object obj, Object value)
      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. a double value into a float field); a narrowing conversion may lose precision. Uses Assertions.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:
      findWithDeviation in class Wrapper<T>
    • 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