Class BeanCreator<T>

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

public class BeanCreator<T> extends Object
Utility class for creating beans through constructors, creator methods, and builders.

Uses a BeanStore to find available ways to construct beans via injection of beans from the store.

This class is instantiated through the following method:

Example:

// Construct and throw a RuntimeException using a bean store. throw BeanStore .create() .build() .addBean(Throwable.class, cause) .addBean(String.class, msg) .addBean(Object[].class, args) .createBean(RuntimeException.class) .run();

Looks for the following methods for creating a bean:

  1. Looks for a singleton no-arg method of the form:

    public static MyClass getInstance();

  2. Looks for a static creator method of the form:

    public static MyClass create(<args>);

    • All arguments except Optional and List parameters must have beans available in the store.
    • If multiple methods are found, the one with the most matching parameters is used.
    • Deprecated and @BeanIgnore-annotated methods are ignored.
  3. Looks for a public constructor of the form:

    public MyClass(<args>);

    • All arguments except Optional and List parameters must have beans available in the store.
    • If multiple methods are found, the one with the most matching parameters is used.
    • Deprecated and @BeanIgnore-annotated methods are ignored.
  4. Looks for a protected constructor of the form:

    protected MyClass(<args>);

    • All arguments except Optional and List parameters must have beans available in the store.
    • If multiple methods are found, the one with the most matching parameters is used.
    • Deprecated and @BeanIgnore-annotated methods are ignored.
  5. Looks for a static no-arg create method that returns a builder object that can be passed in to a protected constructor.

    public static MyClass.Builder create(); protected MyClass(MyClass.Builder builder);

Notes:
See Also:
  • Constructor Details

    • BeanCreator

      protected BeanCreator(Class<T> type, BeanStore store)
      Constructor.
      Parameters:
      type - The bean type being created.
      store - The bean store creating this creator.
  • Method Details

    • of

      public static <T> BeanCreator<T> of(Class<T> beanType)
      Shortcut for calling BeanStore.INSTANCE.createBean(beanType).
      Type Parameters:
      T - The bean type to create.
      Parameters:
      beanType - The bean type to create.
      Returns:
      A new creator.
    • type

      public BeanCreator<T> type(Class<?> value)
      Allows you to specify a subclass of the specified bean type to create.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • type

      public BeanCreator<T> type(ClassInfo value)
      Allows you to specify a subclass of the specified bean type to create.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • impl

      public BeanCreator<T> impl(T value)
      Allows you to specify a specific instance for the build method to return.
      Parameters:
      value - The value for this setting.
      Returns:
      This object.
    • arg

      public <T2> BeanCreator<T> arg(Class<T2> beanType, T2 bean)
      Adds an argument to this creator.
      Type Parameters:
      T2 - The parameter type.
      Parameters:
      beanType - The parameter type.
      bean - The parameter value.
      Returns:
      This object.
    • silent

      public BeanCreator<T> silent()
      Suppresses throwing of ExecutableExceptions from the run() method when a form of creation cannot be found.
      Returns:
      This object.
    • builder

      public <B> BeanCreator<T> builder(Class<B> type, B value)
      Specifies a builder object for the bean type.
      Notes:
      • When specified, we don't look for a static creator method.
      Type Parameters:
      B - The class type of the builder.
      Parameters:
      type - The class type of the builder.
      value - The value for this setting.
      Returns:
      This object.
    • orElse

      public T orElse(T other)
      Same as run() but returns the alternate value if a method of creation could not be found.
      Parameters:
      other - The other bean to use.
      Returns:
      Either the created or other bean.
    • execute

      public Optional<T> execute()
      Same as run() but returns the value wrapped in an Optional.
      Returns:
      A new bean wrapped in an Optional.
    • run

      public T run()
      Creates the bean.
      Returns:
      A new bean.
      Throws:
      ExecutableException - if bean could not be created and silent() was not enabled.
    • supplier

      public Supplier<T> supplier()
      Converts this creator into a supplier.
      Returns:
      A supplier that returns the results of the run() method.