Refine search
@Override @SuppressWarnings("unchecked") public Object getInstance(ForeignDescriptor foreignDescriptor) { Bean bean = (Bean) foreignDescriptor.get(); CreationalContext ctx = beanManager.createCreationalContext(bean); return bean.create(ctx); }
@Test(groups = { "contexts", "producerMethod" }) @SpecAssertion(section = "6.4", id = "da") public void testInstanceUsedForProducerMethodNotShared() throws Exception { Bean<Tarantula> tarantulaBean = getBeans(Tarantula.class, PET_LITERAL).iterator().next(); CreationalContext<Tarantula> creationalContext = getCurrentManager().createCreationalContext(tarantulaBean); Tarantula tarantula = tarantulaBean.create(creationalContext); Tarantula tarantula2 = tarantulaBean.create(creationalContext); assert tarantula != null; assert tarantula2 != null; assert tarantula != tarantula2; }
@Test @SpecAssertion(section = DEPENDENT_CONTEXT, id = "g") // Dependent context is now always active public void testContextIsActiveDuringInjection() { Bean<FoxRun> foxRunBean = getBeans(FoxRun.class).iterator().next(); FoxRun foxRun = foxRunBean.create(new MockCreationalContext<FoxRun>()); assert foxRun.fox != null; }
@Test(groups = { "contexts", "producerMethod" }) @SpecAssertion(section = "6.4", id = "db") public void testInstanceUsedForProducerFieldNotShared() throws Exception { Bean<Tarantula> tarantulaBean = getBeans(Tarantula.class, TAME_LITERAL).iterator().next(); CreationalContext<Tarantula> creationalContext = getCurrentManager().createCreationalContext(tarantulaBean); Tarantula tarantula = tarantulaBean.create(creationalContext); Tarantula tarantula2 = tarantulaBean.create(creationalContext); assert tarantula != null; assert tarantula2 != null; assert tarantula != tarantula2; }
@Test(groups = { "contexts", "injection" }) @SpecAssertion(section = "6.4", id = "g") // Dependent context is now always active public void testContextIsActiveDuringInjection() { Bean<FoxRun> foxRunBean = getBeans(FoxRun.class).iterator().next(); FoxRun foxRun = foxRunBean.create(new MockCreationalContext<FoxRun>()); assert foxRun.fox != null; }
this.creationalContext = beanManager.createCreationalContext( null ); Set<Bean<?>> beans = beanManager.getBeans( beanType, new NamedBeanQualifier( beanName ) ); this.bean = (Bean<B>) beanManager.resolve( beans ); this.beanInstance = bean.create( creationalContext );
@Test(groups = { "injection", "producerMethod" }) @SpecAssertion(section = "2.3.5", id = "a") public void testFieldInjectedFromProducerMethod() throws Exception { Bean<Barn> barnBean = getBeans(Barn.class).iterator().next(); Barn barn = barnBean.create(getCurrentManager().createCreationalContext(barnBean)); assert barn.petSpider != null; assert barn.petSpider instanceof DefangedTarantula; }
@Override public Object resolve(Injectee injectee) { Injectee newInjectee = injectee; if (injectee.isFactory()) { newInjectee = getFactoryInjectee(injectee, ReflectionHelper.getTypeArgument(injectee.getRequiredType(), 0)); } Bean<?> bean = descriptorCache.apply(newInjectee.getRequiredType()); if (bean != null) { CreationalContext ctx = beanManager.get().createCreationalContext(bean); Object result = bean.create(ctx); if (injectee.isFactory()) { return (Supplier<Object>) () -> result; } else { return result; } } return null; }
@Test(groups = { "enterpriseBeans", "lifecycle", "integration" }) @SpecAssertions({ @SpecAssertion(section = "7.3.3", id = "b")}) public void testCreateSLSB() { Bean<NeueStadt> stadtBean = getBeans(NeueStadt.class).iterator().next(); assert stadtBean != null : "Expected a bean for stateful session bean Kassel"; CreationalContext<NeueStadt> creationalContext = getCurrentManager().createCreationalContext(stadtBean); NeueStadt stadtInstance = stadtBean.create(creationalContext); assert stadtInstance != null : "Expected instance to be created by container"; // Verify that the instance returned is a proxy by checking for all local interfaces Set<Class> interfaces = new HashSet<Class>(Arrays.asList(stadtInstance.getClass().getInterfaces())); assert interfaces.contains(NeueStadt.class); assert interfaces.contains(GeschichtslosStadt.class); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { T instance = bean.create(beanManager.createCreationalContext(bean)); return method.invoke(instance, args); } }
@Test(groups = { "producerMethod" }, expectedExceptions = FooException.class) @SpecAssertions({ @SpecAssertion(section = "6.1", id = "a0") }) public void testCreateRethrowsUncheckedException() { Bean<Ship> shipBean = getBeans(Ship.class, FAIL_LITERAL).iterator().next(); CreationalContext<Ship> shipCc = getCurrentManager().createCreationalContext(shipBean); shipBean.create(shipCc); assert false; }
@Inject BeanManager manager; //inject the bean manager into your class public static MyExample getMyExample(String myExampleType){ Set<Bean<?>> beans = manager.getBeans("example1"); //retrieve the bean by name. This returns a set, but you're ideally interested in only one bean Bean bean = beans.iterator().next(); //get the first (and ideally the only bean in the set) CreationalContext ctx = manager.createCreationalContext(bean); //CDI stipulation: there has to be a context associated with everything. MyExample theExample = bean.create(ctx); //create an instance of the bean and return return theExample; }
@Test(expectedExceptions = FooException.class) @SpecAssertions({ @SpecAssertion(section = CONTEXTUAL, id = "aa") }) public void testCreateRethrowsUncheckedException() { Bean<Ship> shipBean = getBeans(Ship.class, FAIL_LITERAL).iterator().next(); CreationalContext<Ship> shipCc = getCurrentManager().createCreationalContext(shipBean); shipBean.create(shipCc); assert false; }
@SuppressWarnings({ "rawtypes", "unchecked", "serial" }) private Set<ProviderQosFactory> getProviderQosFactories() { Set<Bean<?>> providerQosFactoryBeans = beanManager.getBeans(ProviderQosFactory.class, new AnnotationLiteral<Any>() { }); Set<ProviderQosFactory> providerQosFactories = new HashSet<>(); for (Bean providerQosFactoryBean : providerQosFactoryBeans) { ProviderQosFactory factory = (ProviderQosFactory) providerQosFactoryBean.create(beanManager.createCreationalContext(providerQosFactoryBean)); providerQosFactories.add(factory); } return providerQosFactories; }
@Test(expectedExceptions = CreationException.class) @SpecAssertions({ @SpecAssertion(section = CONTEXTUAL, id = "aa") }) public void testCreateWrapsCheckedExceptionAndRethrows() { Bean<Lorry> lorryBean = getBeans(Lorry.class, FAIL_LITERAL).iterator().next(); CreationalContext<Lorry> lorryCc = getCurrentManager().createCreationalContext(lorryBean); lorryBean.create(lorryCc); assert false; } }
@SuppressWarnings("unchecked") private MetadataCache getMetadataCache() { if (metadataCache == null) { Bean<MetadataCache> bean = (Bean<MetadataCache>) beanManager.getBeans( MetadataCache.class).iterator().next(); metadataCache = bean.create(beanManager.createCreationalContext(bean)); } return metadataCache; }
@Test(groups = { "producerMethod" }) @SpecAssertions({ @SpecAssertion(section = "7.3.4", id = "k") }) public void testCreateReturnsNullIfProducerDoesAndDependent() { Bean<Spider> nullSpiderBean = getBeans(Spider.class, NULL_LITERAL).iterator().next(); CreationalContext<Spider> nullSpiderBeanCc = getCurrentManager().createCreationalContext(nullSpiderBean); assert nullSpiderBean.create(nullSpiderBeanCc) == null; }
@SuppressWarnings("unchecked") private MetadataCache getMetadataCache() { if (metadataCache == null) { Bean<MetadataCache> bean = (Bean<MetadataCache>) beanManager.getBeans( MetadataCache.class).iterator().next(); metadataCache = bean.create(beanManager.createCreationalContext(bean)); } return metadataCache; }
@Test(groups = { "producerMethod" }, expectedExceptions = CreationException.class) @SpecAssertions({ @SpecAssertion(section = "6.1", id = "a0") }) public void testCreateWrapsCheckedExceptionAndRethrows() { Bean<Lorry> lorryBean = getBeans(Lorry.class, FAIL_LITERAL).iterator().next(); CreationalContext<Lorry> lorryCc = getCurrentManager().createCreationalContext(lorryBean); lorryBean.create(lorryCc); assert false; } }
@Override public ScopedInstance<?> apply(final Class<?> ignored) { logger.info(m("create(%s)", beanClass.getName())); if (creationalContext == null) { final CreationalContext<T> context = beanManager.createCreationalContext(bean); return new ScopedInstance<T>(bean, context, bean.create(context)); } return new ScopedInstance<T>(bean, creationalContext, bean.create(creationalContext)); } });