Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, 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.bean.builtin;
 
 import static org.jboss.weld.util.reflection.Reflections.cast;
 
 import java.util.Set;
 
 
 
Helper implementation for Instance for getting instances

Parameters:
<T>
Author(s):
Gavin King
 
 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = { "SE_NO_SUITABLE_CONSTRUCTOR""SE_BAD_FIELD" }, justification = "Uses SerializationProxy")
 public class InstanceImpl<T> extends AbstractFacade<T, Instance<T>> implements Instance<T>, Serializable {
 
     private static final long serialVersionUID = -376721889693284887L;
 
     private final Set<Bean<?>> beans;
 
     public static <I> Instance<I> of(InjectionPoint injectionPointCreationalContext<I> creationalContext,
             BeanManagerImpl beanManager) {
         return new InstanceImpl<I>(injectionPointcreationalContextbeanManager);
     }
 
     private InstanceImpl(InjectionPoint injectionPointCreationalContext<? super T> creationalContextBeanManagerImpl beanManager) {
         super(injectionPointcreationalContextbeanManager);
 
         // Perform typesafe resolution, and possibly attempt to resolve the ambiguity
         Resolvable resolvable = new ResolvableBuilder(getType(), getBeanManager()).addQualifiers(getQualifiers())
                 .setDeclaringBean(getInjectionPoint().getBean()).create();
         TypeSafeBeanResolver beanResolver = getBeanManager().getBeanResolver();
         this. = beanResolver.resolve(beanResolver.resolve(resolvable, Reflections.isCacheable(getQualifiers())));
     }
 
     public T get() {
         if (isUnsatisfied()) {
             throw ..unresolvableElement("Type: " + getType() + "; Qualifiers: " + getQualifiers());
         } else if (isAmbiguous()) {
             throw ..ambiguousBeansForDependency(WeldCollections.toMultiRowString());
         } else {
             return getBeanInstance(.iterator().next());
         }
     }

    
Gets a string representation

Returns:
A string representation
 
     @Override
     public String toString() {
         return Formats.formatAnnotations(getQualifiers()) + " Instance<" + Formats.formatType(getType()) + ">";
    }
    public Iterator<T> iterator() {
        return new InstanceImplIterator();
    }
    public boolean isAmbiguous() {
        return .size() > 1;
    }
    public boolean isUnsatisfied() {
        return .isEmpty();
    }
    public Instance<T> select(Annotation... qualifiers) {
        return selectInstance(this.getType(), qualifiers);
    }
    public <U extends T> Instance<U> select(Class<U> subtypeAnnotation... qualifiers) {
        return selectInstance(subtypequalifiers);
    }
    public <U extends T> Instance<U> select(TypeLiteral<U> subtypeAnnotation... qualifiers) {
        return selectInstance(subtype.getType(), qualifiers);
    }
    private <U extends T> Instance<U> selectInstance(Type subtypeAnnotation[] newQualifiers) {
        InjectionPoint modifiedInjectionPoint = new FacadeInjectionPoint(getBeanManager(), getInjectionPoint(), subtypegetQualifiers(),
                newQualifiers);
        return new InstanceImpl<U>(modifiedInjectionPointgetCreationalContext(), getBeanManager());
    }
    @Override
    public void destroy(T instance) {
        Preconditions.checkNotNull(instance);
        // check if this is a proxy of a normal-scoped bean
        if (instance instanceof ProxyObject) {
            ProxyObject proxy = (ProxyObjectinstance;
            if (proxy.getHandler() instanceof ProxyMethodHandler) {
                ProxyMethodHandler handler = (ProxyMethodHandlerproxy.getHandler();
                Bean<?> bean = handler.getBean();
                Context context = getBeanManager().getContext(bean.getScope());
                if (context instanceof AlterableContext) {
                    AlterableContext alterableContext = (AlterableContextcontext;
                    alterableContext.destroy(bean);
                    return;
                } else {
                    throw ..destroyUnsupported(context);
                }
            }
        }
        // check if this is a dependent instance
        CreationalContext<? super T> ctx = getCreationalContext();
        if (ctx instanceof WeldCreationalContext<?>) {
            WeldCreationalContext<? super T> weldCtx = cast(ctx);
            weldCtx.destroyDependentInstance(instance);
        }
    }
    private T getBeanInstance(Bean<?> bean) {
        // Generate a correct injection point for the bean, we do this by taking the original injection point and adjusting the
        // qualifiers and type
        CurrentInjectionPoint currentInjectionPoint = getBeanManager().getServices().get(CurrentInjectionPoint.class);
        try {
            currentInjectionPoint.push(ip);
            return Reflections.<T> cast(getBeanManager().getReference(beangetType(), getCreationalContext()));
        } finally {
            currentInjectionPoint.pop();
        }
    }
    // Serialization
    private Object writeReplace() throws ObjectStreamException {
        return new SerializationProxy<T>(this);
    }
    private void readObject(ObjectInputStream streamthrows InvalidObjectException {
        throw ..proxyRequired();
    }
    private static class SerializationProxy<T> extends AbstractFacadeSerializationProxy<T, Instance<T>> {
        private static final long serialVersionUID = 9181171328831559650L;
        public SerializationProxy(InstanceImpl<T> instance) {
            super(instance);
        }
        private Object readResolve() throws ObjectStreamException {
            return InstanceImpl.of(getInjectionPoint(), getCreationalContext(), getBeanManager());
        }
    }
    final class InstanceImplIterator implements Iterator<T> {
        private final Iterator<Bean<?>> delegate;
        private InstanceImplIterator(Set<Bean<?>> beans) {
            super();
            this. = beans.iterator();
        }
        @Override
        public boolean hasNext() {
            return .hasNext();
        }
        @Override
        public T next() {
            return getBeanInstance(.next());
        }
        @Override
        public void remove() {
            throw ..instanceIteratorRemoveUnsupported();
        }
    }
New to GrepCode? Check out our FAQ X