@Override public <T> T cdiNamed(Class<T> clazz, final String name) { Set<Bean<?>> beans = this.bm.getBeans(clazz, ServiceLocatorUtils.createNamedAnnotation(name)); return ServiceLocatorUtils.toInstances(this.bm, beans, clazz, 1).get(0); }
/** * Bean is annotated with @ScopeControl annotation * @return */ public CdiComplexLookupBuilder<T> scopeControl() { this.annotations.add(ServiceLocatorUtils.createScopeControlAnnotation()); return this; }
@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> List<T> cdiAllAnno(Class<T> clazz, Annotation... annotations) { Set<Bean<?>> beans = this.bm.getBeans(clazz, annotations); return ServiceLocatorUtils.toInstances(this.bm, beans, clazz, null); }
/** * Bean is annotated with @Named annotation * @param name Value of the @Named annotation * @return */ public CdiComplexLookupBuilder<T> named(String name) { this.annotations.add(ServiceLocatorUtils.createNamedAnnotation(name)); return this; }
@Override public <T> List<T> cdiAllAnno(Class<T> clazz, Annotation... annotations) { Set<Bean<?>> beans = this.bm.getBeans(clazz, annotations); return ServiceLocatorUtils.toInstances(this.bm, beans, clazz, null); }
/** * Bean is annotated with @Named annotation * @param name Value of the @Named annotation * @return */ public CdiComplexLookupBuilder<T> named(String name) { this.annotations.add(ServiceLocatorUtils.createNamedAnnotation(name)); return this; }
@Override public <T> T cdiNamed(Class<T> clazz, final String name) { Set<Bean<?>> beans = this.bm.getBeans(clazz, ServiceLocatorUtils.createNamedAnnotation(name)); return ServiceLocatorUtils.toInstances(this.bm, beans, clazz, 1).get(0); }
@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); }
/** * Returns a FindHelper instance to query the result set of CDI objects * found by our settings. * @return FindHelper instance */ public FindHelper<T> find() { Set<Bean<?>> beans = this.bm.getBeans(this.expectedType, this.annotations.toArray(new Annotation[0])); List<T> instances = ServiceLocatorUtils.toInstances(this.bm, beans, this.expectedType, null); return new FindHelperBuilder<T>() .collection(instances) .createFindHelper(); } }
/** * Bean is annotated with @ScopeControl annotation * @return */ public CdiComplexLookupBuilder<T> scopeControl() { this.annotations.add(ServiceLocatorUtils.createScopeControlAnnotation()); return this; }
/** * 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; }
/** * Returns a FindHelper instance to query the result set of CDI objects * found by our settings. * @return FindHelper instance */ public FindHelper<T> find() { Set<Bean<?>> beans = this.bm.getBeans(this.expectedType, this.annotations.toArray(new Annotation[0])); List<T> instances = ServiceLocatorUtils.toInstances(this.bm, beans, this.expectedType, null); return new FindHelperBuilder<T>() .collection(instances) .createFindHelper(); } }
/** * 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; }