Interface PropertyExtractor

All Known Implementing Classes:
PropertyExtractors.ListPropertyExtractor, PropertyExtractors.MapPropertyExtractor, PropertyExtractors.ObjectPropertyExtractor

public interface PropertyExtractor
Interface for custom property extraction strategies in the Bean-Centric Testing framework.

Property extractors define how the converter accesses object properties during nested field navigation (e.g., "user.address.city"). The framework uses a chain-of-responsibility pattern, trying each registered extractor until one can handle the property access.

Extraction Strategy:

The two-phase approach ensures efficient and flexible property access:

  1. canExtract(BeanConverter, Object, String): Quick check if this extractor can handle the property
  2. extract(BeanConverter, Object, String): Perform the actual property extraction
Common Use Cases:
  • JavaBean properties: Standard getter methods and public fields
  • Map-style access: Key-based property retrieval from Map objects
  • Collection indices: Numeric access for arrays and lists
  • Custom data structures: Domain-specific property access patterns
  • Dynamic properties: Computed or cached property values
Implementation Example:

// Custom extractor for database entities public class DatabaseEntityExtractor implements PropertyExtractor { @Override public boolean canExtract(BeanConverter converter, Object obj, String property) { return obj instanceof DatabaseEntity; } @Override public Object extract(BeanConverter converter, Object obj, String property) { DatabaseEntity entity = (DatabaseEntity) obj; switch (property) { case "id": return entity.getPrimaryKey(); case "lastModified": return entity.getTimestamp(); case "metadata": return entity.getMetadata().asMap(); default: return entity.getAttribute(property); } } }

Registration and Usage:

var converter = BasicBeanConverter.builder() .defaultSettings() // Adds standard extractors .addPropertyExtractor(new DatabaseEntityExtractor()) .addPropertyExtractor((conv, obj, prop) -> { // Lambda-based extractor for simple cases if (obj instanceof MyConfig config && "timeout".equals(prop)) { return config.getTimeoutMillis(); } return null; // Let next extractor try }) .build();

Best Practices:
  • Fast canExtract() checks: Use efficient type checking and avoid expensive operations
  • Handle edge cases: Gracefully handle null objects and invalid property names
  • Consider caching: Cache reflection results for better performance
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canExtract(BeanConverter converter, Object o, String key)
    Determines if this extractor can handle property access for the given object and property name.
    extract(BeanConverter converter, Object o, String key)
    Extracts the specified property value from the given object.
  • Method Details

    • canExtract

      boolean canExtract(BeanConverter converter, Object o, String key)
      Determines if this extractor can handle property access for the given object and property name.

      This method should perform a quick check to determine compatibility without doing expensive operations. It's called frequently during property navigation.

      Parameters:
      converter - The bean converter instance (for recursive operations if needed)
      o - The object to extract the property from
      key - The property name to extract
      Returns:
      true if this extractor can handle the property access, false otherwise
    • extract

      Object extract(BeanConverter converter, Object o, String key)
      Extracts the specified property value from the given object.

      This method is only called after canExtract(BeanConverter, Object, String) returns true. It should perform the actual property extraction and return the property value.

      Parameters:
      converter - The bean converter instance (for recursive operations if needed)
      o - The object to extract the property from
      key - The property name to extract
      Returns:
      The property value
      Throws:
      PropertyNotFoundException - if the property cannot be found on the object