Skip to content

Commit

Permalink
Fix scope inheritance
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1083 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jan 19, 2009
1 parent 86e82c0 commit c7cb3b1
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 111 deletions.
8 changes: 4 additions & 4 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -261,7 +261,7 @@ public void setEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledD
*/
public <T> Set<Bean<T>> resolveByType(Class<T> type, Annotation... bindings)
{
return resolveByType(new AnnotatedClassImpl<T>(type, type, bindings), bindings);
return resolveByType(AnnotatedClassImpl.of(type, bindings), bindings);
}

/**
Expand All @@ -276,7 +276,7 @@ public <T> Set<Bean<T>> resolveByType(Class<T> type, Annotation... bindings)
*/
public <T> Set<Bean<T>> resolveByType(TypeLiteral<T> type, Annotation... bindings)
{
return resolveByType(new AnnotatedClassImpl<T>(type.getRawType(), type.getType(), bindings), bindings);
return resolveByType(AnnotatedClassImpl.of(type, bindings), bindings);
}

/**
Expand Down Expand Up @@ -564,7 +564,7 @@ else if (beans.size() > 1)
*/
public <T> T getInstanceByType(Class<T> type, Annotation... bindings)
{
return getInstanceByType(new AnnotatedClassImpl<T>(type, type, bindings), bindings);
return getInstanceByType(AnnotatedClassImpl.of(type, bindings), bindings);
}

public <T> T getMostSpecializedInstance(Bean<T> bean, boolean create)
Expand Down Expand Up @@ -592,7 +592,7 @@ public <T> T getMostSpecializedInstance(Bean<T> bean, boolean create)
*/
public <T> T getInstanceByType(TypeLiteral<T> type, Annotation... bindings)
{
return getInstanceByType(new AnnotatedClassImpl<T>(type.getRawType(), type.getType(), bindings), bindings);
return getInstanceByType(AnnotatedClassImpl.of(type, bindings), bindings);
}

/**
Expand Down
Expand Up @@ -32,7 +32,6 @@
import javax.webbeans.Event;
import javax.webbeans.InjectionPoint;
import javax.webbeans.Named;
import javax.webbeans.ScopeType;
import javax.webbeans.Specializes;
import javax.webbeans.Standard;
import javax.webbeans.Stereotype;
Expand Down Expand Up @@ -287,33 +286,25 @@ protected boolean injectionPointsAreSerializable()
/**
* Initializes the scope type
*/
protected void initScopeType()
protected abstract void initScopeType();

protected boolean initScopeFromStereotype()
{
Set<Annotation> scopeAnnotations = getAnnotatedItem().getMetaAnnotations(ScopeType.class);
if (scopeAnnotations.size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
}
if (scopeAnnotations.size() == 1)
{
this.scopeType = scopeAnnotations.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by annotation");
return;
}

Set<Annotation> possibleScopeTypes = getMergedStereotypes().getPossibleScopeTypes();
if (possibleScopeTypes.size() == 1)
{
this.scopeType = possibleScopeTypes.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by stereotype");
return;
return true;
}
else if (possibleScopeTypes.size() > 1)
{
throw new DefinitionException("All stereotypes must specify the same scope OR a scope must be specified on the bean");
}
this.scopeType = Dependent.class;
log.trace("Using default @Dependent scope");
else
{
return false;
}
}

/**
Expand Down
Expand Up @@ -23,12 +23,14 @@

import javax.webbeans.BindingType;
import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.Destructor;
import javax.webbeans.Disposes;
import javax.webbeans.Initializer;
import javax.webbeans.Observes;
import javax.webbeans.Produces;
import javax.webbeans.Production;
import javax.webbeans.ScopeType;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedClass;
Expand Down Expand Up @@ -150,6 +152,44 @@ else if (annotatedMethod.getAnnotatedParameters(Observes.class).size() > 0)
}
}
}

@Override
protected void initScopeType()
{
Set<Annotation> scopeAnnotations = getAnnotatedItem().getMetaAnnotations(ScopeType.class);
if (scopeAnnotations.size() == 1)
{
this.scopeType = scopeAnnotations.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by annotation");
return;
}
else if (scopeAnnotations.size() > 1)
{
for (AnnotatedClass<?> clazz = getAnnotatedItem(); clazz != null; clazz = clazz.getSuperclass())
{
scopeAnnotations = clazz.getDeclaredMetaAnnotations(ScopeType.class);
if (scopeAnnotations.size() == 1)
{
this.scopeType = scopeAnnotations.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by annotation");
return;
}
else if (scopeAnnotations.size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
}
}

}

initScopeFromStereotype();

if (this.scopeType == null)
{
this.scopeType = Dependent.class;
log.trace("Using default @Dependent scope");
}
}

/**
* Validate that the scope type is allowed by the stereotypes on the bean and
Expand Down
Expand Up @@ -21,17 +21,21 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.IllegalProductException;
import javax.webbeans.Initializer;
import javax.webbeans.Produces;
import javax.webbeans.ScopeType;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.MetaDataCache;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.injection.InjectionPointImpl;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.util.Names;
import org.jboss.webbeans.util.Reflections;

Expand All @@ -47,6 +51,8 @@ public abstract class AbstractProducerBean<T, S> extends AbstractBean<T, S>
{
// The declaring bean
protected AbstractClassBean<?> declaringBean;

private static final LogProvider log = Logging.getLogProvider(AbstractProducerBean.class);

/**
* Constructor
Expand Down Expand Up @@ -200,6 +206,30 @@ else if (injectionPoint.isConstructor())
}
}
}

@Override
protected void initScopeType()
{
Set<Annotation> scopeAnnotations = getAnnotatedItem().getMetaAnnotations(ScopeType.class);
if (scopeAnnotations.size() > 1)
{
throw new DefinitionException("At most one scope may be specified");
}
if (scopeAnnotations.size() == 1)
{
this.scopeType = scopeAnnotations.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by annotation");
return;
}

initScopeFromStereotype();

if (this.scopeType == null)
{
this.scopeType = Dependent.class;
log.trace("Using default @Dependent scope");
}
}

/**
* Gets the receiver of the product
Expand Down
Expand Up @@ -192,7 +192,7 @@ protected void registerBeans(Iterable<Class<?>> classes)

private <T> void createSimpleBean(Class<T> clazz, Set<AbstractBean<?, ?>> beans)
{
AnnotatedClass<T> annotatedClass = new AnnotatedClassImpl<T>(clazz);
AnnotatedClass<T> annotatedClass = AnnotatedClassImpl.of(clazz);
createBean(SimpleBean.of(annotatedClass, manager), annotatedClass, beans);
}

Expand Down
Expand Up @@ -108,9 +108,9 @@ public interface AnnotatedClass<T> extends AnnotatedType<T>
public Set<AnnotatedMethod<?>> getMethodsWithAnnotatedParameters(Class<? extends Annotation> annotationType);

/**
* Gets the superclass
* Gets the superclass.
*
* @return The abstracted superclass
* @return The abstracted superclass, null if there is no superclass
*/
public AnnotatedClass<?> getSuperclass();

Expand Down
Expand Up @@ -299,7 +299,7 @@ public Set<Annotation> getMetaAnnotations(Class<? extends Annotation> metaAnnota

public Set<Annotation> getDeclaredMetaAnnotations(Class<? extends Annotation> metaAnnotationType)
{
return Collections.unmodifiableSet(metaAnnotationMap.get(metaAnnotationType));
return Collections.unmodifiableSet(declaredMetaAnnotationMap.get(metaAnnotationType));
}

/**
Expand Down

0 comments on commit c7cb3b1

Please sign in to comment.