/** * Runs CDI injection on the instance. This should be called after resource injection has been performed */ public void inject(Object instance) { injectionTarget.inject(instance, context); }
/** * Inject the instance * * @throws IllegalStateException if inject() is called before produce() is called * @throws IllegalStateException if inject() is called on an instance that has already been disposed * @return self */ public UnmanagedInstance<T> inject() { if (instance == null) { throw new IllegalStateException("Trying to call inject() before produce() was called"); } if (disposed) { throw new IllegalStateException("Trying to call inject() on already disposed instance"); } injectionTarget.inject(instance, ctx); return this; }
@Override public T create(CreationalContext<T> context) { T service = binding.getService(); this.injectionTarget.inject(service, context); return service; }
@Override public void inject(final T instance, final CreationalContext<T> ctx) { delegate().inject(instance, ctx); }
public void injectInterceptor(Object instance) { final InjectionTarget injection = interceptorInjections.get(instance.getClass()); if (injection != null) { injection.inject(instance, context); } else { throw WeldLogger.ROOT_LOGGER.unknownInterceptorClassForCDIInjection(instance.getClass()); } }
@Override @SuppressWarnings("unchecked") public void inject(Object instance) { if (beanManager != null) { AnnotatedType annotatedType = beanManager.createAnnotatedType((Class) instance.getClass()); InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType); CreationalContext context = beanManager.createCreationalContext(null); injectionTarget.inject(instance, context); } }
@Override public ManagedReference createInstance(final Object instance) { final BeanManager beanManager = beanManagerSupplier.get(); final InjectionTarget injectionTarget = getInjectionTarget(instance.getClass()); final CreationalContext context = beanManager.createCreationalContext(null); injectionTarget.inject(instance, context); injectionTarget.postConstruct(instance); return new WeldManagedReference(injectionTarget, context, instance); }
@Override public CdiExternalRequestScope create(CreationalContext<CdiExternalRequestScope> ctx) { final CdiExternalRequestScope result = interceptorTarget.produce(ctx); interceptorTarget.inject(result, ctx); interceptorTarget.postConstruct(result); return result; }
@Override public void inject(final Object t, final CreationalContext cc) { delegate.inject(t, cc); InjectionManager injectingManager = getEffectiveInjectionManager(); if (injectingManager == null) { injectingManager = effectiveInjectionManager; } if (injectingManager != null) { injectingManager.inject(t, CdiComponentProvider.CDI_CLASS_ANALYZER); } }
@Override @SuppressWarnings("unchecked") public T create(CreationalContext<T> context) { T instance = injectionTarget.produce(context); injectionTarget.inject(instance, context); injectionTarget.postConstruct(instance); return instance; }
@Override public ValidationInterceptor create(CreationalContext<ValidationInterceptor> ctx) { final ValidationInterceptor result = interceptorTarget.produce(ctx); interceptorTarget.inject(result, ctx); interceptorTarget.postConstruct(result); return result; }
@Override public Supplier<T> create(CreationalContext<Supplier<T>> context) { Supplier<T> instance = injectionTarget.produce(context); injectionTarget.inject(instance, context); injectionTarget.postConstruct(instance); return instance; }
@Override @SuppressWarnings("unchecked") public CdiRequestScope create(CreationalContext<CdiRequestScope> context) { CdiRequestScope instance = injectionTarget.produce(context); injectionTarget.inject(instance, context); injectionTarget.postConstruct(instance); return instance; }
@Override public T getInstance(final Class<T> clazz) { final CreationalContext<T> creationalContext = beanManager.createCreationalContext(null); final T instance = injectionTarget.produce(creationalContext); injectionTarget.inject(instance, creationalContext); if (injectionManager != null) { injectionManager.inject(instance, CdiComponentProvider.CDI_CLASS_ANALYZER); } injectionTarget.postConstruct(instance); return instance; }
@Override public ManagedReference getReference() { final CreationalContext context = beanManager.createCreationalContext(null); final Object instance = injectionTarget.produce(context); injectionTarget.inject(instance, context); injectionTarget.postConstruct(instance); return new WeldManagedReference(injectionTarget, context, instance); } };
@SuppressWarnings("unchecked") private static void doCallInject(BeanManagerImpl beanManager, AbstractClassBean bean, Object instance) { // In whatever reason, we have to use reflection call for beanManager.createCreationalContext() in weld>=3.0 Method m = null; try { m = beanManager.getClass().getMethod("createCreationalContext", Contextual.class); } catch (Exception e) { LOGGER.error("BeanManager.createCreationalContext() method not found beanManagerClass='{}'", e, bean.getBeanClass().getName()); return; } try { bean.getProducer().inject(instance, (CreationalContext) m.invoke(beanManager, bean)); LOGGER.debug("Bean instance '{}' injection points was reinjected.", instance); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LOGGER.error("beanManager.createCreationalContext(beanManager, bean) invocation failed beanManagerClass='{}', beanClass='{}'", e, bean.getBeanClass().getName(), bean.getClass().getName()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectBeanInstance(BeanManagerImpl beanManager, InjectionTargetBean bean, Context context) { Object instance = context.get(bean); if (instance != null) { bean.getProducer().inject(instance, beanManager.createCreationalContext(bean)); LOGGER.info("Bean '{}' injection points was reinjected.", bean.getBeanClass().getName()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectRegisteredBeanInstances(BeanManagerImpl beanManager, InjectionTargetBean bean) { for (Object instance: HaCdiCommons.getBeanInstances(bean)) { if (instance != null) { bean.getProducer().inject(instance, beanManager.createCreationalContext(bean)); LOGGER.info("Bean '{}' injection points was reinjected.", bean.getBeanClass().getName()); } else { LOGGER.info("Unexpected 'null' bean instance in registry. bean='{}'", bean.getBeanClass().getName()); } } }
/** * Will re-inject any managed beans in the target. Will not call any other life-cycle methods * * @param ctx * @param managedBean */ public static void reinitialize(Context ctx, Contextual<Object> contextual) { try { ManagedBean<Object> managedBean = ManagedBean.class.cast(contextual); LOGGER.debug("Re-Initializing........ {},: {}", managedBean, ctx); Object get = ctx.get(managedBean); if (get != null) { LOGGER.debug("Bean injection points are reinitialized '{}'", managedBean); managedBean.getProducer().inject(get, managedBean.getBeanManager().createCreationalContext(managedBean)); } } catch (Exception e) { LOGGER.error("Error reinitializing bean {},: {}", e, contextual, ctx); } } }
injectionTarget.inject( beanInstance, creationalContext );