private <T> ProducerFactory<T> factory(final Object mock) { return new ProducerFactory<T>() { @Override public <T1> Producer<T1> createProducer(final Bean<T1> bean) { return new Producer<T1>() { @Override public T1 produce(final CreationalContext<T1> ctx) { return (T1) mock; } @Override public void dispose(final T1 instance) { } @Override public Set<InjectionPoint> getInjectionPoints() { return Collections.emptySet(); } }; } }; }
@Test public void testProducer() { logger.info("starting producer spi test"); CDI<Object> cdi = current(); BeanManager beanManager = cdi.select(BeanManager.class).get(); AnnotatedField<? super AnotherFactory> field = this .<AnotherFactory, AnnotatedField<AnotherFactory>>getAnnotatedMember(AnotherFactory.class, "jessie", beanManager); Bean<AnotherFactory> declaringBean = cast(beanManager.resolve(beanManager.getBeans(AnotherFactory.class))); ProducerFactory<AnotherFactory> producerFactory = beanManager.getProducerFactory(field, declaringBean); Producer<Toy> producer = cast(producerFactory.createProducer(null)); assertNotNull(producer); assertTrue(producer.getInjectionPoints().isEmpty()); Toy jessie = producer.produce(beanManager.<Toy>createCreationalContext(null)); assertEquals("Jessie", jessie.getName()); }
<T> void producerFactory(@Observes ProducerFactory<T> pf) { logger.info("finished the scanning process"); producerFactory++; }
@Override public <X> ProducerFactory<X> getProducerFactory(AnnotatedField<? super X> field, Bean<X> declaringBean) { // TODO Auto-generated method stub return null; }
@Override public <X> ProducerFactory<X> getProducerFactory(AnnotatedMethod<? super X> method, Bean<X> declaringBean) { // TODO Auto-generated method stub return null; }
@Override public <T, X> Bean<T> createBean(BeanAttributes<T> attributes, Class<X> beanClass, ProducerFactory<X> producerFactory) { // TODO Auto-generated method stub return null; }
@Override public <T, X> Bean<T> createBean(final BeanAttributes<T> arg0, final Class<X> arg1, final ProducerFactory<X> arg2) { return this.beanManager.createBean(arg0, arg1, arg2); }
@Override public <X> ProducerFactory<X> getProducerFactory( final AnnotatedField<? super X> arg0, final Bean<X> arg1) { return this.beanManager.getProducerFactory(arg0, arg1); }
@Override public <X> ProducerFactory<X> getProducerFactory( final AnnotatedMethod<? super X> arg0, final Bean<X> arg1) { return this.beanManager.getProducerFactory(arg0, arg1); }