Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc. and/or its affiliates, 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
  * 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.bean.proxy;
 import static org.jboss.weld.util.reflection.Reflections.cast;
 import  edu.umd.cs.findbugs.annotations.SuppressWarnings;

An instance locator that uses a context to lookup the instance if it exists; otherwise, a new instance will be created from the bean.

David Allen
 @SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "bean field is loaded lazily")
 public class ContextBeanInstance<T> extends AbstractBeanInstance implements Serializable {
     private static final long serialVersionUID = -8144230657830556503L;
     // The bean
     private transient Bean<T> bean;
     // The bean index in the manager
     private final BeanIdentifier id;
     private final String contextId;
     // The actual type of the resulting bean instance
     private final Class<?> instanceType;
     private static final ThreadLocal<WeldCreationalContext<?>> currentCreationalContext = new ThreadLocal<WeldCreationalContext<?>>();

Creates a new locator for instances of the given bean.

bean The contextual bean
id The unique identifier of this bean
     public ContextBeanInstance(Bean<T> beanBeanIdentifier idString contextId) {
         this. = bean;
         this. = id;
         this. = contextId;
         this. = computeInstanceType(bean);
     public T getInstance() {
         Container container = Container.instance();
         if ( == null) {
              =<Bean<T>, T>getContextual();
         Context context = container.deploymentManager().getContext(.getScope());
         T existingInstance = context.get();
         if (existingInstance != null) {
             return existingInstance;
         WeldCreationalContext<T> creationalContext;
         WeldCreationalContext<?> previousCreationalContext = .get();
         if (previousCreationalContext == null) {
             creationalContext = new CreationalContextImpl<T>();
         } else {
             creationalContext = previousCreationalContext.getCreationalContext();
         final CurrentInjectionPoint currentInjectionPoint =;
         try {
             // Ensure that there is no injection point associated
             return context.get(creationalContext);
         } finally {
            if (previousCreationalContext == null) {
            } else {
    public Class<T> getInstanceType() {
        return cast();
New to GrepCode? Check out our FAQ X