Interface Getter<T>

  • Type Parameters:
    T - The generic base type.
    All Superinterfaces:
    Function<T,​Object>, Serializable

    public interface Getter<T>
    extends Function<T,​Object>, Serializable

    So we can extract method info from a method reference. Usage example:

     Map<Getter<YourEntity>, Object> criteria = new HashMap<>();
     criteria.put(YourEntity::getName, Like.startsWith(searchNameStartsWith));
     criteria.put(YourEntity::getCreated, Order.greaterThanOrEqualTo(searchStartDate));
     criteria.put(YourEntity::getType, searchTypes);
     criteria.put(YourEntity::isDeleted, false);
     

    And then later on in "the backend":

     criteria.forEach((getter, value) -> requiredCriteria.put(getter.getPropertyName(), value));
     

    This allows a type safe way of defining property names.

    Inspired by Lambda parameter names with reflection. NOTE: works only in Java 8u60 and newer.

    Author:
    Bauke Scholtz
    • Method Detail

      • getBaseType

        default Class<T> getBaseType()
      • getMethod

        default Method getMethod()
      • getPropertyName

        default String getPropertyName()
      • getReturnType

        default Class<?> getReturnType()