Class BeanCreateMethodFinder<T>

java.lang.Object
org.apache.juneau.cp.BeanCreateMethodFinder<T>
Type Parameters:
T - The bean type being created.

public class BeanCreateMethodFinder<T> extends Object
Utility class for creating beans through creator methods.

Used for finding and invoking methods on an object that take in arbitrary parameters and returns bean instances.

This class is instantiated through the following methods:

Example:

// The bean we want to create. public class A {} // The bean that has a creator method for the bean above. public class B { // Creator method. // Bean store must have a C bean and optionally a D bean. public A createA(C c, Optional<D> d) { return new A(c, d.orElse(null)); } } // Instantiate the bean with the creator method. B b = new B(); // Create a bean store with some mapped beans. BeanStore beanStore = BeanStore.create().addBean(C.class, new C()); // Instantiate the bean using the creator method. A a = beanStore .createMethodFinder(A.class, b) // Looking for creator for A on b object. .find("createA") // Look for method called "createA". .thenFind("createA2") // Then look for method called "createA2". .withDefault(()->new A()) // Optionally supply a default value if method not found. .run(); // Execute.

See Also:
  • Method Details

    • addBean

      public <T2> BeanCreateMethodFinder<T> addBean(Class<T2> c, T2 t)
      Adds a bean to the lookup for parameters.
      Type Parameters:
      T2 - The bean type.
      Parameters:
      c - The bean type.
      t - The bean.
      Returns:
      This object.
    • find

      Find the method matching the specified predicate.

      In order for the method to be used, it must adhere to the following restrictions:

      • The method must be public.
      • The method can be static.
      • The method name must match exactly.
      • The method must not be deprecated or annotated with BeanIgnore.
      • The method must have all parameter types specified in requiredParams.
      • The bean store must contain beans for all parameter types.
      • The bean store may contain beans for all Optional parameter types.

      This method can be called multiple times with different method names or required parameters until a match is found.
      Once a method is found, subsequent calls to this method will be no-ops. See BeanStore.createMethodFinder(Class, Object) for usage.

      Parameters:
      filter - The predicate to apply.
      Returns:
      This object.
    • find

      public BeanCreateMethodFinder<T> find(String methodName)
      Shortcut for calling find(x -> x.hasName(methodName)).
      Parameters:
      methodName - The method name to match.
      Returns:
      This object.
    • thenFind

      Identical to find(Predicate) but named for fluent-style calls.
      Parameters:
      filter - The predicate to apply.
      Returns:
      This object.
    • thenFind

      public BeanCreateMethodFinder<T> thenFind(String methodName)
      Identical to find(Predicate) but named for fluent-style calls.
      Parameters:
      methodName - The method name to match.
      Returns:
      This object.
    • withDefault

      A default value to return if no matching methods were found.
      Parameters:
      def - The default value. Can be null.
      Returns:
      This object.
    • withDefault

      A default value to return if no matching methods were found.
      Parameters:
      def - The default value.
      Returns:
      This object.
    • run

      public T run() throws ExecutableException
      Executes the matched method and returns the result.
      Returns:
      The object returned by the method invocation, or the default value if method was not found.
      Throws:
      ExecutableException - If method invocation threw an exception.
    • run

      public T run(Consumer<? super T> consumer) throws ExecutableException
      Same as run() but also executes a consumer if the returned value was not null.
      Parameters:
      consumer - The consumer of the response.
      Returns:
      The object returned by the method invocation, or the default value if method was not found, or Optional.empty().
      Throws:
      ExecutableException - If method invocation threw an exception.