Skip to content

Commit

Permalink
Allow the model to take null if the bean isn't specified in xml or java
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@250 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Nov 5, 2008
1 parent 0412756 commit 4a9990f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 89 deletions.
Expand Up @@ -33,12 +33,12 @@ public MergedStereotypesModel(AnnotatedItem<T, E> annotatedItem, AnnotatedItem<T
requiredTypes = new HashSet<Class<?>>();
supportedScopes = new HashSet<Class<? extends Annotation>>();

if (xmlAnnotatedItem.getAnnotations(Stereotype.class).size() > 0)
if (xmlAnnotatedItem != null && xmlAnnotatedItem.getAnnotations(Stereotype.class).size() > 0)
{
merge(xmlAnnotatedItem.getAnnotations(Stereotype.class), manager);
isDeclaredInXml = true;
}
else
else if (annotatedItem != null)
{
merge(annotatedItem.getAnnotations(Stereotype.class), manager);
}
Expand Down
@@ -1,7 +1,6 @@
package org.jboss.webbeans.model.bean;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -24,7 +23,6 @@
import org.jboss.webbeans.injectable.InjectableMethod;
import org.jboss.webbeans.injectable.InjectableParameter;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.impl.SimpleAnnotatedClass;
import org.jboss.webbeans.model.MergedStereotypesModel;
import org.jboss.webbeans.util.LoggerUtil;
import org.jboss.webbeans.util.Reflections;
Expand Down Expand Up @@ -67,10 +65,6 @@ protected AbstractClassBeanModel<? extends T> getSpecializedType() {
throw new UnsupportedOperationException();
}

protected static <T> SimpleAnnotatedClass getEmptyAnnotatedType(Class<T> type) {
return new SimpleAnnotatedClass<T>(type, new HashMap<Class<? extends Annotation>, Annotation>());
}

protected void initInjectionPoints()
{
injectionPoints = new HashSet<Injectable<?,?>>();
Expand Down Expand Up @@ -116,42 +110,43 @@ protected Set<Class<?>> getTypeHierachy(Class<?> clazz)

protected void initBindingTypes()
{
Set<Annotation> bindingTypes = getAnnotatedItem().getAnnotations(BindingType.class);
Set<Annotation> xmlBindingTypes = getXmlAnnotatedItem().getAnnotations(BindingType.class);
boolean xmlSpecialization = getXmlAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean specialization = getAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean xmlSpecialization = getXmlAnnotatedItem() == null ? false : getXmlAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean specialization = getAnnotatedItem() == null ? false : getAnnotatedItem().isAnnotationPresent(Specializes.class);

if (xmlBindingTypes.size() > 0 || mergedStereotypes.isDeclaredInXml())
this.bindingTypes = new HashSet<Annotation>();

if (getXmlAnnotatedItem() != null && getXmlAnnotatedItem().getAnnotations(BindingType.class).size() > 0)
{
// TODO support producer expression default binding type
this.bindingTypes.addAll(getXmlAnnotatedItem().getAnnotations(BindingType.class));
if (xmlSpecialization)
{
xmlBindingTypes.addAll(bindingTypes);
log.finest("Using binding types " + xmlBindingTypes + " specified in XML and specialized type");
this.bindingTypes.addAll(bindingTypes);
log.finest("Using binding types " + this.bindingTypes + " specified in XML and specialized type");
}
else {
log.finest("Using binding types " + xmlBindingTypes + " specified in XML");
else
{
log.finest("Using binding types " + this.bindingTypes + " specified in XML");
}
this.bindingTypes= xmlBindingTypes;
return;
}
else
else if (!mergedStereotypes.isDeclaredInXml() && getAnnotatedItem() != null)
{
this.bindingTypes.addAll(getAnnotatedItem().getAnnotations(BindingType.class));
if (specialization)
{
bindingTypes.addAll(getSpecializedType().getBindingTypes());
this.bindingTypes.addAll(getSpecializedType().getBindingTypes());
log.finest("Using binding types " + bindingTypes + " specified by annotations and specialized supertype");
}
else if (bindingTypes.size() == 0)
{
log.finest("Adding default @Current binding type");
bindingTypes.add(new CurrentAnnotationLiteral());
this.bindingTypes.add(new CurrentAnnotationLiteral());
}
else
{
log.finest("Using binding types " + bindingTypes + " specified by annotations");
}
this.bindingTypes = bindingTypes;
return;
}
}
Expand All @@ -161,30 +156,35 @@ else if (bindingTypes.size() == 0)
*/
protected void initScopeType()
{
Set<Annotation> xmlScopes = getXmlAnnotatedItem().getAnnotations(ScopeType.class);
if (xmlScopes.size() > 1)
{
throw new DefinitionException("At most one scope may be specified in XML");
}

if (xmlScopes.size() == 1)
if (getXmlAnnotatedItem() != null)
{
this.scopeType = xmlScopes.iterator().next().annotationType();
log.finest("Scope " + scopeType + " specified in XML");
return;
}

Set<Annotation> scopes = getAnnotatedItem().getAnnotations(ScopeType.class);
if (scopes.size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
if (getXmlAnnotatedItem().getAnnotations(ScopeType.class).size() > 1)
{
throw new DefinitionException("At most one scope may be specified in XML");
}

if (getXmlAnnotatedItem().getAnnotations(ScopeType.class).size() == 1)
{
this.scopeType = getXmlAnnotatedItem().getAnnotations(ScopeType.class).iterator().next().annotationType();
log.finest("Scope " + scopeType + " specified in XML");
return;
}
}

if (scopes.size() == 1)
if (getAnnotatedItem() != null)
{
this.scopeType = scopes.iterator().next().annotationType();
log.finest("Scope " + scopeType + " specified b annotation");
return;
if (getAnnotatedItem().getAnnotations(ScopeType.class).size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
}

if (getAnnotatedItem().getAnnotations(ScopeType.class).size() == 1)
{
this.scopeType = getAnnotatedItem().getAnnotations(ScopeType.class).iterator().next().annotationType();
log.finest("Scope " + scopeType + " specified b annotation");
return;
}
}

if (getMergedStereotypes().getPossibleScopeTypes().size() == 1)
Expand All @@ -203,10 +203,10 @@ else if (getMergedStereotypes().getPossibleScopeTypes().size() > 1)

protected void initName()
{
boolean xmlSpecialization = getXmlAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean specialization = getAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean xmlSpecialization = getXmlAnnotatedItem() == null ? false : getXmlAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean specialization = getAnnotatedItem() == null ? false : getAnnotatedItem().isAnnotationPresent(Specializes.class);
boolean beanNameDefaulted = false;
if (getXmlAnnotatedItem().isAnnotationPresent(Named.class))
if (getXmlAnnotatedItem() != null && getXmlAnnotatedItem().isAnnotationPresent(Named.class))
{
if (xmlSpecialization)
{
Expand All @@ -225,7 +225,7 @@ protected void initName()
return;
}
}
else if (getAnnotatedItem().isAnnotationPresent(Named.class))
else if (getAnnotatedItem() != null && getAnnotatedItem().isAnnotationPresent(Named.class))
{
if (specialization)
{
Expand Down Expand Up @@ -259,48 +259,48 @@ else if (specialization)

protected void initDeploymentType()
{
Set<Annotation> xmlDeploymentTypes = getXmlAnnotatedItem().getAnnotations(DeploymentType.class);

if (xmlDeploymentTypes.size() > 1)
{
throw new RuntimeException("At most one deployment type may be specified (" + xmlDeploymentTypes + " are specified)");
}

if (xmlDeploymentTypes.size() == 1)
{
this.deploymentType = xmlDeploymentTypes.iterator().next().annotationType();
log.finest("Deployment type " + deploymentType + " specified in XML");
return;
}


Set<Annotation> deploymentTypes = getAnnotatedItem().getAnnotations(DeploymentType.class);

if (deploymentTypes.size() > 1)
{
throw new DefinitionException("At most one deployment type may be specified (" + deploymentTypes + " are specified) on " + getAnnotatedItem().toString());
}
if (deploymentTypes.size() == 1)
if (getXmlAnnotatedItem() != null)
{
this.deploymentType = deploymentTypes.iterator().next().annotationType();
log.finest("Deployment type " + deploymentType + " specified by annotation");
return;
Set<Annotation> xmlDeploymentTypes = getXmlAnnotatedItem().getAnnotations(DeploymentType.class);
if (xmlDeploymentTypes.size() > 1)
{
throw new RuntimeException("At most one deployment type may be specified (" + xmlDeploymentTypes + " are specified)");
}

if (xmlDeploymentTypes.size() == 1)
{
this.deploymentType = xmlDeploymentTypes.iterator().next().annotationType();
log.finest("Deployment type " + deploymentType + " specified in XML");
return;
}
}

if (getMergedStereotypes().getPossibleDeploymentTypes().size() > 0)
if (getAnnotatedItem() != null)
{
this.deploymentType = getDeploymentType(container.getEnabledDeploymentTypes(), getMergedStereotypes().getPossibleDeploymentTypes());
log.finest("Deployment type " + deploymentType + " specified by stereotype");
return;
Set<Annotation> deploymentTypes = getAnnotatedItem().getAnnotations(DeploymentType.class);

if (deploymentTypes.size() > 1)
{
throw new DefinitionException("At most one deployment type may be specified (" + deploymentTypes + " are specified) on " + getAnnotatedItem().toString());
}
if (deploymentTypes.size() == 1)
{
this.deploymentType = deploymentTypes.iterator().next().annotationType();
log.finest("Deployment type " + deploymentType + " specified by annotation");
return;
}

if (getMergedStereotypes().getPossibleDeploymentTypes().size() > 0)
{
this.deploymentType = getDeploymentType(container.getEnabledDeploymentTypes(), getMergedStereotypes().getPossibleDeploymentTypes());
log.finest("Deployment type " + deploymentType + " specified by stereotype");
return;
}
}

// TODO This isn't the right way to check if XML isn't in use
if (getXmlAnnotatedItem().getDelegate() != null)
{
this.deploymentType = Production.class;
log.finest("Using default @Production deployment type");
return;
}
this.deploymentType = Production.class;
log.finest("Using default @Production deployment type");
return;
}

protected void checkDeploymentType()
Expand All @@ -309,8 +309,7 @@ protected void checkDeploymentType()
{
throw new RuntimeException("type: " + getType() + " must specify a deployment type");
}

if (deploymentType.equals(Standard.class))
else if (deploymentType.equals(Standard.class))
{
throw new DefinitionException();
}
Expand Down
Expand Up @@ -79,16 +79,16 @@ protected AnnotatedClass<T> getXmlAnnotatedItem()
protected void initType()
{
// TODO This is not the right way to check XML definition
if (getAnnotatedItem().getDelegate() != null && getXmlAnnotatedItem().getDelegate() != null && !getAnnotatedItem().getDelegate().equals(getXmlAnnotatedItem().getDelegate()))
if (getAnnotatedItem() != null && getXmlAnnotatedItem() != null && !getAnnotatedItem().getDelegate().equals(getXmlAnnotatedItem().getDelegate()))
{
throw new IllegalArgumentException("Cannot build a bean which specifies different classes in XML and Java");
}
else if (getXmlAnnotatedItem().getDelegate() != null)
else if (getXmlAnnotatedItem() != null)
{
log.finest("Bean type specified in XML");
this.type = getXmlAnnotatedItem().getDelegate();
}
else if (getAnnotatedItem().getDelegate() != null)
else if (getAnnotatedItem() != null)
{
log.finest("Bean type specified in Java");
this.type = getAnnotatedItem().getDelegate();
Expand Down
Expand Up @@ -169,7 +169,7 @@ protected AbstractClassBeanModel<? extends T> getSpecializedType()
Class<?> superclass = getAnnotatedItem().getType().getSuperclass();
if ( superclass!=null )
{
return new EnterpriseBeanModel(new SimpleAnnotatedClass(superclass), getEmptyAnnotatedType(superclass), container);
return new EnterpriseBeanModel(new SimpleAnnotatedClass(superclass), null, container);
}
else {
throw new RuntimeException();
Expand Down
Expand Up @@ -129,7 +129,7 @@ protected AbstractClassBeanModel<? extends T> getSpecializedType()
Class<?> superclass = getAnnotatedItem().getType().getSuperclass();
if ( superclass!=null )
{
return new SimpleBeanModel(new SimpleAnnotatedClass(superclass), getEmptyAnnotatedType(superclass), container);
return new SimpleBeanModel(new SimpleAnnotatedClass(superclass), null, container);
}
else {
throw new RuntimeException();
Expand Down
Expand Up @@ -19,7 +19,7 @@ public static <T> SimpleBeanImpl<T> createSimpleWebBean(Class<T> clazz, ManagerI

public static <T> SimpleBeanModel<T> createSimpleModel(Class<T> clazz, ManagerImpl manager)
{
return new SimpleBeanModel<T>(new SimpleAnnotatedClass<T>(clazz), getEmptyAnnotatedType(clazz), manager);
return new SimpleBeanModel<T>(new SimpleAnnotatedClass<T>(clazz), null, manager);
}

public static <T> SimpleBeanModel<T> createSimpleModel(Class<T> clazz, AnnotatedClass<T> xmlAnnotatedType, ManagerImpl manager)
Expand All @@ -37,6 +37,7 @@ public static <T> EnterpriseBeanModel<T> createEnterpriseBeanModel(Class<T> claz
return new EnterpriseBeanModel<T>(new SimpleAnnotatedClass<T>(clazz), xmlAnnotatedType, manager);
}

@Deprecated
public static <T> AnnotatedClass<T> getEmptyAnnotatedType(Class<T> type)
{
return new SimpleAnnotatedClass<T>(type, new HashMap<Class<? extends Annotation>, Annotation>());
Expand Down

0 comments on commit 4a9990f

Please sign in to comment.