Class CDIUtils


  • public final class CDIUtils
    extends Object
    Utility methods usable in a CDI 1.1 environment (Java EE 7+)
    • 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 - a Class representing 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 - a Class representing the required type
        qualifiers - 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 match
        jakarta.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 - a Class representing the required type
        qualifiers - 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 the BeanManager which is basically a shorthand for CDI.current().getBeanManager().
        Returns:
        The BeanManager for 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, UnsatisfiedResolutionException is thrown (instead of the ClassCastException by the JVM). When no bean is found, UnsatisfiedResolutionException is 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 writing CDI.current().getBeanManager().fireEvent.
        Parameters:
        event - the event object
        qualifiers - 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)