Class 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

    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);
     
    Since:
    1.6.1
    Author:
    Bauke Scholtz
    • Method Summary

      All Methods Static Methods Concrete Methods 
      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>
      getActiveInstances​(Class<S> scope)
      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.
      static InjectionPoint getCurrentInjectionPoint​(CreationalContext<?> creationalContext)
      Gets the current injection point when called from a context where injection is taking place (e.g.
      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 getInstance​(String name)
      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.
      static BeanManager getManager()
      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 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).
    • Method Detail

      • getManager

        public static BeanManager getManager()
        Returns the CDI bean manager.
        Returns:
        The CDI bean manager.
        Since:
        2.0
        See Also:
        CDI.getBeanManager()
      • 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:
        BeanManager.getBeans(java.lang.reflect.Type, Annotation...), BeanManager.resolve(java.util.Set)
      • 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:
        BeanManager.getBeans(java.lang.reflect.Type, Annotation...), BeanManager.resolve(java.util.Set)
      • 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:
        resolve(Class, Annotation...), getReference(Bean), resolve(Class, Annotation...)
      • 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(Class, boolean, Annotation...)
      • 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:
        resolve(Class, Annotation...), getInstance(Bean, boolean)
      • 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:
        BeanManager.getContext(Class), Context.isActive()
      • 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