Package be.atbash.util
Class CDIUtils
- java.lang.Object
-
- be.atbash.util.CDIUtils
-
public final class CDIUtils extends Object
Utility methods usable in a CDI 1.1 environment (Java EE 7+)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidfireEvent(Object event, Annotation... qualifiers)Fire an event and notify observers.static jakarta.enterprise.inject.spi.BeanManagergetBeanManager()Returns theBeanManagerwhich is basically a shorthand forCDI.current().getBeanManager().static voidregisterProducerMethod(Method producerMethod)Keeps track of all the CDI Bean producer methods.static <T> TretrieveInstance(Class<T> classType, Annotation... qualifiers)Retrieve the single CDI instance which has the classType in the bean definition.static <T> TretrieveInstanceByName(String beanName, Class<T> targetClass)Retrieve the CDI bean assigned the bean name in the context.static <T> List<T>retrieveInstances(Class<T> classType, Annotation... qualifiers)Retrieve all CDI instances which have the classType in their bean definition.static <T> TretrieveOptionalInstance(Class<T> classType, Annotation... qualifiers)Retrieve the Single CDI instance which has the classType in the bean definition or null when no CDI beans found.
-
-
-
Method Detail
-
retrieveInstances
public static <T> List<T> retrieveInstances(Class<T> classType, Annotation... qualifiers)
Retrieve all CDI instances which have the classType in their bean definition. The list is an unmodifiable version.- Type Parameters:
T- Generic Type argument- Parameters:
classType- aClassrepresenting the required type.qualifiers- the additional required qualifiers.- Returns:
- List of all CDI instances matching the class type and qualifiers (if specified) or empty list if no beans are found.
-
retrieveInstance
public static <T> T retrieveInstance(Class<T> classType, Annotation... qualifiers)
Retrieve the single CDI instance which has the classType in the bean definition. It throws the standard CDI exceptions in case when there are no or multiple beans which are a candidate for the type.- Type Parameters:
T- Generic Type argument- Parameters:
classType- aClassrepresenting the required typequalifiers- the additional required qualifiers.- Returns:
- CDI instance matching the class type and qualifiers (if specified).
- Throws:
jakarta.enterprise.inject.AmbiguousResolutionException- When more than 1 bean is found in the matchjakarta.enterprise.inject.UnsatisfiedResolutionException- When no bean is found in the match.
-
retrieveOptionalInstance
public static <T> T retrieveOptionalInstance(Class<T> classType, Annotation... qualifiers)
Retrieve the Single CDI instance which has the classType in the bean definition or null when no CDI beans found. When there are multiple matching beans, the standard CDI AmbiguousResolutionException is thrown. Method also tries to find the bean when created by a Producer method and bean has generic type information.- Type Parameters:
T- Generic Type argument- Parameters:
classType- aClassrepresenting the required typequalifiers- the additional required qualifiers.- Returns:
- CDI instance matching the class type and qualifiers (if specified) or null when no bean found.
- Throws:
jakarta.enterprise.inject.AmbiguousResolutionException- When more then 1 bean is found in the match
-
getBeanManager
public static jakarta.enterprise.inject.spi.BeanManager getBeanManager()
Returns theBeanManagerwhich is basically a shorthand forCDI.current().getBeanManager().- Returns:
- The
BeanManagerfor the current CDI context.
-
retrieveInstanceByName
public static <T> T retrieveInstanceByName(String beanName, Class<T> targetClass)
Retrieve the CDI bean assigned the bean name in the context. The bean must also be assignable to the targetClass class. When the retrieved bean is not assignable,UnsatisfiedResolutionExceptionis thrown (instead of theClassCastExceptionby the JVM). When no bean is found,UnsatisfiedResolutionExceptionis also thrown.- Type Parameters:
T- Generic Type argument- Parameters:
beanName- The name of the bean we are interested in.targetClass- The class this bean must have (or subclass)- Returns:
- The CDI bean or exception when an issue in retrieving the bean.
- Throws:
jakarta.enterprise.inject.UnsatisfiedResolutionException- When instance cannot be found or is assignable to the requested type.
-
fireEvent
public static void fireEvent(Object event, Annotation... qualifiers)
Fire an event and notify observers. A shorthand for writingCDI.current().getBeanManager().fireEvent.- Parameters:
event- the event objectqualifiers- the event qualifiers
-
registerProducerMethod
public static void registerProducerMethod(Method producerMethod)
Keeps track of all the CDI Bean producer methods.- Parameters:
producerMethod- Method which produces a CDI bean (@Produces present)
-
-