You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ar...@apache.org on 2013/01/06 16:46:12 UTC
svn commit: r1429543 - in
/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans:
component/AbstractOwbBean.java component/creation/AbstractBeanCreator.java
component/creation/AbstractInjecionTargetBeanCreator.java
util/AnnotationUtil.java
Author: arne
Date: Sun Jan 6 15:46:11 2013
New Revision: 1429543
URL: http://svn.apache.org/viewvc?rev=1429543&view=rev
Log:
OWB-745: Delayed setting of bean name and qualifiers
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractBeanCreator.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjecionTargetBeanCreator.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java?rev=1429543&r1=1429542&r2=1429543&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java Sun Jan 6 15:46:11 2013
@@ -443,7 +443,7 @@ public abstract class AbstractOwbBean<T>
this.name = name;
cachedHashCode = 0;
}
- else
+ else if (!this.name.equals(name))
{
throw new UnsupportedOperationException("Component name is already set to: " + this.name);
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractBeanCreator.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractBeanCreator.java?rev=1429543&r1=1429542&r2=1429543&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractBeanCreator.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractBeanCreator.java Sun Jan 6 15:46:11 2013
@@ -22,11 +22,13 @@ import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.NormalScope;
+import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.InjectionPoint;
@@ -65,7 +67,13 @@ public class AbstractBeanCreator<T>
private final AbstractOwbBean<T> bean;
private Annotated annotated;
+
+ private WebBeansContext webBeansContext;
+
+ private String beanName;
+ private Set<Annotation> qualifiers = new HashSet<Annotation>();
+
/**
* Creates a bean instance.
*
@@ -76,6 +84,12 @@ public class AbstractBeanCreator<T>
{
this.bean = bean;
this.annotated = annotated;
+ this.webBeansContext = bean.getWebBeansContext();
+ }
+
+ protected Set<Annotation> getQualifiers()
+ {
+ return qualifiers;
}
/**
@@ -121,7 +135,7 @@ public class AbstractBeanCreator<T>
if (nameAnnot == null) // no @Named
{
// Check for stereottype
- if (getBean().getWebBeansContext().getAnnotationManager().hasNamedOnStereoTypes(getBean()))
+ if (webBeansContext.getAnnotationManager().hasNamedOnStereoTypes(getBean()))
{
isDefault = true;
}
@@ -136,14 +150,14 @@ public class AbstractBeanCreator<T>
}
else
{
- getBean().setName(nameAnnot.value());
+ beanName = nameAnnot.value();
}
}
if (isDefault)
{
- getBean().setName(name);
+ beanName = name;
}
}
@@ -153,7 +167,7 @@ public class AbstractBeanCreator<T>
public void defineQualifiers()
{
Annotation[] annotations = AnnotationUtil.asSet(annotated.getAnnotations());
- final AnnotationManager annotationManager = getBean().getWebBeansContext().getAnnotationManager();
+ final AnnotationManager annotationManager = webBeansContext.getAnnotationManager();
for (Annotation annotation : annotations)
{
@@ -161,7 +175,7 @@ public class AbstractBeanCreator<T>
if (annotationManager.isQualifierAnnotation(type))
{
- Method[] methods = getBean().getWebBeansContext().getSecurityService().doPrivilegedGetDeclaredMethods(type);
+ Method[] methods = webBeansContext.getSecurityService().doPrivilegedGetDeclaredMethods(type);
for (Method method : methods)
{
@@ -170,77 +184,64 @@ public class AbstractBeanCreator<T>
{
if (!AnnotationUtil.hasAnnotation(method.getDeclaredAnnotations(), Nonbinding.class))
{
- throw new WebBeansConfigurationException("WebBeans definition class : " + getBean().getReturnType().getName() + " @Qualifier : "
+ throw new WebBeansConfigurationException("WebBeans definition class : " + method.getDeclaringClass().getName() + " @Qualifier : "
+ annotation.annotationType().getName()
+ " must have @NonBinding valued members for its array-valued and annotation valued members");
}
}
}
- if (annotation.annotationType().equals(Named.class) && getBean().getName() != null)
+ if (annotation.annotationType().equals(Named.class) && beanName != null)
{
- getBean().addQualifier(new NamedLiteral(getBean().getName()));
+ qualifiers.add(new NamedLiteral(beanName));
}
else
{
- getBean().addQualifier(annotation);
- }
- }
- }
-
- // Adding inherited qualifiers
- IBeanInheritedMetaData inheritedMetaData = null;
-
- if(getBean() instanceof InjectionTargetBean)
- {
- inheritedMetaData = ((InjectionTargetBean<?>) getBean()).getInheritedMetaData();
- }
-
- if (inheritedMetaData != null)
- {
- Set<Annotation> inheritedTypes = inheritedMetaData.getInheritedQualifiers();
- for (Annotation inherited : inheritedTypes)
- {
- Set<Annotation> qualifiers = getBean().getQualifiers();
- boolean found = false;
- for (Annotation existQualifier : qualifiers)
- {
- if (existQualifier.annotationType().equals(inherited.annotationType()))
- {
- found = true;
- break;
- }
- }
- if (!found)
- {
- getBean().addQualifier(inherited);
+ qualifiers.add(annotation);
}
}
}
+ configureInheritedQualifiers();
// No-binding annotation
- if (getBean().getQualifiers().size() == 0 )
+ if (qualifiers.size() == 0 )
{
- getBean().addQualifier(new DefaultLiteral());
+ qualifiers.add(new DefaultLiteral());
}
- else if(getBean().getQualifiers().size() == 1)
+ else if(qualifiers.size() == 1)
{
- Annotation annot = getBean().getQualifiers().iterator().next();
+ Annotation annot = qualifiers.iterator().next();
if(annot.annotationType().equals(Named.class))
{
- getBean().addQualifier(new DefaultLiteral());
+ qualifiers.add(new DefaultLiteral());
}
}
//Add @Any support
- if(!AnnotationUtil.hasAnyQualifier(getBean()))
+ if(!hasAnyQualifier())
{
- getBean().addQualifier(new AnyLiteral());
+ qualifiers.add(new AnyLiteral());
}
}
+ protected void configureInheritedQualifiers()
+ {
+ // hook for subclasses
+ }
+
+ /**
+ * Returns true if any binding exist
+ *
+ * @param bean bean
+ * @return true if any binding exist
+ */
+ private boolean hasAnyQualifier()
+ {
+ return AnnotationUtil.getAnnotation(qualifiers, Any.class) != null;
+ }
+
/**
* {@inheritDoc}
*/
@@ -249,7 +250,7 @@ public class AbstractBeanCreator<T>
Annotation[] annotations = AnnotationUtil.asSet(annotated.getAnnotations());
boolean found = false;
- List<ExternalScope> additionalScopes = getBean().getWebBeansContext().getBeanManagerImpl().getAdditionalScopes();
+ List<ExternalScope> additionalScopes = webBeansContext.getBeanManagerImpl().getAdditionalScopes();
for (Annotation annotation : annotations)
{
@@ -396,7 +397,7 @@ public class AbstractBeanCreator<T>
{
getBean().setImplScopeType(new DependentScopeLiteral());
- if (allowLazyInit && getBean() instanceof ManagedBean && isPurePojoBean(getBean().getWebBeansContext(), getBean().getBeanClass()))
+ if (allowLazyInit && getBean() instanceof ManagedBean && isPurePojoBean(webBeansContext, getBean().getBeanClass()))
{
// take the bean as Dependent but we could lazily initialize it
// because the bean doesn't contains any CDI feature
@@ -525,6 +526,8 @@ public class AbstractBeanCreator<T>
*/
public AbstractOwbBean<T> getBean()
{
+ bean.setName(beanName);
+ bean.getQualifiers().addAll(qualifiers);
return bean;
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjecionTargetBeanCreator.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjecionTargetBeanCreator.java?rev=1429543&r1=1429542&r2=1429543&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjecionTargetBeanCreator.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/creation/AbstractInjecionTargetBeanCreator.java Sun Jan 6 15:46:11 2013
@@ -54,6 +54,7 @@ import org.apache.webbeans.component.Pro
import org.apache.webbeans.component.ProducerMethodBean;
import org.apache.webbeans.component.ResourceBean;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.config.inheritance.IBeanInheritedMetaData;
import org.apache.webbeans.container.InjectionResolver;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.spi.api.ResourceReference;
@@ -507,7 +508,7 @@ public abstract class AbstractInjecionTa
producerFieldBeanCreator.defineQualifiers();
producerFieldBeanCreator.defineName(WebBeansUtil.getProducerDefaultName(annotatedField.getJavaMember().getName()));
- producerBeans.add(producerFieldBean);
+ producerBeans.add(producerFieldBeanCreator.getBean());
}
}
}
@@ -578,7 +579,7 @@ public abstract class AbstractInjecionTa
producerMethodBeanCreator.defineQualifiers();
producerMethodBeanCreator.addMethodInjectionPointMetaData(annotatedMethod);
- producerBeans.add(producerMethodBean);
+ producerBeans.add(producerMethodBeanCreator.getBean());
}
@@ -627,4 +628,31 @@ public abstract class AbstractInjecionTa
{
return (AnnotatedType<T>) super.getAnnotated();
}
+
+ protected void configureInheritedQualifiers()
+ {
+ // Adding inherited qualifiers
+ IBeanInheritedMetaData inheritedMetaData = getBean().getInheritedMetaData();
+
+ if (inheritedMetaData != null)
+ {
+ Set<Annotation> inheritedTypes = inheritedMetaData.getInheritedQualifiers();
+ for (Annotation inherited : inheritedTypes)
+ {
+ boolean found = false;
+ for (Annotation existQualifier : getQualifiers())
+ {
+ if (existQualifier.annotationType().equals(inherited.annotationType()))
+ {
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ getQualifiers().add(inherited);
+ }
+ }
+ }
+ }
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java?rev=1429543&r1=1429542&r2=1429543&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java Sun Jan 6 15:46:11 2013
@@ -28,10 +28,8 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
-import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedParameter;
-import javax.enterprise.inject.spi.Bean;
import javax.enterprise.util.Nonbinding;
import org.apache.webbeans.exception.WebBeansException;
@@ -599,21 +597,6 @@ public final class AnnotationUtil
return result;
}
-
- /**
- * Returns true if any binding exist
- *
- * @param bean bean
- * @return true if any binding exist
- */
- public static boolean hasAnyQualifier(Bean<?> bean)
- {
- Asserts.assertNotNull(bean, "bean parameter can not be null");
- Set<Annotation> qualifiers = bean.getQualifiers();
-
- return getAnnotation(qualifiers, Any.class) != null;
- }
-
/**
* Search in the given Set of Annotations for the one with the given AnnotationClass.
* @param annotations to scan