Author: swd847
Date: 2010-03-02 18:11:32 -0500 (Tue, 02 Mar 2010)
New Revision: 5989
Added:
extensions/trunk/genericbeans/
extensions/trunk/genericbeans/pom.xml
extensions/trunk/genericbeans/src/
extensions/trunk/genericbeans/src/main/
extensions/trunk/genericbeans/src/main/java/
extensions/trunk/genericbeans/src/main/java/org/
extensions/trunk/genericbeans/src/main/java/org/jboss/
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/FieldSetter.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/Generic.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericAnnotation.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericBeanInjectionTargetWrapper.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericExtension.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectConfiguration.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectGeneric.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectLiteral.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/ProducerFieldInjectionTarget.java
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/SyntheticQualifier.java
extensions/trunk/genericbeans/src/main/resources/
extensions/trunk/genericbeans/src/main/resources/META-INF/
extensions/trunk/genericbeans/src/main/resources/META-INF/services/
extensions/trunk/genericbeans/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
extensions/trunk/genericbeans/src/test/
extensions/trunk/genericbeans/src/test/java/
extensions/trunk/genericbeans/src/test/java/org/
extensions/trunk/genericbeans/src/test/java/org/jboss/
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/AbstractTest.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericBeanTest.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericDep.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericMain.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericProducer.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/InjectedBean.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/SomeQualifier.java
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/TestAnnotation.java
extensions/trunk/genericbeans/src/test/resources/
extensions/trunk/genericbeans/src/test/resources/META-INF/
extensions/trunk/genericbeans/src/test/resources/META-INF/beans.xml
extensions/trunk/genericbeans/src/test/resources/META-INF/services/
Modified:
extensions/trunk/pom.xml
Log:
Commiting initial generic beans implementation
Added: extensions/trunk/genericbeans/pom.xml
===================================================================
--- extensions/trunk/genericbeans/pom.xml (rev 0)
+++ extensions/trunk/genericbeans/pom.xml 2010-03-02 23:11:32 UTC (rev 5989)
@@ -0,0 +1,80 @@
+<?xml version="1.0"?>
+<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+
+ <!--
+ <parent>
+ <artifactId>weld-extensions-parent</artifactId>
+ <groupId>org.jboss.weld</groupId>
+ <version>1.0.1-SNAPSHOT</version>
+ <relativePath>../parent/pom.xml</relativePath>
+ </parent>
+ -->
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-extensions-genericbeans</artifactId>
+ <version>1.0.1-SNAPSHOT</version>
+ <name>Weld Generic Beans</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <version>1.0-SP1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-core</artifactId>
+ <version>1.0.1-Final</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-extensions</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.weld</groupId>
+ <artifactId>weld-se</artifactId>
+ <version>1.0.1-Final</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>5.10</version>
+ <type>jar</type>
+ <classifier>jdk15</classifier>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-eclipse-plugin</artifactId>
+ <configuration>
+ <workspace>${basedir}</workspace>
+
+ <downloadJavadocs>true</downloadJavadocs>
+ <downloadSources>true</downloadSources>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <scm>
+
<
connection>scm:svn:http://anonsvn.jboss.org/repos/weld/extensions/trun...
+
<
developerConnection>scm:svn:https://svn.jboss.org/repos/weld/extension...
+
<
url>http://fisheye.jboss.org/browse/weld/extensions/trunk/genericbeans...
+ </scm>
+</project>
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/FieldSetter.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/FieldSetter.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/FieldSetter.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,58 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.reflect.Field;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+
+/**
+ * Class that is responsible for setting the values of generic producer fields
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+public class FieldSetter
+{
+ final Field field;
+ final SyntheticQualifier qualifier;
+ final BeanManager beanManager;
+
+ public FieldSetter(BeanManager beanManager, Field field, SyntheticQualifier
qualifier)
+ {
+ this.field = field;
+ this.qualifier = qualifier;
+ this.beanManager = beanManager;
+ field.setAccessible(true);
+ }
+
+ public void set(Object instance, CreationalContext<?> ctx)
+ {
+ Set<Bean<?>> beans = beanManager.getBeans(field.getType(), qualifier);
+ if (beans.size() == 0)
+ {
+ throw new RuntimeException("Could not resolve bean for Generic Producer
field " + field.getDeclaringClass() + "." + field.getName());
+ }
+ if (beans.size() > 1)
+ {
+ throw new RuntimeException("More than 1 bean resolved for Generic Producer
field " + field.getDeclaringClass() + "." + field.getName());
+ }
+ Bean bean = beans.iterator().next();
+ Object dep = beanManager.getReference(bean, field.getType(), ctx);
+ try
+ {
+ field.set(instance, dep);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/Generic.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/Generic.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/Generic.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,19 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a generic bean
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.TYPE })
+public @interface Generic
+{
+ Class value();
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericAnnotation.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericAnnotation.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericAnnotation.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,20 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Must be applied to any annotation that is used as a value in @Generic, this
+ * may not be needed in the future
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.TYPE })
+public @interface GenericAnnotation
+{
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericBeanInjectionTargetWrapper.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericBeanInjectionTargetWrapper.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericBeanInjectionTargetWrapper.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,104 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+import org.jboss.weld.extensions.util.reannotated.ReannotatedField;
+import org.jboss.weld.extensions.util.reannotated.ReannotatedType;
+
+/**
+ * injection target wrapper that injects the configuration for generic beans
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ * @param <T>
+ */
+public class GenericBeanInjectionTargetWrapper<T> implements
InjectionTarget<T>
+{
+ InjectionTarget<T> delegate;
+ Annotation annotation;
+ ReannotatedType<T> annotatedType;
+
+ public GenericBeanInjectionTargetWrapper(ReannotatedType<T> annotatedType,
InjectionTarget<T> delegate, Annotation annotation)
+ {
+ this.annotation = annotation;
+ this.delegate = delegate;
+ this.annotatedType = annotatedType;
+ }
+
+ public void inject(T instance, CreationalContext<T> ctx)
+ {
+ for (Field f : getFields(instance.getClass()))
+ {
+
+ if (annotation.annotationType().isAssignableFrom(f.getType()))
+ {
+ ReannotatedField<? super T> reannotatedField =
annotatedType.getField(f);
+ if (reannotatedField.isAnnotationPresent(InjectConfiguration.class))
+ {
+ try
+ {
+ f.setAccessible(true);
+ f.set(instance, annotation);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+
+ delegate.inject(instance, ctx);
+ }
+
+ public void postConstruct(T instance)
+ {
+ delegate.postConstruct(instance);
+ }
+
+ public void preDestroy(T instance)
+ {
+ delegate.preDestroy(instance);
+ }
+
+ public void dispose(T instance)
+ {
+ delegate.dispose(instance);
+ }
+
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return delegate.getInjectionPoints();
+ }
+
+ public T produce(CreationalContext<T> ctx)
+ {
+ return delegate.produce(ctx);
+ }
+
+ public static Set<Field> getFields(Class clazz)
+ {
+ Set<Field> ret = new HashSet<Field>();
+ return getFields(clazz, ret);
+ }
+
+ private static Set<Field> getFields(Class clazz, Set<Field> ret)
+ {
+ ret.addAll(Arrays.asList(clazz.getDeclaredFields()));
+ Class n = clazz.getSuperclass();
+ if (n != Object.class)
+ {
+ return getFields(n);
+ }
+ return ret;
+ }
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericExtension.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericExtension.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/GenericExtension.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,191 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.AnnotatedParameter;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessInjectionTarget;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Inject;
+
+import org.jboss.weld.extensions.util.AnnotationInstanceProvider;
+import org.jboss.weld.extensions.util.BeanImpl;
+import org.jboss.weld.extensions.util.reannotated.ReannotatedField;
+import org.jboss.weld.extensions.util.reannotated.ReannotatedParameter;
+import org.jboss.weld.extensions.util.reannotated.ReannotatedType;
+
+public class GenericExtension implements Extension
+{
+
+ AnnotationInstanceProvider annotationProvider = new AnnotationInstanceProvider();
+
+ Map<Class<?>, Set<AnnotatedType<?>>> genericBeans = new
HashMap<Class<?>, Set<AnnotatedType<?>>>();
+
+ Map<Class<?>, Map<AnnotatedField<?>, Annotation>>
producerFields = new HashMap<Class<?>, Map<AnnotatedField<?>,
Annotation>>();
+
+ /**
+ * map of a generic annotation type to all instances of that type found on
+ * beans
+ */
+ Map<Class<?>, Set<Annotation>> concreteGenerics = new
HashMap<Class<?>, Set<Annotation>>();
+
+ /**
+ * Map of generic Annotation instance to a SyntheticQualifier
+ */
+ Map<Annotation, SyntheticQualifier> qualifierMap = new HashMap<Annotation,
SyntheticQualifier>();
+
+ long count = 0;
+
+ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event)
+ {
+ event.addQualifier(SyntheticQualifier.class);
+ }
+
+ public void processAnnotatedType(@Observes ProcessAnnotatedType<?> event)
+ {
+ AnnotatedType<?> type = event.getAnnotatedType();
+ if (type.isAnnotationPresent(Generic.class))
+ {
+ Generic an = type.getAnnotation(Generic.class);
+ if (!genericBeans.containsKey(an.value()))
+ {
+ genericBeans.put(an.value(), new HashSet<AnnotatedType<?>>());
+ }
+ genericBeans.get(an.value()).add(type);
+ // we will install this bean later
+ event.veto();
+
+ }
+
+ for (Object f : type.getFields())
+ {
+ AnnotatedField<?> field = (AnnotatedField<?>) f;
+ if (field.isAnnotationPresent(Produces.class))
+ {
+ for (Annotation a : field.getAnnotations())
+ {
+ if (a.annotationType().isAnnotationPresent(GenericAnnotation.class))
+ {
+ if (!producerFields.containsKey(type.getJavaClass()))
+ {
+ producerFields.put(type.getJavaClass(), new
HashMap<AnnotatedField<?>, Annotation>());
+ }
+ if (!concreteGenerics.containsKey(a.annotationType()))
+ {
+ concreteGenerics.put(a.annotationType(), new
HashSet<Annotation>());
+ }
+ producerFields.get(type.getJavaClass()).put(field, a);
+ concreteGenerics.get(a.annotationType()).add(a);
+ }
+ }
+ }
+ }
+ }
+
+ public <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T>
event, BeanManager beanManager)
+ {
+ Class javaClass = event.getAnnotatedType().getJavaClass();
+ if (producerFields.containsKey(javaClass))
+ {
+ Map<AnnotatedField<?>, Annotation> producers =
producerFields.get(javaClass);
+ List<FieldSetter> setters = new ArrayList<FieldSetter>();
+ for (AnnotatedField<?> a : producers.keySet())
+ {
+ SyntheticQualifier qual = this.getQualifierForGeneric(producers.get(a));
+ FieldSetter f = new FieldSetter(beanManager, a.getJavaMember(), qual);
+ setters.add(f);
+ }
+ ProducerFieldInjectionTarget<T> it = new
ProducerFieldInjectionTarget<T>(event.getInjectionTarget(), setters);
+ event.setInjectionTarget(it);
+ }
+ }
+
+ public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager
beanManager)
+ {
+ for (Entry<Class<?>, Set<AnnotatedType<?>>> i :
genericBeans.entrySet())
+ {
+ Set<Annotation> concretes = concreteGenerics.get(i.getKey());
+ if (concretes != null)
+ {
+ for (AnnotatedType<?> at : i.getValue())
+ {
+ for (Annotation conc : concretes)
+ {
+ final SyntheticQualifier newQualifier = getQualifierForGeneric(conc);
+ ReannotatedType<?> rt = new ReannotatedType(at);
+
+ rt.define(newQualifier);
+ for (AnnotatedField<?> f : rt.getFields())
+ {
+ ReannotatedField<?> field = (ReannotatedField<?>) f;
+ if (field.isAnnotationPresent(InjectGeneric.class))
+ {
+ field.define(newQualifier);
+ field.define(new InjectLiteral());
+ }
+ else if (field.isAnnotationPresent(Inject.class))
+ {
+ //if this is a configuration injection point
+ if
(conc.annotationType().isAssignableFrom(field.getJavaMember().getType()))
+ {
+ field.undefine(Inject.class);
+ field.define(new
AnnotationLiteral<InjectConfiguration>()
+ {
+ });
+ }
+ }
+ }
+ for (AnnotatedMethod<?> m : rt.getMethods())
+ {
+ for (AnnotatedParameter<?> a : m.getParameters())
+ {
+ ReannotatedParameter<?> param =
(ReannotatedParameter<?>) a;
+ if (param.isAnnotationPresent(InjectGeneric.class))
+ {
+ param.define(newQualifier);
+ param.define(new InjectLiteral());
+ }
+ }
+ }
+ InjectionTarget<?> it = beanManager.createInjectionTarget(rt);
+
+ it = new GenericBeanInjectionTargetWrapper(rt, it, conc);
+ BeanImpl<?> bean = new BeanImpl(it, rt);
+ event.addBean(bean);
+
+ }
+ }
+ }
+ }
+
+ }
+
+ public SyntheticQualifier getQualifierForGeneric(Annotation a)
+ {
+ if (!qualifierMap.containsKey(a))
+ {
+ SyntheticQualifier qualifier = (SyntheticQualifier)
annotationProvider.get(SyntheticQualifier.class, (Map)
Collections.singletonMap("value", count++));
+ qualifierMap.put(a, qualifier);
+ }
+ return qualifierMap.get(a);
+ }
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectConfiguration.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectConfiguration.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectConfiguration.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,15 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * synthetic qualifier that is added to Generic beans to signify configuration annotation
injection points
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+public @interface InjectConfiguration
+{
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectGeneric.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectGeneric.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectGeneric.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,20 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * used to mark where a generic bean should be inejcted in place of the normal
+ * @Inject, this may not be required in the future
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.FIELD, ElementType.PARAMETER })
+public @interface InjectGeneric
+{
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectLiteral.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectLiteral.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/InjectLiteral.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,9 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Inject;
+
+public class InjectLiteral extends AnnotationLiteral<Inject> implements Inject
+{
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/ProducerFieldInjectionTarget.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/ProducerFieldInjectionTarget.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/ProducerFieldInjectionTarget.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,62 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.util.List;
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+/**
+ * injection target wrapper used for beans that have generic producer fields
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ * @param <T>
+ */
+public class ProducerFieldInjectionTarget<T> implements InjectionTarget<T>
+{
+ final InjectionTarget<T> delegate;
+ final List<FieldSetter> fieldSetters;
+
+ public ProducerFieldInjectionTarget(InjectionTarget<T> delegate,
List<FieldSetter> fieldSetters)
+ {
+ this.delegate = delegate;
+ this.fieldSetters = fieldSetters;
+ }
+
+ public void inject(T instance, CreationalContext<T> ctx)
+ {
+ for (FieldSetter f : fieldSetters)
+ {
+ f.set(instance, ctx);
+ }
+ delegate.inject(instance, ctx);
+ }
+
+ public void postConstruct(T instance)
+ {
+ delegate.postConstruct(instance);
+ }
+
+ public void preDestroy(T instance)
+ {
+ delegate.preDestroy(instance);
+ }
+
+ public void dispose(T instance)
+ {
+ delegate.dispose(instance);
+ }
+
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return delegate.getInjectionPoints();
+ }
+
+ public T produce(CreationalContext<T> ctx)
+ {
+ return delegate.produce(ctx);
+ }
+
+}
Added:
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/SyntheticQualifier.java
===================================================================
---
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/SyntheticQualifier.java
(rev 0)
+++
extensions/trunk/genericbeans/src/main/java/org/jboss/weld/extensions/genericbeans/SyntheticQualifier.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,21 @@
+package org.jboss.weld.extensions.genericbeans;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import javax.inject.Qualifier;
+
+/**
+ * qualifier that is added to Generic beans to that the correct one is injected
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+// even though this is not in a bean archive this is still needed to make
+// BeanImpl work
+@Qualifier
+public @interface SyntheticQualifier
+{
+ long value();
+}
Added:
extensions/trunk/genericbeans/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
---
extensions/trunk/genericbeans/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
(rev 0)
+++
extensions/trunk/genericbeans/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1 @@
+org.jboss.weld.extensions.genericbeans.GenericExtension
\ No newline at end of file
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/AbstractTest.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/AbstractTest.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/AbstractTest.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,59 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+
+import org.jboss.weld.environment.se.Weld;
+import org.jboss.weld.environment.se.WeldContainer;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+
+public class AbstractTest
+{
+
+ protected BeanManager manager;
+
+ Weld weld;
+
+ protected void preBootstrap()
+ {
+ }
+
+ @BeforeClass
+ public void setup()
+ {
+ weld = new Weld();
+ WeldContainer container = weld.initialize();
+ manager = container.getBeanManager();
+ }
+
+ @AfterClass
+ public void teardown()
+ {
+ weld.shutdown();
+ }
+
+ public <T> T getReference(Class<T> clazz, Annotation... bindings)
+ {
+ Set<Bean<?>> beans = manager.getBeans(clazz, bindings);
+ if (beans.isEmpty())
+ {
+ throw new RuntimeException("No bean found with class: " + clazz +
" and bindings " + bindings.toString());
+ }
+ else if (beans.size() != 1)
+ {
+ StringBuilder bs = new StringBuilder("[");
+ for (Annotation a : bindings)
+ {
+ bs.append(a.toString() + ",");
+ }
+ bs.append("]");
+ throw new RuntimeException("More than one bean found with class: " +
clazz + " and bindings " + bs);
+ }
+ Bean bean = beans.iterator().next();
+ return (T) bean.create(manager.createCreationalContext(bean));
+ }
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericBeanTest.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericBeanTest.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericBeanTest.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,17 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import org.testng.annotations.Test;
+
+public class GenericBeanTest extends AbstractTest
+{
+ @Test
+ public void testGeneric()
+ {
+ InjectedBean bean = getReference(InjectedBean.class);
+ assert bean.main1.getValue().equals("hello1");
+ assert bean.main2.getValue().equals("hello2");
+ assert bean.main1.getNoData() == null;
+ assert bean.main2.getNoData() == null;
+
+ }
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericDep.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericDep.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericDep.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,24 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import javax.inject.Inject;
+
+import org.jboss.weld.extensions.genericbeans.Generic;
+
+(a)Generic(TestAnnotation.class)
+public class GenericDep
+{
+ @Inject
+ TestAnnotation data;
+
+ TestAnnotation noData;
+
+ public String getValue()
+ {
+ return data.value();
+ }
+
+ public TestAnnotation getNoData()
+ {
+ return noData;
+ }
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericMain.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericMain.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericMain.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,21 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import org.jboss.weld.extensions.genericbeans.Generic;
+import org.jboss.weld.extensions.genericbeans.InjectGeneric;
+
+(a)Generic(TestAnnotation.class)
+public class GenericMain
+{
+ @InjectGeneric
+ GenericDep dep;
+
+ public String getValue()
+ {
+ return dep.getValue();
+ }
+
+ public TestAnnotation getNoData()
+ {
+ return dep.getNoData();
+ }
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericProducer.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericProducer.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/GenericProducer.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,16 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import javax.enterprise.inject.Produces;
+
+public class GenericProducer
+{
+ @SomeQualifier(1)
+ @Produces
+ @TestAnnotation("hello1")
+ GenericMain main1;
+
+ @SomeQualifier(2)
+ @Produces
+ @TestAnnotation("hello2")
+ GenericMain main2;
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/InjectedBean.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/InjectedBean.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/InjectedBean.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,12 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import javax.inject.Inject;
+
+public class InjectedBean
+{
+ @Inject @SomeQualifier(1)
+ public GenericMain main1;
+
+ @Inject @SomeQualifier(2)
+ public GenericMain main2;
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/SomeQualifier.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/SomeQualifier.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/SomeQualifier.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,13 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import javax.inject.Qualifier;
+
+(a)Retention(RetentionPolicy.RUNTIME)
+@Qualifier
+public @interface SomeQualifier
+{
+ int value();
+}
Added:
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/TestAnnotation.java
===================================================================
---
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/TestAnnotation.java
(rev 0)
+++
extensions/trunk/genericbeans/src/test/java/org/jboss/weld/extensions/genericbeans/test/TestAnnotation.java 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,13 @@
+package org.jboss.weld.extensions.genericbeans.test;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import org.jboss.weld.extensions.genericbeans.GenericAnnotation;
+
+(a)Retention(RetentionPolicy.RUNTIME)
+@GenericAnnotation
+public @interface TestAnnotation
+{
+ String value();
+}
Added: extensions/trunk/genericbeans/src/test/resources/META-INF/beans.xml
===================================================================
--- extensions/trunk/genericbeans/src/test/resources/META-INF/beans.xml
(rev 0)
+++ extensions/trunk/genericbeans/src/test/resources/META-INF/beans.xml 2010-03-02
23:11:32 UTC (rev 5989)
@@ -0,0 +1,5 @@
+<beans
xmlns="http://java.sun.com/xml/ns/javaee"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
+
+</beans>
Modified: extensions/trunk/pom.xml
===================================================================
--- extensions/trunk/pom.xml 2010-03-02 19:40:54 UTC (rev 5988)
+++ extensions/trunk/pom.xml 2010-03-02 23:11:32 UTC (rev 5989)
@@ -8,7 +8,7 @@
<parent>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-parent</artifactId>
- <version>8</version>
+ <version>9</version>
</parent>
<name>Weld Extensions Build Aggregator</name>
@@ -17,6 +17,7 @@
<module>bom</module>
<module>parent</module>
<module>core</module>
+ <module>genericbeans</module>
</modules>
<!-- Minimal metadata -->