- 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
-
-
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>
AgetAnnotation(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 andcreate
argument istrue
, otherwise don't create one and returnnull
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 andcreate
argument istrue
, otherwise don't create one and returnnull
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 andcreate
argument istrue
, otherwise don't create one and returnnull
if there's no current instance.static BeanManager
getManager()
Returns the CDI bean manager.static <A extends Annotation>
AgetQualifier(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>
booleanisActive(Class<S> scope)
Returnstrue
when the given CDI managed bean scope is active.static <T> boolean
isProxy(T object)
Returnstrue
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 perisProxy(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...)
-
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:
BeanManager.createCreationalContext(jakarta.enterprise.context.spi.Contextual)
,BeanManager.getReference(Bean, java.lang.reflect.Type, jakarta.enterprise.context.spi.CreationalContext)
-
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 andcreate
argument istrue
, otherwise don't create one and returnnull
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 thecreate
argument isfalse
. - Since:
- 1.7
- See Also:
resolve(Class, Annotation...)
,getInstance(Bean, boolean)
-
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 andcreate
argument istrue
, otherwise don't create one and returnnull
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 thecreate
argument isfalse
. - Since:
- 1.7
- See Also:
BeanManager.getContext(Class)
,BeanManager.createCreationalContext(jakarta.enterprise.context.spi.Contextual)
,Context.get(jakarta.enterprise.context.spi.Contextual, jakarta.enterprise.context.spi.CreationalContext)
-
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 andcreate
argument istrue
, otherwise don't create one and returnnull
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 thecreate
argument isfalse
. - Since:
- 3.14
- See Also:
BeanManager.getBeans(String)
,BeanManager.resolve(java.util.Set)
,BeanManager.getContext(Class)
,BeanManager.createCreationalContext(jakarta.enterprise.context.spi.Contextual)
,Context.get(jakarta.enterprise.context.spi.Contextual, jakarta.enterprise.context.spi.CreationalContext)
-
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:
BeanManager.getBeans(String)
,BeanManager.resolve(java.util.Set)
,BeanManager.getContext(Class)
,BeanManager.createCreationalContext(jakarta.enterprise.context.spi.Contextual)
,Context.get(jakarta.enterprise.context.spi.Contextual, jakarta.enterprise.context.spi.CreationalContext)
-
isProxy
public static <T> boolean isProxy(T object)
Returnstrue
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 perisProxy(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)
Returnstrue
when the given CDI managed bean scope is active. I.e., all beans therein can be accessed without facingContextNotActiveException
.- 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()
-
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:
BeanManager.getBeans(java.lang.reflect.Type, Annotation...)
,BeanManager.getContext(Class)
,Context.get(jakarta.enterprise.context.spi.Contextual)
-
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:
resolve(Class, Annotation...)
,BeanManager.getContext(Class)
,AlterableContext.destroy(jakarta.enterprise.context.spi.Contextual)
-
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:
BeanManager.getContext(Class)
,AlterableContext.destroy(jakarta.enterprise.context.spi.Contextual)
-
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:
resolve(Class, Annotation...)
,BeanManager.createCreationalContext(Contextual)
,Contextual.destroy(Object, CreationalContext)
-
getAnnotation
public static <A extends Annotation> A getAnnotation(Annotated annotated, Class<A> annotationType)
Get program element annotation of a certain annotation type. The difference withAnnotated.getAnnotation(Class)
is that this method will recursively search inside allStereotype
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 (usingProduces
) anInjectionPoint
can either be injected into the bean that contains the producer method, or directly provided as argument of said method.- Parameters:
creationalContext
- aCreationalContext
used to manage objects with aDependent
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:
BeanManager.getEvent()
,Event.select(Annotation...)
,Event.fire(Object)
-
-