Class Beans

java.lang.Object
org.omnifaces.util.Beans

@Typed public final class Beans extends Object

Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the BeanManager.

Usage

Here are some examples:

 // Get the CDI managed bean reference (proxy) of the given bean class.
 SomeBean someBean = Beans.getReference(SomeBean.class);
 
 // Get the CDI managed bean instance (actual) of the given bean class.
 SomeBean someBean = Beans.getInstance(SomeBean.class);
 
 // Check if CDI session scope is active in current context.
 Beans.isActive(SessionScope.class);
 
 // Get all currently active CDI managed bean instances in the session scope.
 Map<Object, String> activeSessionScopedBeans = Beans.getActiveInstances(SessionScope.class);
 
 // Destroy any currently active CDI managed bean instance of given bean class.
 Beans.destroy(SomeBean.class);
 
 // Fire a CDI event.
 Beans.fireEvent(someEvent);
 

For a full list, check the method summary.

Since:
1.6.1
Author:
Bauke Scholtz
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    destroy(Bean<T> bean)
    Destroy the currently active instance of the given CDI managed bean representation.
    static <T> void
    destroy(Class<T> beanClass, Annotation... qualifiers)
    Destroy the currently active instance of the given CDI managed bean class, optionally with the given qualifiers.
    static <T> void
    destroy(T instance)
    Destroy the currently active instance of the given CDI managed bean instance.
    static void
    fireEvent(Object event, Annotation... qualifiers)
    Fires the given CDI event, optionally with the given qualifiers.
    static <S extends Annotation>
    Map<Object,String>
    Returns all active CDI managed bean instances in the given CDI managed bean scope.
    static <A extends Annotation>
    A
    getAnnotation(Annotated annotated, Class<A> annotationType)
    Get program element annotation of a certain annotation type.
    Gets the current injection point when called from a context where injection is taking place (e.g. from a producer).
    static <T> T
    getInstance(Bean<T> bean, boolean create)
    Returns the CDI managed bean instance (actual) of the given bean representation and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
    static <T> T
    getInstance(Class<T> beanClass, boolean create, Annotation... qualifiers)
    Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers, and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
    static <T> T
    getInstance(Class<T> beanClass, Annotation... qualifiers)
    Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers, and creates one if one doesn't exist.
    static <T> T
    Returns the CDI managed bean instance (actual) associated with the given bean name and creates one if one doesn't exist.
    static <T> T
    getInstance(String name, boolean create)
    Returns the CDI managed bean instance (actual) associated with the given bean name and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
    Returns the CDI bean manager.
    static <A extends Annotation>
    A
    getQualifier(InjectionPoint injectionPoint, Class<A> qualifierClass)
    Returns the qualifier annotation of the given qualifier class from the given injection point.
    static <T> T
    getReference(Bean<T> bean)
    Returns the CDI managed bean reference (proxy) of the given bean representation.
    static <T> T
    getReference(Class<T> beanClass, Annotation... qualifiers)
    Returns the CDI managed bean reference (proxy) of the given bean class, optionally with the given qualifiers.
    static <S extends Annotation>
    boolean
    isActive(Class<S> scope)
    Returns true when the given CDI managed bean scope is active.
    static <T> boolean
    isProxy(T object)
    Returns true if given object or class is actually a CDI proxy.
    static <T> Bean<T>
    resolve(Class<T> beanClass, Annotation... qualifiers)
    Returns the CDI managed bean representation of the given bean class, optionally with the given qualifiers.
    static <T> Bean<T>
    resolveExact(Class<T> beanClass, Annotation... qualifiers)
    Returns the CDI managed bean representation of exactly the given bean class, optionally with the given qualifiers.
    static <T> T
    Returns the actual instance or class of the given object or class if it is actually a CDI proxy as per isProxy(Object).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getManager

      public static BeanManager getManager()
      Returns the CDI bean manager.
      Returns:
      The CDI bean manager.
      Since:
      2.0
      See Also:
    • resolve

      public static <T> Bean<T> resolve(Class<T> beanClass, Annotation... qualifiers)
      Returns the CDI managed bean representation of the given bean class, optionally with the given qualifiers.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      beanClass - The CDI managed bean class.
      qualifiers - The CDI managed bean qualifiers, if any.
      Returns:
      The CDI managed bean representation of the given bean class, or null if there is none.
      See Also:
    • resolveExact

      public static <T> Bean<T> resolveExact(Class<T> beanClass, Annotation... qualifiers)
      Returns the CDI managed bean representation of exactly the given bean class, optionally with the given qualifiers. This will ignore any subclasses.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      beanClass - The CDI managed bean class.
      qualifiers - The CDI managed bean qualifiers, if any.
      Returns:
      The CDI managed bean representation of the given bean class, or null if there is none.
      Since:
      3.1
      See Also:
    • getReference

      public static <T> T getReference(Class<T> beanClass, Annotation... qualifiers)
      Returns the CDI managed bean reference (proxy) of the given bean class, optionally with the given qualifiers. Note that this actually returns a client proxy and the underlying actual instance is thus always auto-created.
      Type Parameters:
      T - The expected return type.
      Parameters:
      beanClass - The CDI managed bean class.
      qualifiers - The CDI managed bean qualifiers, if any.
      Returns:
      The CDI managed bean reference (proxy) of the given class, or null if there is none.
      See Also:
    • getReference

      public static <T> T getReference(Bean<T> bean)
      Returns the CDI managed bean reference (proxy) of the given bean representation. Note that this actually returns a client proxy and the underlying actual instance is thus always auto-created.
      Type Parameters:
      T - The expected return type.
      Parameters:
      bean - The CDI managed bean representation.
      Returns:
      The CDI managed bean reference (proxy) of the given bean, or null if there is none.
      See Also:
    • getInstance

      public static <T> T getInstance(Class<T> beanClass, Annotation... qualifiers)
      Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers, and creates one if one doesn't exist.
      Type Parameters:
      T - The expected return type.
      Parameters:
      beanClass - The CDI managed bean class.
      qualifiers - The CDI managed bean qualifiers, if any.
      Returns:
      The CDI managed bean instance (actual) of the given bean class.
      Since:
      1.8
      See Also:
    • getInstance

      public static <T> T getInstance(Class<T> beanClass, boolean create, Annotation... qualifiers)
      Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers, and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
      Type Parameters:
      T - The expected return type.
      Parameters:
      beanClass - The CDI managed bean class.
      create - Whether to create create CDI managed bean instance if one doesn't exist.
      qualifiers - The CDI managed bean qualifiers, if any.
      Returns:
      The CDI managed bean instance (actual) of the given bean class, or null if there is none and/or the create argument is false.
      Since:
      1.7
      See Also:
    • getInstance

      public static <T> T getInstance(Bean<T> bean, boolean create)
      Returns the CDI managed bean instance (actual) of the given bean representation and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
      Type Parameters:
      T - The expected return type.
      Parameters:
      bean - The CDI managed bean representation.
      create - Whether to create create CDI managed bean instance if one doesn't exist.
      Returns:
      The CDI managed bean instance (actual) of the given bean, or null if there is none and/or the create argument is false.
      Since:
      1.7
      See Also:
    • getInstance

      public static <T> T getInstance(String name, boolean create)
      Returns the CDI managed bean instance (actual) associated with the given bean name and creates one if one doesn't exist and create argument is true, otherwise don't create one and return null if there's no current instance.
      Type Parameters:
      T - The expected return type.
      Parameters:
      name - The CDI managed bean name.
      create - Whether to create create CDI managed bean instance if one doesn't exist.
      Returns:
      The CDI managed bean instance (actual) associated with the given bean name, or null if there is none and/or the create argument is false.
      Since:
      3.14
      See Also:
    • getInstance

      public static <T> T getInstance(String name)
      Returns the CDI managed bean instance (actual) associated with the given bean name and creates one if one doesn't exist.
      Type Parameters:
      T - The expected return type.
      Parameters:
      name - The CDI managed bean name.
      Returns:
      The CDI managed bean instance (actual) associated with the given bean name, or null if there is none.
      Since:
      3.14
      See Also:
    • isProxy

      public static <T> boolean isProxy(T object)
      Returns true if given object or class is actually a CDI proxy.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      object - The object to be checked.
      Returns:
      true if given object or class is actually a CDI proxy.
      Since:
      3.8
    • unwrapIfNecessary

      public static <T> T unwrapIfNecessary(T object)
      Returns the actual instance or class of the given object or class if it is actually a CDI proxy as per isProxy(Object).
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      object - The object or class to be unwrapped.
      Returns:
      The actual instance or class of the given object or class if it is actually a CDI proxy as per isProxy(Object).
      Since:
      3.8
    • isActive

      public static <S extends Annotation> boolean isActive(Class<S> scope)
      Returns true when the given CDI managed bean scope is active. I.e., all beans therein can be accessed without facing ContextNotActiveException.
      Type Parameters:
      S - The generic CDI managed bean scope type.
      Parameters:
      scope - The CDI managed bean scope, e.g. SessionScoped.class.
      Returns:
      true when the given CDI managed bean scope is active.
      Since:
      2.3
      See Also:
    • getActiveInstances

      public static <S extends Annotation> Map<Object,String> getActiveInstances(Class<S> scope)
      Returns all active CDI managed bean instances in the given CDI managed bean scope. The map key represents the active CDI managed bean instance and the map value represents the CDI managed bean name, if any.
      Type Parameters:
      S - The generic CDI managed bean scope type.
      Parameters:
      scope - The CDI managed bean scope, e.g. RequestScoped.class.
      Returns:
      All active CDI managed bean instances in the given CDI managed bean scope.
      Since:
      1.7
      See Also:
    • destroy

      public static <T> void destroy(Class<T> beanClass, Annotation... qualifiers)
      Destroy the currently active instance of the given CDI managed bean class, optionally with the given qualifiers.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      beanClass - The CDI managed bean class.
      qualifiers - The CDI managed bean qualifiers, if any.
      Since:
      2.0
      See Also:
    • destroy

      public static <T> void destroy(Bean<T> bean)
      Destroy the currently active instance of the given CDI managed bean representation.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      bean - The CDI managed bean representation.
      Throws:
      IllegalArgumentException - When the given CDI managed bean type is actually not put in an alterable context.
      Since:
      2.0
      See Also:
    • destroy

      public static <T> void destroy(T instance)
      Destroy the currently active instance of the given CDI managed bean instance.
      Type Parameters:
      T - The generic CDI managed bean type.
      Parameters:
      instance - The CDI managed bean instance.
      Throws:
      IllegalArgumentException - When the given CDI managed bean type is actually not put in an alterable context.
      Since:
      2.5
      See Also:
    • getAnnotation

      public static <A extends Annotation> A getAnnotation(Annotated annotated, Class<A> annotationType)
      Get program element annotation of a certain annotation type. The difference with Annotated.getAnnotation(Class) is that this method will recursively search inside all Stereotype annotations.
      Type Parameters:
      A - The generic annotation type.
      Parameters:
      annotated - A Java program element that can be annotated.
      annotationType - The class of the annotation type.
      Returns:
      The program element annotation of the given annotation type if it could be found, otherwise null.
      Since:
      1.8
    • getCurrentInjectionPoint

      public static InjectionPoint getCurrentInjectionPoint(CreationalContext<?> creationalContext)
      Gets the current injection point when called from a context where injection is taking place (e.g. from a producer).

      This is mostly intended to be used from within a dynamic producer Bean. For a "regular" producer (using Produces) an InjectionPoint can either be injected into the bean that contains the producer method, or directly provided as argument of said method.

      Parameters:
      creationalContext - a CreationalContext used to manage objects with a Dependent scope
      Returns:
      the current injection point when called from a context where injection is taking place (e.g. from a producer)
    • getQualifier

      public static <A extends Annotation> A getQualifier(InjectionPoint injectionPoint, Class<A> qualifierClass)
      Returns the qualifier annotation of the given qualifier class from the given injection point.
      Type Parameters:
      A - The generic annotation type.
      Parameters:
      injectionPoint - The injection point to obtain the qualifier annotation of the given qualifier class from.
      qualifierClass - The class of the qualifier annotation to be looked up in the given injection point.
      Returns:
      The qualifier annotation of the given qualifier class from the given injection point.
      Since:
      2.1
    • fireEvent

      public static void fireEvent(Object event, Annotation... qualifiers)
      Fires the given CDI event, optionally with the given qualifiers.
      Parameters:
      event - The event object.
      qualifiers - The event qualifiers, if any.
      Since:
      2.3
      See Also: