Search in sources :

Example 1 with ProxyObject

use of org.jboss.weld.bean.proxy.ProxyObject in project core by weld.

the class ProxyClassConstructorInjectionPointWrapper method newInstance.

@Override
protected T newInstance(Object[] parameterValues) {
    // Once the instance is created, a method handler is required regardless of whether
    // an actual bean instance is known yet.
    final T instance = super.newInstance(parameterValues);
    if (decorator) {
        BeanInstance beanInstance = null;
        if (hasDelegateInjectionPoint()) {
            Object decoratorDelegate = parameterValues[delegateInjectionPointPosition];
            beanInstance = new TargetBeanInstance(decoratorDelegate);
        }
        ProxyFactory.setBeanInstance(contextId, instance, beanInstance, bean);
    } else {
        if (instance instanceof ProxyObject) {
            ((ProxyObject) instance).setHandler(new CombinedInterceptorAndDecoratorStackMethodHandler());
            // Set method handler for private methods if necessary
            InterceptedSubclassFactory.setPrivateMethodHandler(instance);
        }
    }
    return instance;
}
Also used : ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) CombinedInterceptorAndDecoratorStackMethodHandler(org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler) TargetBeanInstance(org.jboss.weld.bean.proxy.TargetBeanInstance) BeanInstance(org.jboss.weld.bean.proxy.BeanInstance) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) TargetBeanInstance(org.jboss.weld.bean.proxy.TargetBeanInstance)

Example 2 with ProxyObject

use of org.jboss.weld.bean.proxy.ProxyObject in project core by weld.

the class InstanceImpl method destroy.

@Override
public void destroy(T instance) {
    checkNotNull(instance);
    // Attempt to destroy instance which is either a client proxy or a dependent session bean proxy
    if (instance instanceof ProxyObject) {
        ProxyObject proxy = (ProxyObject) instance;
        if (proxy.getHandler() instanceof ProxyMethodHandler) {
            ProxyMethodHandler handler = (ProxyMethodHandler) proxy.getHandler();
            Bean<?> bean = handler.getBean();
            if (isSessionBeanProxy(instance) && Dependent.class.equals(bean.getScope())) {
                // Destroy internal reference to a dependent session bean
                destroyDependentInstance(instance);
                return;
            } else {
                // Destroy contextual instance of a normal-scoped bean
                Context context = getBeanManager().getContext(bean.getScope());
                if (context instanceof AlterableContext) {
                    AlterableContext alterableContext = (AlterableContext) context;
                    alterableContext.destroy(bean);
                    return;
                } else {
                    throw BeanLogger.LOG.destroyUnsupported(context);
                }
            }
        }
    }
    // Attempt to destroy dependent instance which is neither a client proxy nor a dependent session bean proxy
    destroyDependentInstance(instance);
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) Dependent(javax.enterprise.context.Dependent) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyMethodHandler(org.jboss.weld.bean.proxy.ProxyMethodHandler)

Example 3 with ProxyObject

use of org.jboss.weld.bean.proxy.ProxyObject in project core by weld.

the class InterceptionFactoryImpl method createInterceptedInstance.

@Override
public T createInterceptedInstance(T instance) {
    if (used) {
        throw InterceptorLogger.LOG.interceptionFactoryNotReusable();
    }
    if (instance instanceof ProxyObject) {
        InterceptorLogger.LOG.interceptionFactoryInternalContainerConstruct(instance.getClass());
        return instance;
    }
    UnproxyableResolutionException exception = Proxies.getUnproxyableTypeException(annotatedType.getBaseType(), null, beanManager.getServices(), ignoreFinalMethods);
    if (exception != null) {
        throw exception;
    }
    used = true;
    if (annotatedType.getJavaClass().isInterface()) {
        throw InterceptorLogger.LOG.interceptionFactoryNotOnInstance(annotatedType.getJavaClass().getCanonicalName());
    }
    Optional<InterceptionFactoryData<T>> cached = beanManager.getServices().get(InterceptionFactoryDataCache.class).getInterceptionFactoryData(configurator != null ? configurator.complete() : annotatedType);
    if (!cached.isPresent()) {
        InterceptorLogger.LOG.interceptionFactoryNotRequired(annotatedType.getJavaClass().getSimpleName());
        return instance;
    }
    InterceptionFactoryData<T> data = cached.get();
    InterceptedProxyMethodHandler methodHandler = new InterceptedProxyMethodHandler(instance);
    methodHandler.setInterceptorMethodHandler(new InterceptorMethodHandler(InterceptionContext.forNonConstructorInterception(data.getInterceptionModel(), creationalContext, beanManager, data.getSlimAnnotatedType())));
    T proxy = (System.getSecurityManager() == null) ? data.getInterceptedProxyFactory().run() : AccessController.doPrivileged(data.getInterceptedProxyFactory());
    ((ProxyObject) proxy).setHandler(methodHandler);
    return proxy;
}
Also used : ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) InterceptedProxyMethodHandler(org.jboss.weld.bean.proxy.InterceptedProxyMethodHandler) InterceptionFactoryData(org.jboss.weld.bean.proxy.InterceptionFactoryDataCache.InterceptionFactoryData) InterceptionFactoryDataCache(org.jboss.weld.bean.proxy.InterceptionFactoryDataCache) UnproxyableResolutionException(org.jboss.weld.exceptions.UnproxyableResolutionException) InterceptorMethodHandler(org.jboss.weld.interceptor.proxy.InterceptorMethodHandler)

Example 4 with ProxyObject

use of org.jboss.weld.bean.proxy.ProxyObject in project core by weld.

the class SessionBeanInjectionTarget method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    T result = super.produce(ctx);
    if (result instanceof ProxyObject) {
        // if decorators are applied, use SessionBeanViewMethodHandler
        ProxyObject proxy = (ProxyObject) result;
        proxy.setHandler(new SessionBeanViewMethodHandler(bean.getTypes(), (CombinedInterceptorAndDecoratorStackMethodHandler) proxy.getHandler()));
    }
    return result;
}
Also used : ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) CombinedInterceptorAndDecoratorStackMethodHandler(org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler)

Example 5 with ProxyObject

use of org.jboss.weld.bean.proxy.ProxyObject in project core by weld.

the class DecoratorInjectionTarget method inject.

@Override
public void inject(T instance, CreationalContext<T> ctx) {
    super.inject(instance, ctx);
    if (accessibleField != null && instance instanceof DecoratorProxy) {
        // this code is only applicable if the delegate is injected into a field
        // as the proxy can't intercept the delegate when setting the field
        // we need to now read the delegate from the field
        // this is only needed for fields, as constructor and method injection are handed
        // at injection time
        Object delegate;
        try {
            delegate = accessibleField.get(instance);
        } catch (IllegalAccessException e) {
            throw UtilLogger.LOG.accessErrorOnField(accessibleField.getName(), accessibleField.getDeclaringClass(), e);
        }
        final ProxyMethodHandler handler = new ProxyMethodHandler(beanManager.getContextId(), new TargetBeanInstance(delegate), getBean());
        ((ProxyObject) instance).setHandler(handler);
    }
}
Also used : ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) DecoratorProxy(org.jboss.weld.bean.proxy.DecoratorProxy) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) TargetBeanInstance(org.jboss.weld.bean.proxy.TargetBeanInstance) ProxyMethodHandler(org.jboss.weld.bean.proxy.ProxyMethodHandler)

Aggregations

ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)5 CombinedInterceptorAndDecoratorStackMethodHandler (org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler)2 ProxyMethodHandler (org.jboss.weld.bean.proxy.ProxyMethodHandler)2 TargetBeanInstance (org.jboss.weld.bean.proxy.TargetBeanInstance)2 Dependent (javax.enterprise.context.Dependent)1 AlterableContext (javax.enterprise.context.spi.AlterableContext)1 Context (javax.enterprise.context.spi.Context)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 BeanInstance (org.jboss.weld.bean.proxy.BeanInstance)1 DecoratorProxy (org.jboss.weld.bean.proxy.DecoratorProxy)1 InterceptedProxyMethodHandler (org.jboss.weld.bean.proxy.InterceptedProxyMethodHandler)1 InterceptionFactoryDataCache (org.jboss.weld.bean.proxy.InterceptionFactoryDataCache)1 InterceptionFactoryData (org.jboss.weld.bean.proxy.InterceptionFactoryDataCache.InterceptionFactoryData)1 WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)1 UnproxyableResolutionException (org.jboss.weld.exceptions.UnproxyableResolutionException)1 InterceptorMethodHandler (org.jboss.weld.interceptor.proxy.InterceptorMethodHandler)1