Class Property<T,V>

java.lang.Object
org.apache.juneau.commons.reflect.Property<T,V>
Type Parameters:
T - The object type.
V - The value type.

public class Property<T,V> extends Object
A typed property descriptor that allows you to specify arbitrary consumers and producers corresponding to setters and getters.

This class provides a flexible, builder-based approach to defining properties on objects, supporting both method-based and field-based access patterns.

Features:
  • Type-safe - generic types for object and value types
  • Builder-based - fluent API for construction
  • Flexible - supports arbitrary consumers and producers that can throw exceptions
  • Convenience methods - easy integration with FieldInfo and MethodInfo
  • Exception handling - uses ThrowingFunction and ThrowingConsumer2 for exception support
Usage:

// Create property with method getter and setter Property<MyClass, String> nameProperty = Property .<MyClass, String>create() .getter(obj -> obj.getName()) .setter((obj, val) -> obj.setName(val)) .build(); // Create property from FieldInfo FieldInfo field = ClassInfo.of(MyClass.class).getField("name"); Property<MyClass, String> fieldProperty = Property .<MyClass, String>create() .field(field) .build(); // Create property from MethodInfo getter and setter MethodInfo getter = ClassInfo.of(MyClass.class).getMethod("getName"); MethodInfo setter = ClassInfo.of(MyClass.class).getMethod("setName", String.class); Property<MyClass, String> methodProperty = Property .<MyClass, String>create() .getter(getter) .setter(setter) .build(); // Use property MyClass obj = new MyClass(); nameProperty.set(obj, "John"); String value = nameProperty.get(obj);

See Also:
  • Constructor Details

    • Property

      public Property(ThrowingFunction<T,V> producer, ThrowingConsumer2<T,V> consumer)
      Constructor.
      Parameters:
      producer - The producer function (getter). Can be null.
      consumer - The consumer function (setter). Can be null.
  • Method Details

    • create

      public static <T, V> Property.Builder<T,V> create()
      Creates a new builder for constructing a property.
      Example:

      Property<MyClass, String> prop = Property .<MyClass, String>create() .getter(obj -> obj.getName()) .setter((obj, val) -> obj.setName(val)) .build();

      Type Parameters:
      T - The object type.
      V - The value type.
      Returns:
      A new builder instance.
    • get

      public V get(T object) throws ExecutableException
      Gets the value from the specified object using the producer (getter).
      Parameters:
      object - The object from which to get the value. Must not be null.
      Returns:
      The value.
      Throws:
      ExecutableException
    • set

      public void set(T object, V value) throws ExecutableException
      Sets the value on the specified object using the consumer (setter).
      Parameters:
      object - The object on which to set the value. Must not be null.
      value - The value to set. Can be null.
      Throws:
      ExecutableException
    • canRead

      public boolean canRead()
      Returns true if this property can be read.
      Returns:
      true if this property can be read.
    • canWrite

      public boolean canWrite()
      Returns true if this property can be written.
      Returns:
      true if this property can be written.