Author: pete.muir(a)jboss.org
Date: 2008-06-25 06:50:10 -0400 (Wed, 25 Jun 2008)
New Revision: 15
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/CurrentBinding.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/EnhancedAnnotatedElement.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/MutableEnhancedAnnotatedElement.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/MutableEnhancedAnnotatedElementTest.java
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Order.java
Modified:
ri/trunk/webbeans-impl/pom.xml
Log:
Start WBRI-3 - component meta model and WBRI-13
Modified: ri/trunk/webbeans-impl/pom.xml
===================================================================
--- ri/trunk/webbeans-impl/pom.xml 2008-06-25 10:48:42 UTC (rev 14)
+++ ri/trunk/webbeans-impl/pom.xml 2008-06-25 10:50:10 UTC (rev 15)
@@ -1,21 +1,26 @@
-<?xml version="1.0"?><project>
- <parent>
- <artifactId>parent</artifactId>
- <groupId>org.jboss.webbeans</groupId>
- <version>1.0.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.jboss.webbeans</groupId>
- <artifactId>webbeans-impl</artifactId>
- <name>webbeans-impl</name>
- <version>1.0-SNAPSHOT</version>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+<?xml version="1.0"?>
+<project>
+ <parent>
+ <artifactId>parent</artifactId>
+ <groupId>org.jboss.webbeans</groupId>
+ <version>1.0.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.jboss.webbeans</groupId>
+ <artifactId>webbeans-impl</artifactId>
+ <name>Web Beans RI</name>
+ <dependencies>
+
+ <dependency>
+ <groupId>org.jboss.webbeans</groupId>
+ <artifactId>webbeans-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
</project>
\ No newline at end of file
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,152 @@
+package org.jboss.webbeans;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.Arrays;
+import java.util.Set;
+
+import javax.webbeans.BindingType;
+import javax.webbeans.ComponentInstance;
+import javax.webbeans.Container;
+import javax.webbeans.Current;
+import javax.webbeans.DeploymentType;
+import javax.webbeans.Named;
+import javax.webbeans.ScopeType;
+import javax.webbeans.Stereotype;
+
+import org.jboss.webbeans.util.EnhancedAnnotatedElement;
+
+/**
+ * Web Beans Component meta model
+ *
+ * @author Pete Muir
+ *
+ */
+public class ComponentInstanceImpl<T> extends ComponentInstance<T>
+{
+
+ private Set<Annotation> bindingTypes;
+ private Annotation componentType;
+ private String name;
+ private Annotation scopeType;
+ private Set<Annotation> stereotypes;
+
+ public ComponentInstanceImpl(EnhancedAnnotatedElement annotatedElement)
+ {
+ initSterotypes(annotatedElement);
+ initBindingTypes(annotatedElement);
+ initComponentType(annotatedElement);
+ initScopeType(annotatedElement);
+ initName(annotatedElement);
+ }
+
+ private void initSterotypes(EnhancedAnnotatedElement annotatedElement)
+ {
+ this.stereotypes = annotatedElement.getAnnotations(Stereotype.class);
+ }
+
+ private void initScopeType(EnhancedAnnotatedElement annotatedElement)
+ {
+ Set<Annotation> scopes = annotatedElement.getAnnotations(ScopeType.class);
+ if (scopes.size() > 1)
+ {
+ throw new RuntimeException("At most one scope may be specified");
+ }
+ else if (scopes.size() == 1)
+ {
+ this.scopeType = scopes.iterator().next();
+ }
+ else
+ {
+ // TODO Look at sterotypes
+ }
+ }
+
+ private void initComponentType(EnhancedAnnotatedElement annotatedElement)
+ {
+ Set<Annotation> deploymentTypes =
annotatedElement.getAnnotations(DeploymentType.class);
+ if (deploymentTypes.size() > 1)
+ {
+ throw new RuntimeException("At most one deployment type may be
specified");
+ }
+ else if (deploymentTypes.size() == 1)
+ {
+ this.componentType = deploymentTypes.iterator().next();
+ }
+ else
+ {
+ // TODO Look at sterotypes
+ }
+ }
+
+ private void initBindingTypes(EnhancedAnnotatedElement annotatedElement)
+ {
+ bindingTypes = annotatedElement.getAnnotations(BindingType.class);
+
+ // Add the default binding if needed
+ if (bindingTypes.size() == 0)
+ {
+ bindingTypes.add(new CurrentBinding());
+ }
+ }
+
+ private void initName(EnhancedAnnotatedElement annotatedElement)
+ {
+ if (annotatedElement.isAnnotationPresent(Named.class))
+ {
+ String name = annotatedElement.getAnnotation(Named.class).value();
+ if ("".equals(name))
+ {
+ // TODO write default name algorithm
+
+ }
+ this.name = name;
+ }
+ }
+
+ @Override
+ public T create(Container container)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void destroy(Container container, Object instance)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public Set<Annotation> getBindingTypes()
+ {
+ return bindingTypes;
+ }
+
+ @Override
+ public Annotation getComponentType()
+ {
+ return componentType;
+ }
+
+ @Override
+ public String getName()
+ {
+ return name;
+ }
+
+ @Override
+ public Set<Class> getTypes()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Annotation getScopeType()
+ {
+ return scopeType;
+ }
+
+}
Property changes on:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ComponentInstanceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/CurrentBinding.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/CurrentBinding.java
(rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/CurrentBinding.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,6 @@
+package org.jboss.webbeans;
+
+import javax.webbeans.Current;
+import javax.webbeans.DynamicBinding;
+
+public class CurrentBinding extends DynamicBinding<Current> implements Current {}
Property changes on:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/CurrentBinding.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/EnhancedAnnotatedElement.java
===================================================================
---
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/EnhancedAnnotatedElement.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/EnhancedAnnotatedElement.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,18 @@
+package org.jboss.webbeans.util;
+
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+public interface EnhancedAnnotatedElement
+{
+
+ public abstract <T extends Annotation> Set<T> getAnnotations();
+
+ public abstract <T extends Annotation> Set<Annotation> getAnnotations(
+ Class<T> metaAnnotationType);
+
+ public <T extends Annotation> T getAnnotation(Class<T> arg0);
+
+ public boolean isAnnotationPresent(Class<? extends Annotation> arg0);
+
+}
\ No newline at end of file
Property changes on:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/EnhancedAnnotatedElement.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/MutableEnhancedAnnotatedElement.java
===================================================================
---
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/MutableEnhancedAnnotatedElement.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/MutableEnhancedAnnotatedElement.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,113 @@
+package org.jboss.webbeans.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+/**
+ * Helper class which allows us to store the annotations present on an object
+ *
+ * Also allows you to discover meta annotations
+ *
+ * @author pmuir
+ *
+ */
+public class MutableEnhancedAnnotatedElement implements EnhancedAnnotatedElement
+{
+
+ private Map<Class<? extends Annotation>, Annotation> annotations;
+
+ private Map<Class<? extends Annotation>, Set<Annotation>>
metaAnnotations;
+ private Set<Annotation> annotationSet;
+
+ public MutableEnhancedAnnotatedElement()
+ {
+ this.annotations = new HashMap<Class<? extends Annotation>,
Annotation>();
+ }
+
+ public MutableEnhancedAnnotatedElement(AnnotatedElement annotatedElement)
+ {
+ this();
+ for (Annotation annotation : annotatedElement.getAnnotations())
+ {
+ add(annotation);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends Annotation> T getAnnotation(Class<T> annotationType)
+ {
+ return (T) annotations.get(annotationType);
+ }
+
+ private void setDirty()
+ {
+ metaAnnotations = null;
+ this.annotationSet = null;
+ }
+
+ public <T extends Annotation> Set<Annotation>
getAnnotations(Class<T> metaAnnotationType)
+ {
+ if (metaAnnotations == null)
+ {
+ metaAnnotations = new HashMap<Class<? extends Annotation>,
Set<Annotation>>();
+ }
+ if (!metaAnnotations.containsKey(metaAnnotationType))
+ {
+ Set<Annotation> s = new HashSet<Annotation>();
+ for (Entry<Class<? extends Annotation>, Annotation> entry :
annotations.entrySet())
+ {
+ if
(entry.getValue().annotationType().isAnnotationPresent(metaAnnotationType))
+ {
+ s.add(entry.getValue());
+ }
+ }
+ metaAnnotations.put(metaAnnotationType, s);
+ }
+ return metaAnnotations.get(metaAnnotationType);
+ }
+
+
+ public Set<Annotation> getAnnotations()
+ {
+ if (annotationSet == null)
+ {
+ annotationSet = new HashSet<Annotation>();
+ for (Entry<Class<? extends Annotation>, Annotation> entry :
annotations.entrySet())
+ {
+ annotationSet.add(entry.getValue());
+ }
+ }
+ return annotationSet;
+ }
+
+ public boolean isAnnotationPresent(Class<? extends Annotation> annotatedType)
+ {
+ return annotations.containsKey(annotatedType);
+ }
+
+ /**
+ * Add an annotation to the AnnotatedElement
+ * @param annotation
+ */
+ public void add(Annotation annotation)
+ {
+ setDirty();
+ annotations.put(annotation.annotationType(), annotation);
+ }
+
+ public void addAll(Collection<Annotation> annotations)
+ {
+ for (Annotation annotation : annotations)
+ {
+ this.annotations.put(annotation.annotationType(), annotation);
+ }
+ setDirty();
+ }
+
+}
Property changes on:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/util/MutableEnhancedAnnotatedElement.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
===================================================================
---
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,22 @@
+package org.jboss.webbeans.test;
+
+import javax.webbeans.ComponentInstance;
+import javax.webbeans.Production;
+
+import org.jboss.webbeans.ComponentInstanceImpl;
+import org.jboss.webbeans.test.components.Order;
+import org.jboss.webbeans.util.MutableEnhancedAnnotatedElement;
+import org.junit.Test;
+
+public class ComponentInstanceTest
+{
+
+ @Test
+ public void testMetaModel()
+ {
+ ComponentInstance<Order> order = new ComponentInstanceImpl<Order>(new
MutableEnhancedAnnotatedElement(Order.class));
+ assert Production.class.equals(order.getComponentType().annotationType());
+ assert "order".equals(order.getName());
+ }
+
+}
Property changes on:
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/ComponentInstanceTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/MutableEnhancedAnnotatedElementTest.java
===================================================================
---
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/MutableEnhancedAnnotatedElementTest.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/MutableEnhancedAnnotatedElementTest.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,66 @@
+package org.jboss.webbeans.test;
+
+import java.lang.annotation.Annotation;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.webbeans.Current;
+import javax.webbeans.DeploymentType;
+import javax.webbeans.Named;
+import javax.webbeans.Production;
+import javax.webbeans.ScopeType;
+
+import org.jboss.webbeans.CurrentBinding;
+import org.jboss.webbeans.test.components.Order;
+import org.jboss.webbeans.util.EnhancedAnnotatedElement;
+import org.jboss.webbeans.util.MutableEnhancedAnnotatedElement;
+import org.junit.Test;
+
+public class MutableEnhancedAnnotatedElementTest
+{
+
+ @Test
+ public void testDeclaredAnnotations()
+ {
+ EnhancedAnnotatedElement annotatedElement = new
MutableEnhancedAnnotatedElement(Order.class);
+ assert annotatedElement.getAnnotations().size() == 2;
+ assert annotatedElement.getAnnotation(Production.class) != null;
+ assert annotatedElement.getAnnotation(Named.class) != null;
+ }
+
+ @Test
+ public void testMutability()
+ {
+ MutableEnhancedAnnotatedElement annotatedElement = new
MutableEnhancedAnnotatedElement(Order.class);
+ assert annotatedElement.getAnnotations().size() == 2;
+ annotatedElement.add(new CurrentBinding());
+ assert annotatedElement.getAnnotations().size() == 3;
+ assert annotatedElement.getAnnotation(Production.class) != null;
+ assert annotatedElement.getAnnotation(Named.class) != null;
+ assert annotatedElement.getAnnotation(Current.class) != null;
+ }
+
+ @Test
+ public void testMetaAnnotations()
+ {
+ EnhancedAnnotatedElement annotatedElement = new
MutableEnhancedAnnotatedElement(Order.class);
+ Set<Annotation> annotations =
annotatedElement.getAnnotations(DeploymentType.class);
+ assert annotations.size() == 1;
+ Iterator<Annotation> it = annotations.iterator();
+ Annotation production = it.next();
+ assert Production.class.equals(production.annotationType());
+ }
+
+ @Test
+ public void testMutableMetaAnnotations()
+ {
+ MutableEnhancedAnnotatedElement annotatedElement = new
MutableEnhancedAnnotatedElement(Order.class);
+ annotatedElement.add(new CurrentBinding());
+ Set<Annotation> annotations =
annotatedElement.getAnnotations(ScopeType.class);
+ assert annotations.size() == 1;
+ Iterator<Annotation> it = annotations.iterator();
+ Annotation production = it.next();
+ assert Current.class.equals(production.annotationType());
+ }
+
+}
Property changes on:
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/MutableEnhancedAnnotatedElementTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Order.java
===================================================================
--- ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Order.java
(rev 0)
+++
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Order.java 2008-06-25
10:50:10 UTC (rev 15)
@@ -0,0 +1,11 @@
+package org.jboss.webbeans.test.components;
+
+import javax.webbeans.Named;
+import javax.webbeans.Production;
+
+@Production
+@Named("order")
+public class Order
+{
+
+}
Property changes on:
ri/trunk/webbeans-impl/src/test/java/org/jboss/webbeans/test/components/Order.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain