protected void registerOuterDecorator(ProxyObject instance, T outerDelegate) { CombinedInterceptorAndDecoratorStackMethodHandler wrapperMethodHandler = (CombinedInterceptorAndDecoratorStackMethodHandler) instance.weld_getHandler(); wrapperMethodHandler.setOuterDecorator(outerDelegate); }
/** * Method to create a new proxy that wraps the bean instance. * * @param beanInstance the bean instance * @return a new proxy object */ public T create(BeanInstance beanInstance) { final T proxy = (System.getSecurityManager() == null) ? run() : AccessController.doPrivileged(this); ((ProxyObject) proxy).weld_setHandler(new ProxyMethodHandler(contextId, beanInstance, bean)); return proxy; }
protected void registerOuterDecorator(ProxyObject instance, T outerDelegate) { CombinedInterceptorAndDecoratorStackMethodHandler wrapperMethodHandler = (CombinedInterceptorAndDecoratorStackMethodHandler) instance.getHandler(); wrapperMethodHandler.setOuterDecorator(outerDelegate); }
@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).weld_setHandler(handler); } }
UnproxyableResolutionException exception = Proxies.getUnproxyableTypeException(annotatedType.getBaseType(), null, beanManager.getServices(), ignoreFinalMethods); if (exception != null) { AnnotatedType<?> instanceAnnotatedType = beanManager.createAnnotatedType(instance.getClass()); AnnotatedTypeConfiguratorImpl<?> newAtConfigurator = new AnnotatedTypeConfiguratorImpl<>(instanceAnnotatedType); if (beanManager.isInterceptorBinding(annotation.annotationType())) { if (newAtConfigurator.getAnnotated().isAnnotationPresent(annotation.annotationType())) { ((ProxyObject) proxy).weld_setHandler(methodHandler);
@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).weld_setHandler(new CombinedInterceptorAndDecoratorStackMethodHandler()); // Set method handler for private methods if necessary InterceptedSubclassFactory.setPrivateMethodHandler(instance); } } return instance; }
@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.weld_getHandler() instanceof ProxyMethodHandler) { ProxyMethodHandler handler = (ProxyMethodHandler) proxy.weld_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); }
@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); } }
@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; 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).weld_setHandler(methodHandler); return proxy; }
/** * Convenience method to set the underlying bean instance for a proxy. * * @param proxy the proxy instance * @param beanInstance the instance of the bean */ public static <T> void setBeanInstance(String contextId, T proxy, BeanInstance beanInstance, Bean<?> bean) { if (proxy instanceof ProxyObject) { ProxyObject proxyView = (ProxyObject) proxy; proxyView.weld_setHandler(new ProxyMethodHandler(contextId, beanInstance, bean)); } }
protected T applyInterceptors(T instance, InterceptionContext interceptionContext) { try { InterceptorMethodHandler methodHandler = new InterceptorMethodHandler(interceptionContext); CombinedInterceptorAndDecoratorStackMethodHandler wrapperMethodHandler = (CombinedInterceptorAndDecoratorStackMethodHandler) ((ProxyObject) instance).weld_getHandler(); wrapperMethodHandler.setInterceptorMethodHandler(methodHandler); } catch (Exception e) { throw new DeploymentException(e); } return instance; }
@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; }
@Override public void destroy(T instance) { Preconditions.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 (instance instanceof EnterpriseBeanInstance && 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); }
/** * Method to create a new proxy that wraps the bean instance. * * @param beanInstance the bean instance * @return a new proxy object */ public T create(BeanInstance beanInstance) { final T proxy = (System.getSecurityManager() == null) ? run() : AccessController.doPrivileged(this); ((ProxyObject) proxy).setHandler(new ProxyMethodHandler(contextId, beanInstance, bean)); return proxy; }
@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.weld_setHandler(new SessionBeanViewMethodHandler(bean.getTypes(), (CombinedInterceptorAndDecoratorStackMethodHandler) proxy.weld_getHandler())); } return result; }
/** * Convenience method to set the underlying bean instance for a proxy. * * @param proxy the proxy instance * @param beanInstance the instance of the bean */ public static <T> void setBeanInstance(String contextId, T proxy, BeanInstance beanInstance, Bean<?> bean) { if (proxy instanceof ProxyObject) { ProxyObject proxyView = (ProxyObject) proxy; proxyView.setHandler(new ProxyMethodHandler(contextId, beanInstance, bean)); } }
protected T applyInterceptors(T instance, InterceptionContext interceptionContext) { try { InterceptorMethodHandler methodHandler = new InterceptorMethodHandler(interceptionContext); CombinedInterceptorAndDecoratorStackMethodHandler wrapperMethodHandler = (CombinedInterceptorAndDecoratorStackMethodHandler) ((ProxyObject) instance).getHandler(); wrapperMethodHandler.setInterceptorMethodHandler(methodHandler); } catch (Exception e) { throw new DeploymentException(e); } return instance; }
@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; }
@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).weld_setHandler(handler); } }
UnproxyableResolutionException exception = Proxies.getUnproxyableTypeException(annotatedType.getBaseType(), null, beanManager.getServices(), ignoreFinalMethods); if (exception != null) { AnnotatedType<?> instanceAnnotatedType = beanManager.createAnnotatedType(instance.getClass()); AnnotatedTypeConfiguratorImpl<?> newAtConfigurator = new AnnotatedTypeConfiguratorImpl<>(instanceAnnotatedType); if (beanManager.isInterceptorBinding(annotation.annotationType())) { if (newAtConfigurator.getAnnotated().isAnnotationPresent(annotation.annotationType())) { ((ProxyObject) proxy).weld_setHandler(methodHandler);