Java Code Examples for javax.enterprise.inject.spi.BeanManager#createAnnotatedType()

The following examples show how to use javax.enterprise.inject.spi.BeanManager#createAnnotatedType() . 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: SpringCDIExtension.java    From datawave with Apache License 2.0 6 votes vote down vote up
public SpringCDIBean(SpringBean sb, Type targetType, BeanManager beanManager) {
    this.annotation = sb;
    this.targetType = targetType;
    this.name = sb.name();
    if ("".equals(name.trim())) {
        name = generateName();
    }
    
    AnnotatedType<Object> at = beanManager.createAnnotatedType(Object.class);
    injectionTarget = beanManager.createInjectionTarget(at);
    
    if (targetType instanceof ParameterizedType) {
        rawType = (Class<?>) ((ParameterizedType) targetType).getRawType();
    } else {
        rawType = (Class<?>) targetType;
    }
}
 
Example 2
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 3
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 4
Source File: DeltaSpikeProxyContextualLifecycle.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public DeltaSpikeProxyContextualLifecycle(Class<T> targetClass,
                                          Class<H> delegateInvocationHandlerClass,
                                          DeltaSpikeProxyFactory proxyFactory,
                                          BeanManager beanManager)
{
    this.targetClass = targetClass;
    this.delegateInvocationHandlerClass = delegateInvocationHandlerClass;
    this.proxyClass = proxyFactory.getProxyClass(beanManager, targetClass);
    this.delegateMethods = proxyFactory.getDelegateMethods(targetClass);
    this.beanManager = beanManager;
    
    if (!targetClass.isInterface())
    {
        AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(this.targetClass);
        this.injectionTarget = beanManager.createInjectionTarget(annotatedType);
    }
}
 
Example 5
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 6
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 7
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 8
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 9
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 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: CdiHelper.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
static <T extends Annotation> void declareAsInterceptorBinding(Class<T> annotation, BeanManager manager, BeforeBeanDiscovery bbd) {
    AnnotatedType<T> annotated = manager.createAnnotatedType(annotation);
    Set<AnnotatedMethod<? super T>> methods = new HashSet<>();
    for (AnnotatedMethod<? super T> method : annotated.getMethods())
        methods.add(new AnnotatedMethodDecorator<>(method, NON_BINDING));

    bbd.addInterceptorBinding(new AnnotatedTypeDecorator<>(annotated, INTERCEPTOR_BINDING, methods));
}
 
Example 12
Source File: SyntheticBean.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
SyntheticBean(BeanManager manager, Class<T> clazz, String name, String description) {
    AnnotatedType<T> annotatedType = manager.createAnnotatedType(clazz);
    this.clazz = clazz;
    this.name = name;
    this.description = description;
    this.types = annotatedType.getTypeClosure();
    this.target = manager.createInjectionTarget(annotatedType);
}
 
Example 13
Source File: ConfigExtension.java    From smallrye-config with Apache License 2.0 4 votes vote down vote up
protected void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, BeanManager bm) {
    AnnotatedType<ConfigProducer> configBean = bm.createAnnotatedType(ConfigProducer.class);
    bbd.addAnnotatedType(configBean, ConfigProducer.class.getName());
}
 
Example 14
Source File: CDIInterceptorWrapperImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static Map<Method, List<InterceptorInvoker>> initInterceptorInvokers(BeanManager beanManager,
                                                                             CreationalContext<?> creationalContext,
                                                                             Class<?> restClient) {
    Map<Method, List<InterceptorInvoker>> invokers = new HashMap<>();
    // Interceptor as a key in a map is not entirely correct (custom interceptors) but should work in most cases
    Map<Interceptor<?>, Object> interceptorInstances = new HashMap<>();
    
    AnnotatedType<?> restClientType = beanManager.createAnnotatedType(restClient);

    List<Annotation> classBindings = getBindings(restClientType.getAnnotations(), beanManager);

    for (AnnotatedMethod<?> method : restClientType.getMethods()) {
        Method javaMethod = method.getJavaMember();
        if (javaMethod.isDefault() || method.isStatic()) {
            continue;
        }
        List<Annotation> methodBindings = getBindings(method.getAnnotations(), beanManager);

        if (!classBindings.isEmpty() || !methodBindings.isEmpty()) {
            Annotation[] interceptorBindings = merge(methodBindings, classBindings);

            List<Interceptor<?>> interceptors =
                new ArrayList<>(beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, 
                                                                interceptorBindings));
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Resolved interceptors from beanManager, " + beanManager + ":" + interceptors);
            }

            if (!interceptors.isEmpty()) {
                List<InterceptorInvoker> chain = new ArrayList<>();
                for (Interceptor<?> interceptor : interceptors) {
                    chain.add(
                        new InterceptorInvoker(interceptor, 
                                               interceptorInstances.computeIfAbsent(interceptor, 
                                                   i -> beanManager.getReference(i, 
                                                                                 i.getBeanClass(), 
                                                                                 creationalContext))));
                }
                invokers.put(javaMethod, chain);
            }
        }
    }
    return invokers.isEmpty() ? Collections.emptyMap() : invokers;
}
 
Example 15
Source File: ResourceLoaderExtension.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private AnnotatedType<?> createAnnotatedType(final Class<?> clazz, final BeanManager beanManager)
{
    return beanManager.createAnnotatedType(clazz);
}