javax.enterprise.inject.spi.InjectionTarget Java Examples

The following examples show how to use javax.enterprise.inject.spi.InjectionTarget. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CdiEjbBean.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public InjectionTarget<T> createInjectionTarget(final Bean<T> bean) {
    final EjbInjectionTargetImpl<T> injectionTarget = new EjbInjectionTargetImpl<>(getAnnotatedType(), createInjectionPoints(bean), getWebBeansContext());
    final InjectionTarget<T> it = getWebBeansContext().getWebBeansUtil().fireProcessInjectionTargetEvent(injectionTarget, getAnnotatedType()).getInjectionTarget();

    for (final InjectionPoint ip : it.getInjectionPoints()) {
        if (ip.getType() != UserTransaction.class) {
            continue;
        }
        if (beanContext.getTransactionType() != TransactionType.BeanManaged) {
            throw new DefinitionException("@Inject UserTransaction is only valid for BeanManaged beans");
        }
    }

    if (!EjbInjectionTargetImpl.class.isInstance(it)) {
        return new EjbInjectionTargetImpl<>(injectionTarget, it);
    }
    return it;
}
 
Example #2
Source File: ValidatorBuilder.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static <T> T newInstance(final OpenEjbConfig config, final Class<T> clazz) throws Exception {
    final WebBeansContext webBeansContext = AppFinder.findAppContextOrWeb(
            Thread.currentThread().getContextClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE);
    if (webBeansContext == null) {
        return clazz.newInstance();
    }

    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    if (!beanManager.isInUse()) {
        return clazz.newInstance();
    }

    final AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
    final InjectionTarget<T> it = beanManager.createInjectionTarget(annotatedType);
    final CreationalContext<T> context = beanManager.createCreationalContext(null);
    final T instance = it.produce(context);
    it.inject(instance, context);
    it.postConstruct(instance);

    config.releasables.add(new Releasable<>(context, it, instance));

    return instance;
}
 
Example #3
Source File: BeanProvider.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Performs dependency injection on an instance. Useful for instances which aren't managed by CDI.
 * <p/>
 * <b>Attention:</b><br/>
 * The resulting instance isn't managed by CDI; only fields annotated with @Inject get initialized.
 *
 * @param instance current instance
 * @param <T>      current type
 *
 * @return instance with injected fields (if possible - or null if the given instance is null)
 */
@SuppressWarnings("unchecked")
public static <T> T injectFields(T instance)
{
    if (instance == null)
    {
        return null;
    }

    BeanManager beanManager = getBeanManager();

    CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);

    AnnotatedType<T> annotatedType = beanManager.createAnnotatedType((Class<T>) instance.getClass());
    InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
    injectionTarget.inject(instance, creationalContext);
    return instance;
}
 
Example #4
Source File: CDIInstanceManager.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
@Override
public void newInstance(final Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
    if (WebBeansConfigurationListener.class.isInstance(o) || o.getClass().getName().startsWith("org.apache.catalina.servlets.")) {
        return;
    }

    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(o.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(o, creationalContext);
    try {
        injectionTarget.postConstruct(o);
    } catch (final RuntimeException e) {
        creationalContext.release();
        throw e;
    }
    destroyables.put(o, () -> {
        try {
            injectionTarget.preDestroy(o);
        } finally {
            creationalContext.release();
        }
    });
}
 
Example #5
Source File: MakeJCacheCDIInterceptorFriendly.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
protected void addHelper(final @Observes AfterBeanDiscovery afterBeanDiscovery,
                         final BeanManager bm)
{
    if (!needHelper) {
        return;
    }
    /* CDI >= 1.1 only. Actually we shouldn't go here with CDI 1.1 since we defined the annotated type for the helper
    final AnnotatedType<CDIJCacheHelper> annotatedType = bm.createAnnotatedType(CDIJCacheHelper.class);
    final BeanAttributes<CDIJCacheHelper> beanAttributes = bm.createBeanAttributes(annotatedType);
    final InjectionTarget<CDIJCacheHelper> injectionTarget = bm.createInjectionTarget(annotatedType);
    final Bean<CDIJCacheHelper> bean = bm.createBean(beanAttributes, CDIJCacheHelper.class, new InjectionTargetFactory<CDIJCacheHelper>() {
        @Override
        public InjectionTarget<CDIJCacheHelper> createInjectionTarget(Bean<CDIJCacheHelper> bean) {
            return injectionTarget;
        }
    });
    */
    final AnnotatedType<CDIJCacheHelper> annotatedType = bm.createAnnotatedType(CDIJCacheHelper.class);
    final InjectionTarget<CDIJCacheHelper> injectionTarget = bm.createInjectionTarget(annotatedType);
    final HelperBean bean = new HelperBean(annotatedType, injectionTarget, findIdSuffix());
    afterBeanDiscovery.addBean(bean);
}
 
Example #6
Source File: MakeJCacheCDIInterceptorFriendly.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
public HelperBean(final AnnotatedType<CDIJCacheHelper> annotatedType,
                  final InjectionTarget<CDIJCacheHelper> injectionTarget,
                  final String id) {
    this.at = annotatedType;
    this.it = injectionTarget;
    this.id =  "JCS#CDIHelper#" + id;

    this.qualifiers = new HashSet<>();
    this.qualifiers.add(new AnnotationLiteral<Default>() {

        /**
         * 
         */
        private static final long serialVersionUID = 3314657767813459983L;});
    this.qualifiers.add(new AnnotationLiteral<Any>() {

        /**
         * 
         */
        private static final long serialVersionUID = 7419841275942488170L;});
}
 
Example #7
Source File: BeanBuilder.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * Set the ContextualLifecycle and the InjectionPoints for the AnnotatedType
 * @param type
 */
protected void setDefaultBeanLifecycle(AnnotatedType<T> type)
{
    InjectionTarget<T> injectionTarget;
    if (!type.getJavaClass().isInterface())
    {
        injectionTarget = beanManager.createInjectionTarget(type);
    }
    else
    {
        injectionTarget = new DummyInjectionTarget<T>();
    }
    this.beanLifecycle = new DelegatingContextualLifecycle<T>(injectionTarget);
    this.injectionPoints = injectionTarget.getInjectionPoints();
}
 
Example #8
Source File: WebappBeanManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public <T> InjectionTarget<T> createInjectionTarget(final AnnotatedType<T> type) {
    try {
        return super.createInjectionTarget(type);
    } catch (final RuntimeException e) {
        try {
            return getParentBm().createInjectionTarget(type);
        } catch (final RuntimeException ignored) {
            throw e;
        }
    }
}
 
Example #9
Source File: BeanProvider.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Perform CDI injection on the non-managed object {@code bean}.
 */
public static void injectFields(Object bean) {
    if (instance == null) {
        throw new IllegalStateException("BeanManager is null. Cannot perform injection.");
    }
    
    BeanManager beanManager = instance.getBeanManager();
    CreationalContext creationalContext = beanManager.createCreationalContext(null);
    AnnotatedType annotatedType = beanManager.createAnnotatedType(bean.getClass());
    InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
    // noinspection unchecked
    injectionTarget.inject(bean, creationalContext);
}
 
Example #10
Source File: JAXRSCdiResourceExtension.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void injectBus(@Observes final AfterBeanDiscovery event, final BeanManager beanManager) {
    if (!hasBus) {
        final AnnotatedType< ExtensionManagerBus > busAnnotatedType =
            beanManager.createAnnotatedType(ExtensionManagerBus.class);

        final InjectionTarget<ExtensionManagerBus> busInjectionTarget =
            beanManager.createInjectionTarget(busAnnotatedType);
        event.addBean(new CdiBusBean(busInjectionTarget));
    }

    if (applicationBeans.isEmpty() && !serviceBeans.isEmpty()) {
        final DefaultApplicationBean applicationBean = new DefaultApplicationBean();
        applicationBeans.add(applicationBean);
        event.addBean(applicationBean);
    } else {
        // otherwise will be ambiguous since we scanned it with default qualifier already
        existingStandardClasses.add(Application.class.getName());
    }

    // always add the standard context classes
    InjectionUtils.STANDARD_CONTEXT_CLASSES.stream()
            .map(this::toClass)
            .filter(Objects::nonNull)
            .forEach(contextTypes::add);
    // add custom contexts
    contextTypes.addAll(getCustomContextClasses());
    // register all of the context types
    contextTypes.forEach(
        t -> event.addBean(new ContextProducerBean(t, !existingStandardClasses.contains(t.getTypeName()))));
}
 
Example #11
Source File: MockAwareInjectionTargetWrapper.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public MockAwareInjectionTargetWrapper(BeanManager beanManager,
                                       InjectionTarget<T> wrapped,
                                       List<Type> beanTypes,
                                       List<Annotation> qualifiers)
{
    this.beanManager = beanManager;
    this.wrapped = wrapped;
    this.beanTypes = beanTypes;
    this.qualifiers = qualifiers;
}
 
Example #12
Source File: Injector.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
public static CreationalContext<?> inject(final Object testInstance) {
    if (testInstance == null) {
        return null;
    }
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(testInstance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<?> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(testInstance, creationalContext);
    return creationalContext;
}
 
Example #13
Source File: MeecrowaveRuleBase.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private static CreationalContext<Object> doInject(final Object instance) {
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(instance, creationalContext);
    return creationalContext;
}
 
Example #14
Source File: Meecrowave.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
public <T> AutoCloseable inject(final T instance) {
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(instance, creationalContext);
    return creationalContext::release;
}
 
Example #15
Source File: AbstractWeldInitiator.java    From weld-junit with Apache License 2.0 5 votes vote down vote up
void inject() {
    BeanManager beanManager = container.getBeanManager();
    CreationalContext<Object> ctx = beanManager.createCreationalContext(null);
    @SuppressWarnings("unchecked")
    InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) beanManager
            .getInjectionTargetFactory(beanManager.createAnnotatedType(instance.getClass())).createInjectionTarget(null);
    injectionTarget.inject(instance, ctx);
    creationalContext = ctx;
}
 
Example #16
Source File: CdiResourceInjector.java    From cdi with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void injectResources(Object saga) {
    CreationalContext creationalContext = beanManager.createCreationalContext(null);

    AnnotatedType annotatedType = beanManager.createAnnotatedType(saga.getClass());
    InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
    injectionTarget.inject(saga, creationalContext);
}
 
Example #17
Source File: MockExtension.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
public <X> void onProcessInjectionTarget(@Observes ProcessInjectionTarget<X> processInjectionTarget,
                                         BeanManager beanManager)
{
    if (!isActivated)
    {
        return;
    }

    for (MockFilter mockFilter : mockFilters)
    {
        if (!mockFilter.isMockedImplementationSupported(beanManager, processInjectionTarget.getAnnotatedType()))
        {
            return;
        }
    }

    List<Annotation> qualifiers = new ArrayList<Annotation>();
    for (Annotation annotation : processInjectionTarget.getAnnotatedType().getAnnotations())
    {
        if (beanManager.isQualifier(annotation.annotationType()))
        {
            qualifiers.add(annotation);
        }
    }

    Typed typed = processInjectionTarget.getAnnotatedType().getAnnotation(Typed.class);

    List<Type> foundTypes = new ArrayList<Type>();
    if (typed != null)
    {
        Collections.addAll(foundTypes, typed.value());
    }
    else
    {
        foundTypes.addAll(extractTypes(processInjectionTarget.getAnnotatedType().getJavaClass()));
    }

    if (foundTypes.isEmpty())
    {
        return;
    }

    final InjectionTarget<X> originalInjectionTarget = processInjectionTarget.getInjectionTarget();
    processInjectionTarget.setInjectionTarget(new MockAwareInjectionTargetWrapper<X>(
        beanManager, originalInjectionTarget, foundTypes, qualifiers));
}
 
Example #18
Source File: CdiEjbBean.java    From tomee with Apache License 2.0 4 votes vote down vote up
public EjbInjectionTargetImpl(final EjbInjectionTargetImpl<T> original, final InjectionTarget<T> delegate) {
    super(original.annotatedType, original.getInjectionPoints(), original.webBeansContext, Collections.<AnnotatedMethod<?>>emptyList(), Collections.<AnnotatedMethod<?>>emptyList());
    this.delegate = delegate;
}
 
Example #19
Source File: BeanClassRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void createAnnotatedTypeForExistingBeanClass(BeanManagerImpl beanManager, InjectionTargetBean bean) {

    WebBeansContext wbc = beanManager.getWebBeansContext();

    Object forwardingMethIterceptors = null;

    if (bean.getProducer() instanceof AbstractProducer) {
        // methodInterceptors must be the same instance. It is stored in field owbIntDecHandler of existing
        // InterceptedProxy's instances
        try {
            forwardingMethIterceptors = ReflectionHelper.get(bean.getProducer(), "methodInterceptors");
        } catch (IllegalArgumentException e) {
            LOGGER.warning("Field AbstractProducer.methodInterceptors is not accessible", e);
        }
    }

    AnnotatedElementFactory annotatedElementFactory = wbc.getAnnotatedElementFactory();
    // Clear AnnotatedElementFactory caches
    annotatedElementFactory.clear();

    AnnotatedType annotatedType = annotatedElementFactory.newAnnotatedType(bean.getBeanClass());

    ReflectionHelper.set(bean, InjectionTargetBean.class, "annotatedType", annotatedType);

    // Updated members that were set by bean attributes
    BeanAttributesImpl attributes = BeanAttributesBuilder.forContext(wbc).newBeanAttibutes(annotatedType).build();
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "types", attributes.getTypes());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "qualifiers", attributes.getQualifiers());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "scope", attributes.getScope());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "name", attributes.getName());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "stereotypes", attributes.getStereotypes());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "alternative", attributes.isAlternative());

    InjectionTargetFactory factory = new InjectionTargetFactoryImpl(annotatedType, bean.getWebBeansContext());
    InjectionTarget injectionTarget = factory.createInjectionTarget(bean);
    ReflectionHelper.set(bean, InjectionTargetBean.class, "injectionTarget", injectionTarget);

    if (injectionTarget instanceof AbstractProducer) {
        if (forwardingMethIterceptors != null) {
            ReflectionHelper.set(injectionTarget, AbstractProducer.class, "methodInterceptors", forwardingMethIterceptors);
        }
    }

    LOGGER.debug("New annotated type created for bean '{}'", bean.getBeanClass());
}
 
Example #20
Source File: NewCdiEjbBean.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public InjectionTarget<T> createInjectionTarget(final Bean<T> bean) { // avoid to refire it
    return injectionTarget;
}
 
Example #21
Source File: NewCdiEjbBean.java    From tomee with Apache License 2.0 4 votes vote down vote up
public NewEjbInjectionTargetFactory(final AnnotatedType<T> annotatedType, final WebBeansContext webBeansContext, final InjectionTarget<T> it) {
    super(annotatedType, webBeansContext);
    this.injectionTarget = it;
}
 
Example #22
Source File: ValidatorBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
private Releasable(final CreationalContext<T> context, final InjectionTarget<T> injectionTarget, final T instance) {
    this.context = context;
    this.injectionTarget = injectionTarget;
    this.instance = instance;
}
 
Example #23
Source File: StandardMetricsCollectorBean.java    From hammock with Apache License 2.0 4 votes vote down vote up
StandardMetricsCollectorBean(InjectionTarget<? extends MetricsCollector> injectionTarget, BeanManager beanManager) {
    super(injectionTarget);
    this.beanManager = beanManager;
}
 
Example #24
Source File: MetricCollectorBean.java    From hammock with Apache License 2.0 4 votes vote down vote up
protected MetricCollectorBean(InjectionTarget<? extends MetricsCollector> injectionTarget) {
    this.injectionTarget = injectionTarget;
}
 
Example #25
Source File: NoOpMetricCollectorBean.java    From hammock with Apache License 2.0 4 votes vote down vote up
NoOpMetricCollectorBean(InjectionTarget<? extends MetricsCollector> injectionTarget) {
    super(injectionTarget);
}
 
Example #26
Source File: CdiBusBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
CdiBusBean(final InjectionTarget<ExtensionManagerBus> injectionTarget) {
    this.injectionTarget = injectionTarget;
}
 
Example #27
Source File: DolphinPlatformContextualLifecycle.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
public DolphinPlatformContextualLifecycle(InjectionTarget<T> injectionTarget, PostConstructInterceptor<T> interceptor) {
    this.injectionTarget = Assert.requireNonNull(injectionTarget, "injectionTarget");
    this.interceptor = Assert.requireNonNull(interceptor, "interceptor");
}
 
Example #28
Source File: ConfigurableInjectionTarget.java    From thorntail with Apache License 2.0 4 votes vote down vote up
public ConfigurableInjectionTarget(InjectionTarget<T> delegate, ConfigurableManager configurableManager) {
    this.delegate = delegate;
    this.configurableManager = configurableManager;
}
 
Example #29
Source File: BeanManagerImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public <T> InjectionTarget<T> createInjectionTarget(AnnotatedType<T> type) {
    throw new UnsupportedOperationException();
}
 
Example #30
Source File: DelegatingContextualLifecycle.java    From deltaspike with Apache License 2.0 2 votes vote down vote up
/**
 * Instantiate a new {@link ContextualLifecycle} backed by an
 * {@link InjectionTarget}.
 *
 * @param injectionTarget the {@link InjectionTarget} used to create and
 *                        destroy instances
 */
public DelegatingContextualLifecycle(InjectionTarget<T> injectionTarget)
{
    this.injectionTarget = injectionTarget;
}