@Override public <T> T cdiAnno(Class<T> clazz, Annotation... annotations) { Set<Bean<?>> beans = this.bm.getBeans(clazz, annotations); // We'll use resolve to get a single instance (e.g. we might have @Alternative beans and getBeans will include them Bean<?> bean = this.bm.resolve(beans); return ServiceLocatorUtils.toInstance(this.bm, bean, clazz); }
@Override public <T> T cdiAnno(Class<T> clazz, Annotation... annotations) { Set<Bean<?>> beans = this.bm.getBeans(clazz, annotations); // We'll use resolve to get a single instance (e.g. we might have @Alternative beans and getBeans will include them Bean<?> bean = this.bm.resolve(beans); return ServiceLocatorUtils.toInstance(this.bm, bean, clazz); }
/** * Helper method to convert a set of Beans to actual instance. * @param bm BeanManager that was used to retrieve the Beans * @param beans Set of beans to convert * @param clazz Type to convert to * @param count If not null, we'll check if we have exactly count results and throw a JuRuntimeException if not * @return List of instances */ public static <T> List<T> toInstances(BeanManager bm, Set<Bean<?>> beans, Class<T> clazz, Integer count) { List<T> instances = new ArrayList<>(); for (Bean<?> bean : beans) { instances.add(toInstance(bm, bean, clazz)); } if (count != null) { if (instances.size() != count) { throw new JuRuntimeException("Expected exactly %d result(s) for CDI lookup of %s, but found %d", count, clazz, instances.size()); } } return instances; }
/** * Helper method to convert a set of Beans to actual instance. * @param bm BeanManager that was used to retrieve the Beans * @param beans Set of beans to convert * @param clazz Type to convert to * @param count If not null, we'll check if we have exactly count results and throw a JuRuntimeException if not * @return List of instances */ public static <T> List<T> toInstances(BeanManager bm, Set<Bean<?>> beans, Class<T> clazz, Integer count) { List<T> instances = new ArrayList<>(); for (Bean<?> bean : beans) { instances.add(toInstance(bm, bean, clazz)); } if (count != null) { if (instances.size() != count) { throw new JuRuntimeException("Expected exactly %d result(s) for CDI lookup of %s, but found %d", count, clazz, instances.size()); } } return instances; }