Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, 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.attributes;
 
 import java.util.Set;
 
 
Creates javax.enterprise.inject.spi.BeanAttributes based on a given annotated.

Author(s):
Jozef Hartinger
 
 public class BeanAttributesFactory {
 
 
     private BeanAttributesFactory() {
     }

    
Creates new javax.enterprise.inject.spi.BeanAttributes to represent a managed bean.
 
     public static <T> BeanAttributes<T> forBean(EnhancedAnnotated<T, ?> annotatedBeanManagerImpl manager) {
         return new BeanAttributesBuilder<T>(annotatednullmanager).build();
     }

    
Creates new javax.enterprise.inject.spi.BeanAttributes to represent a session bean.
 
     public static <T> BeanAttributes<T> forSessionBean(EnhancedAnnotatedType<T> annotatedInternalEjbDescriptor<?> descriptorBeanManagerImpl manager) {
         return new BeanAttributesBuilder<T>(annotated, Reflections.<InternalEjbDescriptor<T>> cast(descriptor), manager).build();
     }
 
     public static <T> BeanAttributes<T> forNewBean(Set<Typetypesfinal Class<?> javaClass) {
         Set<Annotationqualifiers = Collections.<Annotation>singleton(new NewLiteral(javaClass));
         return new ImmutableBeanAttributes<T>(Collections.<Class<? extends Annotation>> emptySet(), falsenullqualifierstypesDependent.class);
     }
 
     public static <T> BeanAttributes<T> forNewManagedBean(EnhancedAnnotatedType<T> weldClassBeanManagerImpl manager) {
         return forNewBean(SharedObjectCache.instance(manager).getSharedSet(Beans.getTypes(weldClass)), weldClass.getJavaClass());
     }
 
     public static <T> BeanAttributes<T> forNewSessionBean(BeanAttributes<T> originalAttributesClass<?> javaClass) {
         return forNewBean(originalAttributes.getTypes(), javaClass);
     }
 
     private static class BeanAttributesBuilder<T> {
 
         private MergedStereotypes<T, ?> mergedStereotypes;
         private boolean alternative;
         private String name;
         private Set<Annotationqualifiers;
         private Set<Typetypes;
         private Class<? extends Annotationscope;
         private BeanManagerImpl manager;
 
         private BeanAttributesBuilder(EnhancedAnnotated<T, ?> annotatedInternalEjbDescriptor<T> descriptorBeanManagerImpl manager) {
            this. = manager;
            initStereotypes(annotatedmanager);
            initAlternative(annotated);
            initName(annotated);
            initQualifiers(annotated);
            initScope(annotated);
            if (descriptor == null) {
                 = SharedObjectCache.instance(manager).getSharedSet(Beans.getTypes(annotated));
            } else {
                 = SharedObjectCache.instance(manager).getSharedSet(Beans.getTypes(annotateddescriptor));
            }
        }
        protected <S> void initStereotypes(EnhancedAnnotated<T, S> annotatedBeanManagerImpl manager) {
            this. = MergedStereotypes.of(annotatedmanager);
        }
        protected void initAlternative(EnhancedAnnotated<T, ?> annotated) {
            this. = Beans.isAlternative(annotated);
        }

        
Initializes the name
        protected void initName(EnhancedAnnotated<T, ?> annotated) {
            boolean beanNameDefaulted = false;
            if (annotated.isAnnotationPresent(Named.class)) {
                String javaName = annotated.getAnnotation(Named.class).value();
                if ("".equals(javaName)) {
                    beanNameDefaulted = true;
                } else {
                    this. = javaName;
                    return;
                }
            }
            if (beanNameDefaulted || ( != null && .isBeanNameDefaulted())) {
                this. = getDefaultName(annotated);
            }
        }

        
Gets the default name of the bean

Returns:
The default name
        protected String getDefaultName(EnhancedAnnotated<?, ?> annotated) {
            if (annotated instanceof EnhancedAnnotatedType<?>) {
                return Introspector.decapitalize(((EnhancedAnnotatedType<?>) annotated).getSimpleName());
            } else if (annotated instanceof EnhancedAnnotatedField<?, ?>) {
                return ((EnhancedAnnotatedField<?, ?>) annotated).getPropertyName();
            } else if (annotated instanceof EnhancedAnnotatedMethod<?, ?>) {
                return ((EnhancedAnnotatedMethod<?, ?>) annotated).getPropertyName();
            } else {
                return null;
            }
        }
        protected void initQualifiers(Set<Annotationqualifiers) {
            if (qualifiers.isEmpty()) {
                this. = ;
            } else {
                ArraySet<AnnotationnormalizedQualifiers = new ArraySet<Annotation>(qualifiers.size() + 2);
                if (qualifiers.size() == 1) {
                    if (qualifiers.iterator().next().annotationType().equals(Named.class)) {
                        normalizedQualifiers.add(.);
                    }
                }
                normalizedQualifiers.addAll(qualifiers);
                normalizedQualifiers.add(.);
                if ( != null && normalizedQualifiers.remove(.)) {
                    normalizedQualifiers.add(new NamedLiteral());
                }
                this. = SharedObjectCache.instance().getSharedSet(normalizedQualifiers);
            }
        }
        protected void initQualifiers(EnhancedAnnotated<?, ?> annotated) {
            initQualifiers(annotated.getMetaAnnotations(Qualifier.class));
        }
        protected void initScope(EnhancedAnnotated<T, ?> annotated) {
            // class bean
            if (annotated instanceof EnhancedAnnotatedType<?>) {
                EnhancedAnnotatedType<?> weldClass = (EnhancedAnnotatedType<?>) annotated;
                for (EnhancedAnnotatedType<?> clazz = weldClassclazz != nullclazz = clazz.getEnhancedSuperclass()) {
                    Set<Annotationscopes = new HashSet<Annotation>();
                    scopes.addAll(clazz.getDeclaredMetaAnnotations(Scope.class));
                    scopes.addAll(clazz.getDeclaredMetaAnnotations(NormalScope.class));
                    validateScopeSet(scopesannotated);
                    if (scopes.size() == 1) {
                        if (annotated.isAnnotationPresent(scopes.iterator().next().annotationType())) {
                            this. = scopes.iterator().next().annotationType();
                        }
                        break;
                    }
                }
            } else {
                // producer field or method
                Set<Annotationscopes = new HashSet<Annotation>();
                scopes.addAll(annotated.getMetaAnnotations(Scope.class));
                scopes.addAll(annotated.getMetaAnnotations(NormalScope.class));
                if (scopes.size() == 1) {
                    this. = scopes.iterator().next().annotationType();
                }
                validateScopeSet(scopesannotated);
            }
            if (this. == null) {
                initScopeFromStereotype();
            }
            if (this. == null) {
                this. = Dependent.class;
            }
        }
        protected void validateScopeSet(Set<AnnotationscopesEnhancedAnnotated<T, ?> annotated) {
            if (scopes.size() > 1) {
                throw ..onlyOneScopeAllowed(annotated);
            }
        }
        protected boolean initScopeFromStereotype() {
            Set<AnnotationpossibleScopes = .getPossibleScopes();
            if (possibleScopes.size() == 1) {
                this. = possibleScopes.iterator().next().annotationType();
                return true;
            } else if (possibleScopes.size() > 1) {
            } else {
                return false;
            }
        }
        public BeanAttributes<T> build() {
        }
    }
New to GrepCode? Check out our FAQ X