Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2013, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.injection.producer;
 
 import java.util.Map;
 
 
Delegating Instantiator that takes care of javax.interceptor.AroundConstruct interceptor invocation.

Author(s):
Jozef Hartinger
 
 
     private final InterceptionModel model;
     private final SlimAnnotatedType<?> annotatedType;
 
     public ConstructorInterceptionInstantiator(Instantiator<T> delegateInterceptionModel modelSlimAnnotatedType<?> type) {
         super(delegate);
         this. = model;
         this. = type;
     }
 
     @Override
     public T newInstance(CreationalContext<T> ctxBeanManagerImpl manager) {
         if (ctx instanceof CreationalContextImpl<?>) {
             CreationalContextImpl<T> weldCtx = Reflections.cast(ctx);
             if (!weldCtx.isConstructorInterceptionSuppressed()) {
                 registerAroundConstructCallback(weldCtxmanager);
             }
         }
 
         return delegate().newInstance(ctxmanager);
     }
 
     private void registerAroundConstructCallback(CreationalContextImpl<T> ctxBeanManagerImpl manager) {
         final InterceptionContext interceptionContext = InterceptionContext.forConstructorInterception(ctxmanager);
 
         AroundConstructCallback<T> callback = new AroundConstructCallback<T>() {
 
             @Override
             public T aroundConstruct(final ConstructionHandle<T> handleAnnotatedConstructor<T> constructorObject[] parametersMap<StringObjectdata) {
                 /*
                  * The AroundConstruct interceptor method can access the constructed instance using InvocationContext.getTarget
                  * method after the InvocationContext.proceed completes.
                  */
                 final AtomicReference<T> target = new AtomicReference<T>();
 
                 SimpleInterceptionChain chain = new SimpleInterceptionChain(.getConstructorInvocationInterceptors(), interceptionContext.) {
                     @Override
                     protected Object interceptorChainCompleted(InvocationContext invocationCtxthrows Exception {
                         // all the interceptors were invoked, call the constructor now
                         T instance = handle.proceed(invocationCtx.getParameters(), invocationCtx.getContextData());
                         target.set(instance);
                         return null;
                     }
                 };
 
                 InterceptorInvocationContext invocationCtx = new InterceptorInvocationContext(chainconstructor.getJavaMember(), parametersdata) {
                     @Override
                     public Object getTarget() {
                         return target.get();
                     }
                 };
 
                 try {
                    chain.invokeNextInterceptor(invocationCtx);
                } catch (RuntimeException e) {
                    throw e;
                } catch (Throwable e) {
                    throw new WeldException(e);
                }
                return target.get();
            }
        };
        ctx.registerAroundConstructCallback(callback);
    }
    @Override
    public String toString() {
        return "ConstructorInterceptionInstantiator wrapping " + delegate();
    }
New to GrepCode? Check out our FAQ X