Class PropertyAccessor<T,V>

java.lang.Object
org.linkki.util.reflection.accessor.PropertyAccessor<T,V>
Type Parameters:
T - the type containing the property
V - the property's type

public final class PropertyAccessor<T,V> extends Object
Allows reading and writing a value from/to an object's property. Also provides the value class of the property.

For accessing an object's property, create a PropertyAccessor for the class to be accessed and the name of the property. The accessor can determine whether the property can be read, written or invoked.

  • Method Details

    • getPropertyName

      public String getPropertyName()
    • getPropertyValue

      public V getPropertyValue(T boundObject)
      Reads the property's value.
      Throws:
      RuntimeException - if the method cannot be called
    • setPropertyValue

      public void setPropertyValue(T boundObject, @CheckForNull V value)
      Sets the property to the given value.
      Throws:
      RuntimeException - if the method cannot be called
    • invoke

      public void invoke(T boundObject)
      Invokes the method.
      Throws:
      RuntimeException - if the method cannot be called
    • canWrite

      public boolean canWrite()
      Returns:
      true if there is a read method (getter) for the given object and property
    • canInvoke

      public boolean canInvoke()
      Returns:
      true if there is a method that can be invoked for the given object and property
    • canRead

      public boolean canRead()
      Returns:
      true if there is a write method (setter) for the given object and property
    • getValueClass

      public Class<?> getValueClass()
      Returns:
      the return type of the getter, i.e. class of the value the read method returns.
      Throws:
      IllegalStateException - if there is no read method for this property
    • get

      public static <T> PropertyAccessor<T,?> get(Class<T> clazz, String property)
      Parameters:
      clazz - a class
      property - a property of the class
      Returns:
      a PropertyAccessor to access the property of instances of the class