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.resolution;
 
 import java.util.Map;
 import java.util.Set;
 
 
 
 public class ResolvableBuilder {
 
     private static final Class<?>[] FACADE_TYPES = new Class<?>[] { Event.classInstance.classProvider.class };
     private static final Class<?>[] METADATA_TYPES = new Class<?>[] { Interceptor.classDecorator.classBean.class };
     private static final Set<QualifierInstanceANY_SINGLETON = Collections.singleton(.);
 
     protected Class<?> rawType;
     protected final Set<Typetypes;
     protected final Set<Annotationqualifiers;
     protected final Set<QualifierInstancequalifierInstances;
     protected final Map<Class<? extends Annotation>, AnnotationmappedQualifiers;
     protected Bean<?> declaringBean;
     private final MetaAnnotationStore store;
     protected boolean delegate;
 
     public ResolvableBuilder(final MetaAnnotationStore store) {
         this. = store;
         this. = new HashSet<Type>();
         this. = new HashSet<Annotation>();
         this. = new HashMap<Class<? extends Annotation>, Annotation>();
         this. = new HashSet<QualifierInstance>();
     }
 
     public ResolvableBuilder(BeanManagerImpl manager) {
         this(manager.getServices().get(MetaAnnotationStore.class));
     }
 
     public ResolvableBuilder(Type typefinal BeanManagerImpl beanManager) {
         this(beanManager);
         if (type != null) {
             this. = Reflections.getRawType(type);
             if ( == null || type instanceof TypeVariable<?>) {
                 throw ..cannotExtractRawType(type);
             }
             this..add(type);
         }
     }
 
     public ResolvableBuilder(InjectionPoint injectionPointfinal BeanManagerImpl manager) {
         this(injectionPoint.getType(), manager);
         addQualifiers(injectionPoint.getQualifiers());
         if (.containsKey(Named.class) && injectionPoint.getMember() instanceof Field) {
             Named named = (Named.get(Named.class);
             QualifierInstance qualifierInstance = QualifierInstance.of(named);
             if (named.value().equals("")) {
                 .remove(named);
                 .remove(qualifierInstance);
                 // This is field injection point with an @Named qualifier, with no value specified, we need to assume the name of the field is the value
                 named = new NamedLiteral(injectionPoint.getMember().getName());
                 qualifierInstance = QualifierInstance.of(named);
                 .add(named);
                 .add(qualifierInstance);
                .put(Named.classnamed);
            }
        }
        setDeclaringBean(injectionPoint.getBean());
        this. = injectionPoint.isDelegate();
    }
    public ResolvableBuilder setDeclaringBean(Bean<?> declaringBean) {
        this. = declaringBean;
        return this;
    }
    public ResolvableBuilder addType(Type type) {
        this..add(type);
        return this;
    }
    public ResolvableBuilder addTypes(Set<Typetypes) {
        this..addAll(types);
        return this;
    }
    public boolean isDelegate() {
        return ;
    }
    public void setDelegate(boolean delegate) {
        this. = delegate;
    }
    public Resolvable create() {
        if (.size() == 0) {
            this..add(.);
        }
        for (Type type : ) {
            Class<?> rawType = Reflections.getRawType(type);
            for (Class<?> facadeType : ) {
                if (facadeType.equals(rawType)) {
                    return createFacade(facadeType);
                }
            }
            for (Class<?> metadataType : ) {
                if (metadataType.equals(rawType)) {
                    return createMetadataProvider(metadataType);
                }
            }
        }
    }
    private Resolvable createFacade(Class<?> rawType) {
        Set<Typetypes = Collections.<Type>singleton(rawType);
        return new ResolvableImpl(rawTypetypes);
    }
    // just as facade but we keep the qualifiers so that we can recognize Bean from @Intercepted Bean.
    private Resolvable createMetadataProvider(Class<?> rawType) {
        Set<Typetypes = Collections.<Type>singleton(rawType);
        return new ResolvableImpl(rawTypetypes);
    }
    public ResolvableBuilder addQualifier(Annotation qualifier) {
        QualifierInstance qualifierInstance = QualifierInstance.of(qualifier);
        final Class<? extends AnnotationannotationType = qualifierInstance.getAnnotationClass();
        // Handle the @New qualifier special case
        if (annotationType.equals(New.class)) {
            New newQualifier = New.class.cast(qualifier);
            if (newQualifier.value().equals(New.class) &&  == null) {
                throw new IllegalStateException("Cannot transform @New when there is no known raw type");
            } else if (newQualifier.value().equals(New.class)) {
                qualifier = new NewLiteral();
                qualifierInstance = QualifierInstance.of(qualifier);
            }
        }
        checkQualifier(qualifierannotationType);
        this..add(qualifier);
        this..add(qualifierInstance);
        this..put(annotationTypequalifier);
        return this;
    }
    public ResolvableBuilder addQualifierIfAbsent(Annotation qualifier) {
        if (!.contains(qualifier)) {
            addQualifier(qualifier);
        }
        return this;
    }
    public ResolvableBuilder addQualifiers(Annotation[] qualifiers) {
        for (Annotation qualifier : qualifiers) {
            addQualifier(qualifier);
        }
        return this;
    }
    public ResolvableBuilder addQualifiers(Collection<Annotationqualifiers) {
        for (Annotation qualifier : qualifiers) {
            addQualifier(qualifier);
        }
        return this;
    }
    protected void checkQualifier(Annotation qualifierClass<? extends AnnotationannotationType) {
        if (!.getBindingTypeModel(annotationType).isValid()) {
            throw ..invalidQualifier(qualifier);
        }
        if (isAnnotationTypePresent(annotationType)) {
            throw ..duplicateQualifiers();
        }
    }
    protected boolean isAnnotationTypePresent(Class<? extends AnnotationannotationType) {
        for (QualifierInstance instance : ) {
            if (instance.getAnnotationClass().equals(annotationType)) {
                return true;
            }
        }
        return false;
    }
    protected static class ResolvableImpl implements Resolvable {
        private final Set<QualifierInstancequalifierInstances;
        private final Map<Class<? extends Annotation>, AnnotationmappedQualifiers;
        private final Set<TypetypeClosure;
        private final Class<?> rawType;
        private final Bean<?> declaringBean;
        private final boolean delegate;
        protected ResolvableImpl(Class<?> rawTypeSet<TypetypeClosureMap<Class<? extends Annotation>, AnnotationmappedQualifiersBean<?> declaringBeanfinal Set<QualifierInstancequalifierInstancesboolean delegate) {
            this. = mappedQualifiers;
            this. = typeClosure;
            this. = rawType;
            this. = declaringBean;
            this. = qualifierInstances;
            this. = delegate;
        }
        @Override
        public Set<QualifierInstancegetQualifiers() {
            return ;
        }
        @Override
        public boolean isAnnotationPresent(Class<? extends AnnotationannotationType) {
            return .containsKey(annotationType);
        }
        @Override
        public Set<TypegetTypes() {
            return ;
        }
        @Override
        public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
            return Reflections.<A>cast(.get(annotationType));
        }
        @Override
        public Class<?> getJavaClass() {
            return ;
        }
        @Override
        public Bean<?> getDeclaringBean() {
            return ;
        }
        @Override
        public String toString() {
            return "Types: " + getTypes() + "; Bindings: " + getQualifiers();
        }
        @Override
        public int hashCode() {
            int result = 17;
            result = 31 * result + this.getTypes().hashCode();
            result = 31 * result + this..hashCode();
            return result;
        }
        @Override
        public boolean equals(Object o) {
            if (o instanceof ResolvableImpl) {
                ResolvableImpl r = (ResolvableImplo;
                return this.getTypes().equals(r.getTypes()) && this..equals(r.qualifierInstances);
            }
            return false;
        }
        @Override
        public boolean isDelegate() {
            return ;
        }
    }
        return ;
    }
New to GrepCode? Check out our FAQ X