[webbeans-commits] Webbeans SVN: r393 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: introspector and 1 other directories.
by webbeans-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2008-12-03 12:51:11 -0500 (Wed, 03 Dec 2008)
New Revision: 393
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedItem.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/ForwardingAnnotatedItem.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedMember.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedType.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedAnnotationImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedConstructorImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedFieldImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedItemImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedParameterImpl.java
Log:
Make annotatedItem immutable
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ejb/EjbMetaData.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -116,7 +116,7 @@
for (AnnotatedMethod<Object> removeMethod : type.getAnnotatedMethods(REMOVE_ANNOTATION))
{
removeMethods.add(removeMethod);
- if (removeMethod.getDelegate().getParameterTypes().length == 0)
+ if (removeMethod.getParameters().size() == 0)
{
noArgsRemoveMethods.add(removeMethod);
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedItem.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedItem.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/AnnotatedItem.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -91,13 +91,6 @@
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
/**
- * Gets the underlying element
- *
- * @return The annotated item
- */
- public S getDelegate();
-
- /**
* Gets the type of the element
*
* @return The type of the element
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/ForwardingAnnotatedItem.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/ForwardingAnnotatedItem.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/ForwardingAnnotatedItem.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -91,14 +91,6 @@
/**
* @see org.jboss.webbeans.introspector.AnnotatedItem
*/
- public S getDelegate()
- {
- return delegate().getDelegate();
- }
-
- /**
- * @see org.jboss.webbeans.introspector.AnnotatedItem
- */
public String getName()
{
return delegate().getName();
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedItem.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -21,6 +21,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -29,6 +30,9 @@
import java.util.Set;
import javax.webbeans.BindingType;
+import javax.webbeans.DeploymentType;
+import javax.webbeans.ScopeType;
+import javax.webbeans.Stereotype;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bindings.CurrentAnnotationLiteral;
@@ -44,6 +48,8 @@
* Represents functionality common for all annotated items, mainly different
* mappings of the annotations and meta-annotations
*
+ * AbstractAnnotatedItem is an immutable class and therefore threadsafe
+ *
* @author Pete Muir
* @author Nicklas Karlsson
*
@@ -61,7 +67,7 @@
*/
public static class AnnotationMap extends ForwardingMap<Class<? extends Annotation>, Annotation>
{
- private Map<Class<? extends Annotation>, Annotation> delegate;
+ private final Map<Class<? extends Annotation>, Annotation> delegate;
public AnnotationMap()
{
@@ -94,7 +100,7 @@
*/
private static class MetaAnnotationMap extends ForwardingMap<Class<? extends Annotation>, Set<Annotation>>
{
- private Map<Class<? extends Annotation>, Set<Annotation>> delegate;
+ private final Map<Class<? extends Annotation>, Set<Annotation>> delegate;
public MetaAnnotationMap()
{
@@ -155,67 +161,11 @@
private static final Annotation[] DEFAULT_BINDING_ARRAY = { new CurrentAnnotationLiteral() };
// The set of default binding types
private static final Set<Annotation> DEFAULT_BINDING = new HashSet<Annotation>(Arrays.asList(DEFAULT_BINDING_ARRAY));
- // The array of meta-annotations to map
- private static final Annotation[] MAPPED_METAANNOTATIONS_ARRAY = {};
+
// The set of meta-annotations to map
- private static final Set<Annotation> MAPPED_METAANNOTATIONS = new HashSet<Annotation>(Arrays.asList(MAPPED_METAANNOTATIONS_ARRAY));
-
- // The annotation map (annotation type -> annotation) of the item
- private AnnotationMap annotationMap;
- // The meta-annotation map (annotation type -> set of annotations containing
- // meta-annotation) of the item
- private MetaAnnotationMap metaAnnotationMap;
- // The set of all annotations on the item
- private Set<Annotation> annotationSet;
- // The array of all annotations on the item
- private Annotation[] annotationArray;
-
+ private static final Set<Class<? extends Annotation>> MAPPED_METAANNOTATIONS = new HashSet<Class<? extends Annotation>>(Arrays.asList(BindingType.class, DeploymentType.class, Stereotype.class, ScopeType.class));
+
/**
- * Constructor
- *
- * Also builds the meta-annotation map. Throws a NullPointerException if
- * trying to register a null map
- *
- * @param annotationMap A map of annotation to register
- *
- */
- public AbstractAnnotatedItem(AnnotationMap annotationMap)
- {
- if (annotationMap == null)
- {
- throw new NullPointerException("annotationMap cannot be null");
- }
- this.annotationMap = annotationMap;
- buildMetaAnnotationMap(annotationMap);
- }
-
- /**
- * Build the meta-annotation map
- *
- * Iterates through the annotationMap values (annotations) and for each
- * meta-annotation on the annotation register the annotation in the set under
- * they key of the meta-annotation type.
- *
- * @param annotationMap The annotation map to parse
- */
- private void buildMetaAnnotationMap(AnnotationMap annotationMap)
- {
- metaAnnotationMap = new MetaAnnotationMap();
- for (Annotation annotation : annotationMap.values())
- {
- for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
- {
- // TODO: Check with Pete how to fill the array. Make annotation
- // literals for all?
- // if (MAPPED_METAANNOTATIONS.contains(metaAnnotation))
- // {
- metaAnnotationMap.put(metaAnnotation.annotationType(), annotation);
- // }
- }
- }
- }
-
- /**
* Static helper method for building annotation map from an annotated element
*
* @param element The element to examine
@@ -241,7 +191,7 @@
}
return annotationMap;
}
-
+
/**
* Static helper method for getting the current parameter values from a list
* of annotated parameters.
@@ -262,7 +212,47 @@
return parameterValues;
}
+ // The annotation map (annotation type -> annotation) of the item
+ private final AnnotationMap annotationMap;
+ // The meta-annotation map (annotation type -> set of annotations containing
+ // meta-annotation) of the item
+ private final MetaAnnotationMap metaAnnotationMap;
+ // The set of all annotations on the item
+ private final Set<Annotation> annotationSet;
+
/**
+ * Constructor
+ *
+ * Also builds the meta-annotation map. Throws a NullPointerException if
+ * trying to register a null map
+ *
+ * @param annotationMap A map of annotation to register
+ *
+ */
+ public AbstractAnnotatedItem(AnnotationMap annotationMap)
+ {
+ if (annotationMap == null)
+ {
+ throw new NullPointerException("annotationMap cannot be null");
+ }
+ this.annotationMap = annotationMap;
+ this.annotationSet = new HashSet<Annotation>();
+ this.metaAnnotationMap = new MetaAnnotationMap();
+ for (Annotation annotation : annotationMap.values())
+ {
+ for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
+ {
+ // Only map meta-annotations we are interested in
+ if (MAPPED_METAANNOTATIONS.contains(metaAnnotation.annotationType()))
+ {
+ metaAnnotationMap.put(metaAnnotation.annotationType(), annotation);
+ }
+ }
+ annotationSet.add(annotation);
+ }
+ }
+
+ /**
* Gets the annotation for a given annotation type.
*
* @param annotationType the annotation type to match
@@ -287,7 +277,7 @@
*/
public Set<Annotation> getMetaAnnotations(Class<? extends Annotation> metaAnnotationType)
{
- return metaAnnotationMap.get(metaAnnotationType);
+ return Collections.unmodifiableSet(metaAnnotationMap.get(metaAnnotationType));
}
/**
@@ -304,12 +294,7 @@
*/
public Annotation[] getMetaAnnotationsAsArray(Class<? extends Annotation> metaAnnotationType)
{
- if (annotationArray == null)
- {
- annotationArray = new Annotation[0];
- annotationArray = getMetaAnnotations(metaAnnotationType).toArray(annotationArray);
- }
- return annotationArray;
+ return getMetaAnnotations(metaAnnotationType).toArray(new Annotation[0]);
}
/**
@@ -323,12 +308,7 @@
*/
public Set<Annotation> getAnnotations()
{
- if (annotationSet == null)
- {
- annotationSet = new HashSet<Annotation>();
- annotationSet.addAll(annotationMap.values());
- }
- return annotationSet;
+ return Collections.unmodifiableSet(annotationSet);
}
/**
@@ -349,9 +329,9 @@
*
* @return The annotation map
*/
- protected AnnotationMap getAnnotationMap()
+ protected Map<Class<? extends Annotation>, Annotation> getAnnotationMap()
{
- return annotationMap;
+ return Collections.unmodifiableMap(annotationMap);
}
/**
@@ -465,11 +445,11 @@
{
if (getMetaAnnotations(BindingType.class).size() > 0)
{
- return getMetaAnnotations(BindingType.class);
+ return Collections.unmodifiableSet(getMetaAnnotations(BindingType.class));
}
else
{
- return DEFAULT_BINDING;
+ return Collections.unmodifiableSet(DEFAULT_BINDING);
}
}
@@ -525,5 +505,7 @@
return true;
}
}
+
+ protected abstract S getDelegate();
}
\ No newline at end of file
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedMember.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedMember.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedMember.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -36,6 +36,8 @@
/**
* Represents an abstract annotated memeber (field, method or constructor)
*
+ * This class is immutable, and therefore threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -87,20 +89,21 @@
}
// The name of the member
- private String name;
+ private final String name;
/**
* Constructor
*
* @param annotationMap The annotation map
*/
- public AbstractAnnotatedMember(AnnotationMap annotationMap)
+ public AbstractAnnotatedMember(AnnotationMap annotationMap, Member member)
{
super(annotationMap);
+ name = member.getName();
}
/**
- * Indicates if the member is static (through the delegate)
+ * Indicates if the member is static
*
* @return True if static, false otherwise
*
@@ -112,7 +115,7 @@
}
/**
- * Indicates if the member if final (through the delegate)
+ * Indicates if the member if final
*
* @return True if final, false otherwise
*
@@ -137,16 +140,12 @@
/**
* Gets the name of the member
*
- * @returns The name (or the name of the delegate if none is defined)
+ * @returns The name
*
* @see org.jboss.webbeans.introspector.AnnotatedItem#getName()
*/
public String getName()
{
- if (name == null)
- {
- name = getDelegate().getName();
- }
return name;
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedType.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedType.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AbstractAnnotatedType.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -23,6 +23,8 @@
/**
* Represents an abstract annotated type
*
+ * This class is immutable, and therefore threadsage
+ *
* @author Pete Muir
*
* @param <T>
@@ -30,20 +32,31 @@
public abstract class AbstractAnnotatedType<T> extends AbstractAnnotatedItem<T, Class<T>>
{
// The superclass abstraction of the type
- private AnnotatedClass<Object> superclass;
+ private final AnnotatedClass<Object> superclass;
+ private final String name;
/**
* Constructor
*
* @param annotationMap The annotation map
*/
- public AbstractAnnotatedType(AnnotationMap annotationMap)
+ @SuppressWarnings("unchecked")
+ public AbstractAnnotatedType(AnnotationMap annotationMap, Class<T> type)
{
super(annotationMap);
+ this.name = type.getName();
+ if (type.getSuperclass() != null)
+ {
+ this.superclass = new AnnotatedClassImpl(type.getSuperclass());
+ }
+ else
+ {
+ this.superclass = null;
+ }
}
/**
- * Indicates if the type is static (through the delegate)
+ * Indicates if the type is static
*
* @return True if static, false otherwise
*
@@ -55,7 +68,7 @@
}
/**
- * Indicates if the type if final (through the delegate)
+ * Indicates if the type if final
*
* @return True if final, false otherwise
*
@@ -69,13 +82,13 @@
/**
* Gets the name of the type
*
- * @returns The name (through the delegate)
+ * @returns The name
*
* @see org.jboss.webbeans.introspector.AnnotatedItem#getName()
*/
public String getName()
{
- return getDelegate().getName();
+ return name;
}
/**
@@ -84,13 +97,8 @@
* @return The superclass abstraction
*/
@SuppressWarnings("unchecked")
- // TODO Fix this
public AnnotatedClass<Object> getSuperclass()
{
- if (superclass == null)
- {
- superclass = new AnnotatedClassImpl(getDelegate().getSuperclass());
- }
return superclass;
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedAnnotationImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedAnnotationImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedAnnotationImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -20,6 +20,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -34,6 +35,8 @@
/**
* Represents an annotated annotation
*
+ * This class is immutable and therefore threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -85,11 +88,11 @@
}
// The annotated members map (annotation -> member with annotation)
- private AnnotatedMemberMap annotatedMembers;
+ private final AnnotatedMemberMap annotatedMembers;
// The implementation class of the annotation
- private Class<T> clazz;
+ private final Class<T> clazz;
// The set of abstracted members
- private Set<AnnotatedMethod<?>> members;
+ private final Set<AnnotatedMethod<?>> members;
/**
* Constructor
@@ -100,8 +103,19 @@
*/
public AnnotatedAnnotationImpl(Class<T> annotationType)
{
- super(buildAnnotationMap(annotationType));
+ super(buildAnnotationMap(annotationType), annotationType);
this.clazz = annotationType;
+ members = new HashSet<AnnotatedMethod<?>>();
+ annotatedMembers = new AnnotatedMemberMap();
+ for (Method member : clazz.getDeclaredMethods())
+ {
+ AnnotatedMethod<? extends Object> annotatedMethod = new AnnotatedMethodImpl<Object>(member, this);
+ members.add(annotatedMethod);
+ for (Annotation annotation : annotatedMethod.getAnnotations())
+ {
+ annotatedMembers.put(annotation.annotationType(), annotatedMethod);
+ }
+ }
}
/**
@@ -127,26 +141,10 @@
*/
public Set<AnnotatedMethod<?>> getMembers()
{
- if (members == null)
- {
- initMembers();
- }
- return members;
+ return Collections.unmodifiableSet(members);
}
/**
- * Gets the delegate
- *
- * @return The delegate
- *
- * @see org.jboss.webbeans.introspector.AnnotatedAnnotation#getDelegate()
- */
- public Class<T> getDelegate()
- {
- return clazz;
- }
-
- /**
* Gets the type of the annotation
*
* @see org.jboss.webbeans.introspector.AnnotatedAnnotation#getType()
@@ -157,21 +155,6 @@
}
/**
- * Initializes the members
- *
- * Iterates through the declared members, creates abstractions of them and
- * adds them to the members set
- */
- private void initMembers()
- {
- members = new HashSet<AnnotatedMethod<?>>();
- for (Method member : clazz.getDeclaredMethods())
- {
- members.add(new AnnotatedMethodImpl<Object>(member, this));
- }
- }
-
- /**
* Returns the annotated members with a given annotation type
*
* If the annotated members are null, they are initialized first.
@@ -184,39 +167,10 @@
*/
public Set<AnnotatedMethod<?>> getAnnotatedMembers(Class<? extends Annotation> annotationType)
{
- if (annotatedMembers == null)
- {
- initAnnotatedMembers();
- }
- return annotatedMembers.get(annotationType);
+ return Collections.unmodifiableSet(annotatedMembers.get(annotationType));
}
/**
- * Initializes the annotated members
- *
- * If the members are null, the are initialized first.
- *
- * Iterates over the members and for each member, iterate over the
- * annotations present, creating member abstractions and mapping them under
- * the annotation in the annotatedMembers map.
- */
- private void initAnnotatedMembers()
- {
- if (members == null)
- {
- initMembers();
- }
- annotatedMembers = new AnnotatedMemberMap();
- for (AnnotatedMethod<?> member : members)
- {
- for (Annotation annotation : member.getAnnotations())
- {
- annotatedMembers.put(annotation.annotationType(), member);
- }
- }
- }
-
- /**
* Gets a string representation of the constructor
*
* @return A string representation
@@ -239,4 +193,9 @@
return buffer.toString();
}
+ protected Class<T> getDelegate()
+ {
+ return clazz;
+ }
+
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -24,6 +24,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -41,6 +42,8 @@
/**
* Represents an annotated class
*
+ * This class is immutable, and thus threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -213,28 +216,28 @@
}
// The implementing class
- private Class<T> clazz;
+ private final Class<T> clazz;
// The type arguments
- private Type[] actualTypeArguments;
+ private final Type[] actualTypeArguments;
// The set of abstracted fields
- private Set<AnnotatedField<Object>> fields;
+ private final Set<AnnotatedField<Object>> fields;
// The map from annotation type to abstracted field with annotation
- private AnnotatedFieldMap annotatedFields;
+ private final AnnotatedFieldMap annotatedFields;
// The map from annotation type to abstracted field with meta-annotation
- private AnnotatedFieldMap metaAnnotatedFields;
+ private final AnnotatedFieldMap metaAnnotatedFields;
// The set of abstracted methods
- private Set<AnnotatedMethod<Object>> methods;
+ private final Set<AnnotatedMethod<Object>> methods;
// The map from annotation type to abstracted method with annotation
- private AnnotatedMethodMap annotatedMethods;
+ private final AnnotatedMethodMap annotatedMethods;
// The set of abstracted constructors
- private Set<AnnotatedConstructor<T>> constructors;
+ private final Set<AnnotatedConstructor<T>> constructors;
// The map from annotation type to abstracted constructor with annotation
- private AnnotatedConstructorMap annotatedConstructors;
+ private final AnnotatedConstructorMap annotatedConstructors;
// The map from class list to abstracted constructor
- private ConstructorsByArgumentMap constructorsByArgumentMap;
+ private final ConstructorsByArgumentMap constructorsByArgumentMap;
/**
* Constructor
@@ -248,7 +251,7 @@
*/
public AnnotatedClassImpl(Class<T> rawType, Type type, Annotation[] annotations)
{
- super(buildAnnotationMap(annotations));
+ super(buildAnnotationMap(annotations), rawType);
this.clazz = rawType;
if (type instanceof ParameterizedType)
{
@@ -258,6 +261,78 @@
{
actualTypeArguments = new Type[0];
}
+
+ this.fields = new HashSet<AnnotatedField<Object>>();
+ this.annotatedFields = new AnnotatedFieldMap();
+ this.metaAnnotatedFields = new AnnotatedFieldMap();
+ for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass())
+ {
+ for (Field field : clazz.getDeclaredFields())
+ {
+ if (!field.isAccessible())
+ {
+ field.setAccessible(true);
+ }
+ AnnotatedField<Object> annotatedField = new AnnotatedFieldImpl<Object>(field, this);
+ this.fields.add(annotatedField);
+ for (Annotation annotation : annotatedField.getAnnotations())
+ {
+ this.annotatedFields.put(annotation.annotationType(), annotatedField);
+ for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
+ {
+ this.metaAnnotatedFields.put(metaAnnotation.annotationType(), annotatedField);
+ }
+ }
+
+ }
+ }
+
+ this.constructors = new HashSet<AnnotatedConstructor<T>>();
+ this.constructorsByArgumentMap = new ConstructorsByArgumentMap();
+ this.annotatedConstructors = new AnnotatedConstructorMap();
+ for (Constructor<?> constructor : clazz.getDeclaredConstructors())
+ {
+ AnnotatedConstructor<T> annotatedConstructor = new AnnotatedConstructorImpl<T>((Constructor<T>) constructor, this);
+ if (!constructor.isAccessible())
+ {
+ constructor.setAccessible(true);
+ }
+ this.constructors.add(annotatedConstructor);
+ this.constructorsByArgumentMap.put(Arrays.asList(constructor.getParameterTypes()), annotatedConstructor);
+
+ for (Annotation annotation : annotatedConstructor.getAnnotations())
+ {
+ if (!annotatedConstructors.containsKey(annotation.annotationType()))
+ {
+ annotatedConstructors.put(annotation.annotationType(), new HashSet<AnnotatedConstructor<T>>());
+ }
+ annotatedConstructors.get(annotation.annotationType()).add(annotatedConstructor);
+ }
+ }
+
+ this.methods = new HashSet<AnnotatedMethod<Object>>();
+ this.annotatedMethods = new AnnotatedMethodMap();
+ for (Class<?> c = clazz; c != Object.class && c != null; c = c.getSuperclass())
+ {
+ for (Method method : clazz.getDeclaredMethods())
+ {
+ if (!method.isAccessible())
+ {
+ method.setAccessible(true);
+ }
+
+ AnnotatedMethod<Object> annotatedMethod = new AnnotatedMethodImpl<Object>(method, this);
+ this.methods.add(annotatedMethod);
+ for (Annotation annotation : annotatedMethod.getAnnotations())
+ {
+ if (!annotatedMethods.containsKey(annotation.annotationType()))
+ {
+ annotatedMethods.put(annotation.annotationType(), new HashSet<AnnotatedMethod<Object>>());
+ }
+ annotatedMethods.get(annotation.annotationType()).add(annotatedMethod);
+ }
+ }
+ }
}
/**
@@ -301,11 +376,7 @@
*/
public Set<AnnotatedField<Object>> getFields()
{
- if (fields == null)
- {
- initFields();
- }
- return fields;
+ return Collections.unmodifiableSet(fields);
}
/**
@@ -317,37 +388,10 @@
*/
public Set<AnnotatedConstructor<T>> getConstructors()
{
- if (constructors == null)
- {
- initConstructors();
- }
- return constructors;
+ return Collections.unmodifiableSet(constructors);
}
/**
- * Initializes the fields
- *
- * Iterates through the type hierarchy and adds the abstracted fields to the
- * fields list
- *
- */
- private void initFields()
- {
- this.fields = new HashSet<AnnotatedField<Object>>();
- for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass())
- {
- for (Field field : clazz.getDeclaredFields())
- {
- if (!field.isAccessible())
- {
- field.setAccessible(true);
- }
- fields.add(new AnnotatedFieldImpl<Object>(field, this));
- }
- }
- }
-
- /**
* Gets abstracted fields with requested meta-annotation type present
*
* If the meta-annotations map is null, it is initializes. If the annotated
@@ -361,11 +405,7 @@
*/
public Set<AnnotatedField<Object>> getMetaAnnotatedFields(Class<? extends Annotation> metaAnnotationType)
{
- if (annotatedFields == null || metaAnnotatedFields == null)
- {
- initAnnotatedAndMetaAnnotatedFields();
- }
- return metaAnnotatedFields.get(metaAnnotationType);
+ return Collections.unmodifiableSet(metaAnnotatedFields.get(metaAnnotationType));
}
/**
@@ -379,44 +419,10 @@
*/
public Set<AnnotatedField<Object>> getAnnotatedFields(Class<? extends Annotation> annotationType)
{
- if (annotatedFields == null)
- {
- initAnnotatedAndMetaAnnotatedFields();
- }
- return annotatedFields.get(annotationType);
+ return Collections.unmodifiableSet(annotatedFields.get(annotationType));
}
/**
- * Initializes the annotated/meta-annotated fields map
- *
- * If the fields set if empty, populate it first. Iterate through the fields,
- * for each field, iterate over the annotations and map the field abstraction
- * under the annotation type key. In the inner loop, iterate over the
- * annotations of the annotations (the meta-annotations) and map the field
- * under the meta-annotation type key.
- */
- private void initAnnotatedAndMetaAnnotatedFields()
- {
- if (fields == null)
- {
- initFields();
- }
- annotatedFields = new AnnotatedFieldMap();
- metaAnnotatedFields = new AnnotatedFieldMap();
- for (AnnotatedField<Object> field : fields)
- {
- for (Annotation annotation : field.getAnnotations())
- {
- annotatedFields.put(annotation.annotationType(), field);
- for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
- {
- metaAnnotatedFields.put(metaAnnotation.annotationType(), field);
- }
- }
- }
- }
-
- /**
* Gets the type of the class
*
* @return The type
@@ -439,28 +445,6 @@
}
/**
- * Initializes the methods
- *
- * Iterate over the class hierarchy and for each type, add all methods
- * abstracted to the methods list
- */
- private void initMethods()
- {
- this.methods = new HashSet<AnnotatedMethod<Object>>();
- for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass())
- {
- for (Method method : clazz.getDeclaredMethods())
- {
- if (!method.isAccessible())
- {
- method.setAccessible(true);
- }
- methods.add(new AnnotatedMethodImpl<Object>(method, this));
- }
- }
- }
-
- /**
* Gets the abstracted methods that have a certain annotation type present
*
* If the annotated methods map is null, initialize it first
@@ -473,66 +457,10 @@
*/
public Set<AnnotatedMethod<Object>> getAnnotatedMethods(Class<? extends Annotation> annotationType)
{
- if (annotatedMethods == null)
- {
- initAnnotatedMethods();
- }
-
- return annotatedMethods.get(annotationType);
+ return Collections.unmodifiableSet(annotatedMethods.get(annotationType));
}
/**
- * Initializes the annotated methods
- *
- * If the methods set is null, initialize it first. Iterate over all method
- * abstractions and for each annotation, map the method abstraction under the
- * annotation type key.
- */
- private void initAnnotatedMethods()
- {
- if (methods == null)
- {
- initMethods();
- }
- annotatedMethods = new AnnotatedMethodMap();
- for (AnnotatedMethod<Object> member : methods)
- {
- for (Annotation annotation : member.getAnnotations())
- {
- if (!annotatedMethods.containsKey(annotation.annotationType()))
- {
- annotatedMethods.put(annotation.annotationType(), new HashSet<AnnotatedMethod<Object>>());
- }
- annotatedMethods.get(annotation.annotationType()).add(member);
- }
- }
- }
-
- /**
- * Initializes the constructors set and constructors-by-argument map
- *
- * Iterate over the constructors and for each constructor, add an abstracted
- * constructor to the constructors set and the same constructor abstraction
- * to the constructors-by-argument map under the argument-list-key.
- */
- @SuppressWarnings("unchecked")
- private void initConstructors()
- {
- this.constructors = new HashSet<AnnotatedConstructor<T>>();
- this.constructorsByArgumentMap = new ConstructorsByArgumentMap();
- for (Constructor<?> constructor : clazz.getDeclaredConstructors())
- {
- AnnotatedConstructor<T> annotatedConstructor = new AnnotatedConstructorImpl<T>((Constructor<T>) constructor, this);
- if (!constructor.isAccessible())
- {
- constructor.setAccessible(true);
- }
- constructors.add(annotatedConstructor);
- constructorsByArgumentMap.put(Arrays.asList(constructor.getParameterTypes()), annotatedConstructor);
- }
- }
-
- /**
* Gets constructors with given annotation type
*
* @param annotationType The annotation type to match
@@ -544,42 +472,10 @@
*/
public Set<AnnotatedConstructor<T>> getAnnotatedConstructors(Class<? extends Annotation> annotationType)
{
- if (annotatedConstructors == null)
- {
- initAnnotatedConstructors();
- }
-
- return annotatedConstructors.get(annotationType);
+ return Collections.unmodifiableSet(annotatedConstructors.get(annotationType));
}
/**
- * Initializes the annotated constructors.
- *
- * If the constructors set is empty, initialize it first. Iterate over all
- * constructor abstractions and for each annotation on the constructor, map
- * the constructor abstraction under the annotation key.
- */
- private void initAnnotatedConstructors()
- {
- if (constructors == null)
- {
- initConstructors();
- }
- annotatedConstructors = new AnnotatedConstructorMap();
- for (AnnotatedConstructor<T> constructor : constructors)
- {
- for (Annotation annotation : constructor.getAnnotations())
- {
- if (!annotatedConstructors.containsKey(annotation.annotationType()))
- {
- annotatedConstructors.put(annotation.annotationType(), new HashSet<AnnotatedConstructor<T>>());
- }
- annotatedConstructors.get(annotation.annotationType()).add(constructor);
- }
- }
- }
-
- /**
* Gets a constructor with given arguments
*
* @param arguments The arguments to match
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedConstructorImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedConstructorImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedConstructorImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -22,6 +22,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import javax.webbeans.ExecutionException;
@@ -43,15 +44,15 @@
// The type arguments
private static final Type[] actualTypeArguments = new Type[0];
// The underlying constructor
- private Constructor<T> constructor;
+ private final Constructor<T> constructor;
// The list of parameter abstractions
- private List<AnnotatedParameter<Object>> parameters;
+ private final List<AnnotatedParameter<Object>> parameters;
// The mapping of annotation -> parameter abstraction
- private AnnotatedParameterMap annotatedParameters;
+ private final AnnotatedParameterMap annotatedParameters;
// The declaring class abstraction
- private AnnotatedType<T> declaringClass;
+ private final AnnotatedType<T> declaringClass;
/**
* Constructor
@@ -63,9 +64,37 @@
*/
public AnnotatedConstructorImpl(Constructor<T> constructor, AnnotatedType<T> declaringClass)
{
- super(buildAnnotationMap(constructor));
+ super(buildAnnotationMap(constructor), constructor);
this.constructor = constructor;
this.declaringClass = declaringClass;
+
+ this.parameters = new ArrayList<AnnotatedParameter<Object>>();
+ annotatedParameters = new AnnotatedParameterMap();
+ for (int i = 0; i < constructor.getParameterTypes().length; i++)
+ {
+ if (constructor.getParameterAnnotations()[i].length > 0)
+ {
+ Class<? extends Object> clazz = constructor.getParameterTypes()[i];
+ AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(constructor.getParameterAnnotations()[i], (Class<Object>) clazz);
+ parameters.add(parameter);
+
+ for (Annotation annotation : parameter.getAnnotations())
+ {
+ annotatedParameters.put(annotation.annotationType(), parameter);
+ }
+ }
+ else
+ {
+ Class<? extends Object> clazz = constructor.getParameterTypes()[i];
+ AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(new Annotation[0], (Class<Object>) clazz);
+ parameters.add(parameter);
+
+ for (Annotation annotation : parameter.getAnnotations())
+ {
+ annotatedParameters.put(annotation.annotationType(), parameter);
+ }
+ }
+ }
}
/**
@@ -121,41 +150,10 @@
*/
public List<AnnotatedParameter<Object>> getParameters()
{
- if (parameters == null)
- {
- initParameters();
- }
- return parameters;
+ return Collections.unmodifiableList(parameters);
}
/**
- * Initializes the parameter abstractions
- *
- * Iterates over the constructor parameters, adding the parameter abstraction
- * to the parameters list.
- */
- @SuppressWarnings("unchecked")
- private void initParameters()
- {
- parameters = new ArrayList<AnnotatedParameter<Object>>();
- for (int i = 0; i < constructor.getParameterTypes().length; i++)
- {
- if (constructor.getParameterAnnotations()[i].length > 0)
- {
- Class<? extends Object> clazz = constructor.getParameterTypes()[i];
- AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(constructor.getParameterAnnotations()[i], (Class<Object>) clazz);
- parameters.add(parameter);
- }
- else
- {
- Class<? extends Object> clazz = constructor.getParameterTypes()[i];
- AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(new Annotation[0], (Class<Object>) clazz);
- parameters.add(parameter);
- }
- }
- }
-
- /**
* Gets the parameter abstractions with a given annotation type
*
* if the annotated parameters map is null, it is initialized first.
@@ -166,37 +164,10 @@
*/
public List<AnnotatedParameter<Object>> getAnnotatedMethods(Class<? extends Annotation> annotationType)
{
- if (annotatedParameters == null)
- {
- initAnnotatedParameters();
- }
- return annotatedParameters.get(annotationType);
+ return Collections.unmodifiableList(annotatedParameters.get(annotationType));
}
/**
- * Initializes the annotated parameters
- *
- * If the parameters are null, they are initialized first. Iterate over the
- * parameters and for each parameter annotation map it under the annotation
- * type.
- */
- private void initAnnotatedParameters()
- {
- if (parameters == null)
- {
- initParameters();
- }
- annotatedParameters = new AnnotatedParameterMap();
- for (AnnotatedParameter<Object> parameter : parameters)
- {
- for (Annotation annotation : parameter.getAnnotations())
- {
- annotatedParameters.put(annotation.annotationType(), parameter);
- }
- }
- }
-
- /**
* Gets parameter abstractions with a given annotation type.
*
* If the parameters are null, they are initializes first.
@@ -209,11 +180,7 @@
*/
public List<AnnotatedParameter<Object>> getAnnotatedParameters(Class<? extends Annotation> annotationType)
{
- if (annotatedParameters == null)
- {
- initAnnotatedParameters();
- }
- return annotatedParameters.get(annotationType);
+ return Collections.unmodifiableList(annotatedParameters.get(annotationType));
}
/**
@@ -262,7 +229,7 @@
if (super.equals(other) && other instanceof AnnotatedConstructor)
{
AnnotatedConstructor<?> that = (AnnotatedConstructor<?>) other;
- return this.getDelegate().equals(that.getDelegate());
+ return this.getDeclaringClass().equals(that.getDeclaringClass()) && this.getParameters().equals(that.getParameters());
}
return false;
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedFieldImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedFieldImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedFieldImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -29,6 +29,8 @@
/**
* Represents an annotated field
*
+ * This class is immutable, and thus threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -36,11 +38,11 @@
public class AnnotatedFieldImpl<T> extends AbstractAnnotatedMember<T, Field> implements AnnotatedField<T>
{
// The actual type arguments
- private Type[] actualTypeArguments = new Type[0];
+ private final Type[] actualTypeArguments;
// The underlying field
- private Field field;
+ private final Field field;
// The abstraction of the declaring class
- private AnnotatedType<?> declaringClass;
+ private final AnnotatedType<?> declaringClass;
/**
* Constructor
@@ -53,7 +55,7 @@
*/
public AnnotatedFieldImpl(Field field, AnnotatedType<?> declaringClass)
{
- super(buildAnnotationMap(field));
+ super(buildAnnotationMap(field), field);
this.field = field;
this.declaringClass = declaringClass;
if (field.getGenericType() instanceof ParameterizedType)
@@ -61,6 +63,10 @@
ParameterizedType type = (ParameterizedType) field.getGenericType();
actualTypeArguments = type.getActualTypeArguments();
}
+ else
+ {
+ actualTypeArguments = new Type[0];
+ }
}
/**
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedItemImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedItemImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedItemImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -18,14 +18,13 @@
package org.jboss.webbeans.introspector.jlr;
import java.lang.annotation.Annotation;
-import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
-import javax.webbeans.TypeLiteral;
-
/**
* Represents an annotated item
*
+ * This class is immutable, and thus threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -34,115 +33,24 @@
public class AnnotatedItemImpl<T, S> extends AbstractAnnotatedItem<T, S>
{
// The actual type arguments
- private Type[] actualTypeArguments = new Type[0];
+ private final Type[] actualTypeArguments;
// The type of the item
- private Class<T> type;
+ private final Class<T> type;
// The actual annotations
- private Annotation[] actualAnnotations;
+ private final Annotation[] actualAnnotations;
/**
* Constructor
*
- * Initializes the superclass with the annotation map
- *
- * @param annotationMap The annotated map
- */
- private AnnotatedItemImpl(AnnotationMap annotationMap)
- {
- super(annotationMap);
- }
-
- /**
- * Constructor
- *
- * Initializes the superclass with the annotation map and sets the type
- *
- * @param annotationMap The annotation map
- * @param type The type of the item
- */
- private AnnotatedItemImpl(AnnotationMap annotationMap, Class<T> type)
- {
- super(annotationMap);
- this.type = type;
- }
-
- /**
- * Constructor
- *
- * Initializes the superclass with the annotation map, sets the api type and
- * determines the type arguments
- *
- * @param annotationMap The annotation map
- * @param apiType The api type
- */
- private AnnotatedItemImpl(AnnotationMap annotationMap, TypeLiteral<T> apiType)
- {
- super(annotationMap);
- this.type = apiType.getRawType();
- if (apiType.getType() instanceof ParameterizedType)
- {
- actualTypeArguments = ((ParameterizedType) apiType.getType()).getActualTypeArguments();
- }
- }
-
- /**
- * Constructor
- *
- * @param annotationMap The annotation map
- * @param type The type
- * @param actualTypeArguments The actual type arguments
- */
- private AnnotatedItemImpl(AnnotationMap annotationMap, Class<T> type, Type[] actualTypeArguments)
- {
- this(annotationMap, type);
- this.actualTypeArguments = actualTypeArguments;
- }
-
- /**
- * Constructor
- *
* @param annotations The annotations array of the type
- */
- public AnnotatedItemImpl(Annotation[] annotations)
- {
- this(buildAnnotationMap(annotations));
- this.actualAnnotations = annotations;
- }
-
- /**
- * Constructor
- *
- * @param annotations The annotations array of the type
- * @param type The type
- */
- public AnnotatedItemImpl(Annotation[] annotations, Class<T> type)
- {
- this(buildAnnotationMap(annotations), type);
- this.actualAnnotations = annotations;
- }
-
- /**
- * Constructor
- *
- * @param annotations The annotations array of the type
- * @param apiType The API typeliteral
- */
- public AnnotatedItemImpl(Annotation[] annotations, TypeLiteral<T> apiType)
- {
- this(buildAnnotationMap(annotations), apiType);
- this.actualAnnotations = annotations;
- }
-
- /**
- * Constructor
- *
- * @param annotations The annotations array of the type
* @param type The type of the item
* @param actualTypeArguments The actual type arguments array
*/
public AnnotatedItemImpl(Annotation[] annotations, Class<T> type, Type[] actualTypeArguments)
{
- this(buildAnnotationMap(annotations), type, actualTypeArguments);
+ super(buildAnnotationMap(annotations));
+ this.type = type;
+ this.actualTypeArguments = actualTypeArguments;
this.actualAnnotations = annotations;
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedMethodImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -22,6 +22,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.jboss.webbeans.ManagerImpl;
@@ -33,6 +34,8 @@
/**
* Represents an annotated method
*
+ * This class is immutable and thus threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -40,21 +43,21 @@
public class AnnotatedMethodImpl<T> extends AbstractAnnotatedMember<T, Method> implements AnnotatedMethod<T>
{
// The actual type arguments
- private Type[] actualTypeArguments = new Type[0];
+ private final Type[] actualTypeArguments;
// The underlying method
- private Method method;
+ private final Method method;
// The abstracted parameters
- private List<AnnotatedParameter<Object>> parameters;
+ private final List<AnnotatedParameter<Object>> parameters;
// A mapping from annotation type to parameter abstraction with that
// annotation present
- private AnnotatedParameterMap annotatedParameters;
+ private final AnnotatedParameterMap annotatedParameters;
// The property name
- private String propertyName;
+ private final String propertyName;
// The abstracted declaring class
- private AnnotatedType<?> declaringClass;
+ private final AnnotatedType<?> declaringClass;
/**
* Constructor
@@ -67,13 +70,53 @@
*/
public AnnotatedMethodImpl(Method method, AnnotatedType<?> declaringClass)
{
- super(buildAnnotationMap(method));
+ super(buildAnnotationMap(method), method);
this.method = method;
this.declaringClass = declaringClass;
if (method.getGenericReturnType() instanceof ParameterizedType)
{
- actualTypeArguments = ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments();
+ this.actualTypeArguments = ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments();
}
+ else
+ {
+ this.actualTypeArguments = new Type[0];
+ }
+
+ this.parameters = new ArrayList<AnnotatedParameter<Object>>();
+ this.annotatedParameters = new AnnotatedParameterMap();
+ for (int i = 0; i < method.getParameterTypes().length; i++)
+ {
+ if (method.getParameterAnnotations()[i].length > 0)
+ {
+ Class<? extends Object> clazz = method.getParameterTypes()[i];
+ AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(method.getParameterAnnotations()[i], (Class<Object>) clazz);
+ this.parameters.add(parameter);
+ for (Annotation annotation : parameter.getAnnotations())
+ {
+ annotatedParameters.put(annotation.annotationType(), parameter);
+ }
+ }
+ else
+ {
+ Class<? extends Object> clazz = method.getParameterTypes()[i];
+ AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(new Annotation[0], (Class<Object>) clazz);
+ this.parameters.add(parameter);
+ for (Annotation annotation : parameter.getAnnotations())
+ {
+ annotatedParameters.put(annotation.annotationType(), parameter);
+ }
+ }
+ }
+
+ String propertyName = Reflections.getPropertyName(getDelegate());
+ if (propertyName == null)
+ {
+ this.propertyName = getName();
+ }
+ else
+ {
+ this.propertyName = propertyName;
+ }
}
/**
@@ -130,83 +173,10 @@
*/
public List<AnnotatedParameter<Object>> getParameters()
{
- if (parameters == null)
- {
- initParameters();
- }
- return parameters;
+ return Collections.unmodifiableList(parameters);
}
/**
- * Initializes the parameter abstractions
- *
- * Iterates over the method abstraction parameters and creates an abstraction
- * of the parameter
- */
- @SuppressWarnings("unchecked")
- private void initParameters()
- {
- this.parameters = new ArrayList<AnnotatedParameter<Object>>();
- for (int i = 0; i < method.getParameterTypes().length; i++)
- {
- if (method.getParameterAnnotations()[i].length > 0)
- {
- Class<? extends Object> clazz = method.getParameterTypes()[i];
- AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(method.getParameterAnnotations()[i], (Class<Object>) clazz);
- parameters.add(parameter);
- }
- else
- {
- Class<? extends Object> clazz = method.getParameterTypes()[i];
- AnnotatedParameter<Object> parameter = new AnnotatedParameterImpl<Object>(new Annotation[0], (Class<Object>) clazz);
- parameters.add(parameter);
- }
- }
- }
-
- // TODO: Don't get this one - NIK
- // public List<AnnotatedParameter<Object>> getAnnotatedMethods(Class<?
- // extends Annotation> annotationType)
- // {
- // if (annotatedParameters == null)
- // {
- // initAnnotatedParameters();
- // }
- //
- // if (!annotatedParameters.containsKey(annotationType))
- // {
- // return new ArrayList<AnnotatedParameter<Object>>();
- // }
- // else
- // {
- // return annotatedParameters.get(annotationType);
- // }
- // }
-
- /**
- * Initializes the annotated parameters
- *
- * If the parameters are null, they are initialized first. Iterates over the
- * parameter abstractions and for each annotation present, maps the parameter
- * abstraction under that annotation type key.
- */
- private void initAnnotatedParameters()
- {
- if (parameters == null)
- {
- initParameters();
- }
- annotatedParameters = new AnnotatedParameterMap();
- for (AnnotatedParameter<Object> parameter : parameters)
- {
- for (Annotation annotation : parameter.getAnnotations())
- {
- annotatedParameters.put(annotation.annotationType(), parameter);
- }
- }
- }
-
- /**
* Gets the parameter abstractions with a given annotation type
*
* If the parameter abstractions are null, they are initialized first
@@ -217,11 +187,7 @@
*/
public List<AnnotatedParameter<Object>> getAnnotatedParameters(Class<? extends Annotation> annotationType)
{
- if (annotatedParameters == null)
- {
- initAnnotatedParameters();
- }
- return annotatedParameters.get(annotationType);
+ return Collections.unmodifiableList(annotatedParameters.get(annotationType));
}
/**
@@ -235,7 +201,7 @@
if (other instanceof AnnotatedMethod)
{
AnnotatedMethod<?> that = (AnnotatedMethod<?>) other;
- return this.getDelegate().equals(that.getDelegate());
+ return this.getDeclaringClass().equals(that.getDeclaringClass()) && this.getName().equals(that.getName()) && this.getParameters().equals(that.getParameters());
}
else
{
@@ -293,14 +259,6 @@
*/
public String getPropertyName()
{
- if (propertyName == null)
- {
- propertyName = Reflections.getPropertyName(getDelegate());
- if (propertyName == null)
- {
- propertyName = getName();
- }
- }
return propertyName;
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedParameterImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedParameterImpl.java 2008-12-03 11:46:14 UTC (rev 392)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedParameterImpl.java 2008-12-03 17:51:11 UTC (rev 393)
@@ -28,6 +28,8 @@
/**
* Represents a parameter
*
+ * This class is immutable and thus threadsafe
+ *
* @author Pete Muir
*
* @param <T>
@@ -35,13 +37,13 @@
public class AnnotatedParameterImpl<T> extends AbstractAnnotatedItem<T, Object> implements AnnotatedParameter<T>
{
// The type
- private Class<T> type;
+ private final Class<T> type;
// The actual type arguments
- private Type[] actualTypeArguments = new Type[0];
+ private final Type[] actualTypeArguments = new Type[0];
// The final state
- private boolean _final;
+ private final boolean _final = false;
// The static state
- private boolean _static;
+ private final boolean _static = false;
/**
* Constructor
16 years
[webbeans-commits] Webbeans SVN: r392 - in ri/trunk/webbeans-ri/src: test/java/org/jboss/webbeans/test and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-03 06:46:14 -0500 (Wed, 03 Dec 2008)
New Revision: 392
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
Log:
fix tests, change exception handling in proxy pool
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-03 11:40:01 UTC (rev 391)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-03 11:46:14 UTC (rev 392)
@@ -28,7 +28,6 @@
import javassist.util.proxy.ProxyObject;
import javax.webbeans.DefinitionException;
-import javax.webbeans.UnproxyableDependencyException;
import javax.webbeans.manager.Bean;
import org.jboss.webbeans.ManagerImpl;
@@ -146,13 +145,25 @@
* @throws IllegalAccessException When the proxy couldn't be created
*/
@SuppressWarnings("unchecked")
- private <T> T createClientProxy(Bean<T> bean, int beanIndex) throws InstantiationException, IllegalAccessException
+ private <T> T createClientProxy(Bean<T> bean, int beanIndex) throws RuntimeException
{
ProxyFactory proxyFactory = new ProxyFactory();
TypeInfo typeInfo = getTypeInfo(bean.getTypes());
proxyFactory.setInterfaces(typeInfo.interfaces);
proxyFactory.setSuperclass(typeInfo.superclass);
- T clientProxy = (T) proxyFactory.createClass().newInstance();
+ T clientProxy;
+ try
+ {
+ clientProxy = (T) proxyFactory.createClass().newInstance();
+ }
+ catch (InstantiationException e)
+ {
+ throw new RuntimeException("Could not instantiate client proxy for " + bean, e);
+ }
+ catch (IllegalAccessException e)
+ {
+ throw new RuntimeException("Could not access bean correctly when creating client proxy for " + bean, e);
+ }
ProxyMethodHandler proxyMethodHandler = new ProxyMethodHandler(bean, beanIndex, manager);
((ProxyObject) clientProxy).setHandler(proxyMethodHandler);
return clientProxy;
@@ -172,24 +183,13 @@
Object clientProxy = pool.get(bean);
if (clientProxy == null)
{
- try
+ int beanIndex = manager.getBeans().indexOf(bean);
+ if (beanIndex < 0)
{
- int beanIndex = manager.getBeans().indexOf(bean);
- if (beanIndex < 0)
- {
- throw new DefinitionException(bean + " is not known to the manager");
- }
- clientProxy = createClientProxy(bean, beanIndex);
+ throw new DefinitionException(bean + " is not known to the manager");
}
- catch (DefinitionException e)
- {
- throw e;
- }
- catch (Exception e)
- {
- // TODO: What to *really* do here?
- throw new UnproxyableDependencyException("Could not create client proxy for " + bean.getName(), e);
- }
+ clientProxy = createClientProxy(bean, beanIndex);
+
pool.put(bean, clientProxy);
}
return clientProxy;
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-12-03 11:40:01 UTC (rev 391)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-12-03 11:46:14 UTC (rev 392)
@@ -27,6 +27,7 @@
public void testReflectionsUsedForNormalScope()
{
Bean<Tuna> tunaBean = BeanFactory.createSimpleBean(Tuna.class, manager);
+ manager.addBean(tunaBean);
Tuna tuna = manager.getInstance(tunaBean);
assert Reflections.isProxy(tuna);
}
@@ -57,6 +58,7 @@
public void testSimpleWebBeanReflectionsIsSerializable() throws IOException, ClassNotFoundException
{
Bean<TunedTuna> tunaBean = BeanFactory.createSimpleBean(TunedTuna.class, manager);
+ manager.addBean(tunaBean);
TunedTuna tuna = manager.getInstance(tunaBean);
assert Reflections.isProxy(tuna);
byte[] bytes = serializeBean(tuna);
16 years
[webbeans-commits] Webbeans SVN: r391 - in ri/trunk/webbeans-ri/src: test/java/org/jboss/webbeans/test and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-03 06:40:01 -0500 (Wed, 03 Dec 2008)
New Revision: 391
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
Log:
implicit add -> fail when getting instance of bean not registered in manager
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-02 22:26:39 UTC (rev 390)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/proxy/ProxyPool.java 2008-12-03 11:40:01 UTC (rev 391)
@@ -27,6 +27,7 @@
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
+import javax.webbeans.DefinitionException;
import javax.webbeans.UnproxyableDependencyException;
import javax.webbeans.manager.Bean;
@@ -174,15 +175,16 @@
try
{
int beanIndex = manager.getBeans().indexOf(bean);
- // Implicit add required since it is looked up on activation with
- // then index
if (beanIndex < 0)
{
- manager.addBean(bean);
- beanIndex = manager.getBeans().size() - 1;
+ throw new DefinitionException(bean + " is not known to the manager");
}
clientProxy = createClientProxy(bean, beanIndex);
}
+ catch (DefinitionException e)
+ {
+ throw e;
+ }
catch (Exception e)
{
// TODO: What to *really* do here?
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-12-02 22:26:39 UTC (rev 390)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-12-03 11:40:01 UTC (rev 391)
@@ -6,6 +6,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import javax.webbeans.DefinitionException;
import javax.webbeans.UnproxyableDependencyException;
import javax.webbeans.manager.Bean;
@@ -85,11 +86,11 @@
assert tuna.getState().equals("tuned");
}
- @Test(groups = "Reflections")
- public void testProxyCreationDoesImplicitAddBean() {
+ @Test(groups = "Reflections", expectedExceptions=DefinitionException.class)
+ public void testGettingUnknownBeanFails() {
Bean<Tuna> tunaBean = BeanFactory.createSimpleBean(Tuna.class, manager);
Tuna tuna = manager.getInstance(tunaBean);
- assert manager.getBeans().size() == 2;
+ assert false;
}
}
16 years
[webbeans-commits] Webbeans SVN: r390 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: event and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-02 17:26:39 -0500 (Tue, 02 Dec 2008)
New Revision: 390
Removed:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/TransactionObservationPhase.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
Log:
refactorings in events
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -29,6 +29,7 @@
import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.DeploymentType;
+import javax.webbeans.Event;
import javax.webbeans.Named;
import javax.webbeans.Production;
import javax.webbeans.ScopeType;
@@ -60,7 +61,7 @@
{
@SuppressWarnings("unchecked")
- private static Set<Class<?>> STANDARD_WEB_BEAN_CLASSES = new HashSet<Class<?>>(Arrays.asList(DefaultEnterpriseBeanLookup.class));
+ private static Set<Class<?>> STANDARD_WEB_BEAN_CLASSES = new HashSet<Class<?>>(Arrays.asList(DefaultEnterpriseBeanLookup.class, Event.class));
/**
* Helper class for getting deployment type
@@ -387,7 +388,7 @@
}
else if (deploymentType.equals(Standard.class) && !STANDARD_WEB_BEAN_CLASSES.contains(getAnnotatedItem().getType()))
{
- throw new DefinitionException(getAnnotatedItem() + " cannot have deployment type @Standard");
+ throw new DefinitionException(getAnnotatedItem().getName() + " cannot have deployment type @Standard");
}
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -18,8 +18,11 @@
package org.jboss.webbeans.bean;
import java.lang.reflect.Field;
+import java.lang.reflect.Type;
+import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
+import javax.webbeans.Event;
import javax.webbeans.Standard;
import org.jboss.webbeans.ManagerImpl;
@@ -36,7 +39,7 @@
*
* @param <T>
*/
-public class EventBean<T> extends AbstractBean<EventImpl<T>, Field>
+public class EventBean<T> extends AbstractBean<Event<T>, Field>
{
private static LogProvider log = Logging.getLogProvider(EventBean.class);
@@ -44,7 +47,7 @@
// The debug location
private String location;
// The underlying annotated item
- private AnnotatedField<EventImpl<T>> annotatedItem;
+ private AnnotatedField<Event<T>> annotatedItem;
/**
* Constructor
@@ -57,7 +60,7 @@
public EventBean(AnnotatedField<T> field, ManagerImpl manager)
{
super(manager);
- this.annotatedItem = (AnnotatedField<EventImpl<T>>) field;
+ this.annotatedItem = (AnnotatedField<Event<T>>) field;
init();
}
@@ -77,7 +80,15 @@
*/
private void checkAnnotatedItem()
{
- // TODO: checks
+ Type[] actualTypeArguments = annotatedItem.getActualTypeArguments();
+ if (actualTypeArguments.length != 1)
+ {
+ throw new DefinitionException("Event must have type arguments");
+ }
+ if (!(actualTypeArguments[0] instanceof Class))
+ {
+ throw new DefinitionException("Event must have concrete type argument");
+ }
}
/**
@@ -102,7 +113,7 @@
* @see org.jboss.webbeans.bean.AbstractBean#getAnnotatedItem()
*/
@Override
- protected AnnotatedItem<EventImpl<T>, Field> getAnnotatedItem()
+ protected AnnotatedItem<Event<T>, Field> getAnnotatedItem()
{
return annotatedItem;
}
@@ -154,7 +165,7 @@
* @see javax.webbeans.manager.Bean#create()
*/
@Override
- public EventImpl<T> create()
+ public Event<T> create()
{
return new EventImpl<T>(getManager(), annotatedItem.getBindingTypesAsArray());
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -21,6 +21,11 @@
import javax.transaction.Synchronization;
import javax.webbeans.Observer;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_COMPLETION;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_SUCCESS;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_FAILURE;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.BEFORE_COMPLETION;
+
/**
* A synchronization object which will deliver the event to the observer after
* the JTA transaction currently in effect is committed.
@@ -69,20 +74,20 @@
*/
public void afterCompletion(int status)
{
- if (observer.isInterestedInTransactionPhase(TransactionObservationPhase.AFTER_COMPLETION))
+ if (observer.isInterestedInTransactionPhase(AFTER_COMPLETION))
{
observer.notify(event);
}
switch (status)
{
case Status.STATUS_COMMITTED:
- if (observer.isInterestedInTransactionPhase(TransactionObservationPhase.AFTER_SUCCESS))
+ if (observer.isInterestedInTransactionPhase(AFTER_SUCCESS))
{
observer.notify();
}
break;
case Status.STATUS_ROLLEDBACK:
- if (observer.isInterestedInTransactionPhase(TransactionObservationPhase.AFTER_FAILURE))
+ if (observer.isInterestedInTransactionPhase(AFTER_FAILURE))
{
observer.notify();
}
@@ -98,7 +103,7 @@
*/
public void beforeCompletion()
{
- if (observer.isInterestedInTransactionPhase(TransactionObservationPhase.BEFORE_COMPLETION))
+ if (observer.isInterestedInTransactionPhase(BEFORE_COMPLETION))
{
observer.notify(event);
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -22,10 +22,12 @@
import java.util.Set;
import javax.webbeans.BindingType;
+import javax.webbeans.Dependent;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
import javax.webbeans.Observable;
import javax.webbeans.Observer;
+import javax.webbeans.Standard;
import org.jboss.webbeans.ManagerImpl;
@@ -37,6 +39,8 @@
* @param <T>
* @see javax.webbeans.Event
*/
+@Standard
+@Dependent
public class EventImpl<T> implements Event<T>
{
// The set of binding types
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -46,6 +46,15 @@
public class EventManager
{
/**
+ * The known transactional phases a transactional event observer can be
+ * interested in
+ */
+ protected enum TransactionObservationPhase
+ {
+ NONE, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, AFTER_SUCCESS
+ }
+
+ /**
* An event type -> observer list map
*/
private class RegisteredObserversMap extends ForwardingMap<Class<?>, List<EventObserver<?>>>
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -31,6 +31,13 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.EventBean;
+import org.jboss.webbeans.event.EventManager.TransactionObservationPhase;
+
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_COMPLETION;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_FAILURE;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_SUCCESS;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.BEFORE_COMPLETION;
+import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.NONE;
import org.jboss.webbeans.introspector.AnnotatedMethod;
/**
@@ -80,19 +87,19 @@
List<TransactionObservationPhase> observationPhases = new ArrayList<TransactionObservationPhase>();
if (observerMethod.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty())
{
- observationPhases.add(TransactionObservationPhase.BEFORE_COMPLETION);
+ observationPhases.add(BEFORE_COMPLETION);
}
if (observerMethod.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty())
{
- observationPhases.add(TransactionObservationPhase.AFTER_COMPLETION);
+ observationPhases.add(AFTER_COMPLETION);
}
if (observerMethod.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty())
{
- observationPhases.add(TransactionObservationPhase.AFTER_FAILURE);
+ observationPhases.add(AFTER_FAILURE);
}
if (observerMethod.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty())
{
- observationPhases.add(TransactionObservationPhase.AFTER_SUCCESS);
+ observationPhases.add(AFTER_SUCCESS);
}
if (observationPhases.size() > 1)
{
@@ -104,7 +111,7 @@
}
else
{
- transactionObservationPhase = TransactionObservationPhase.NONE;
+ transactionObservationPhase = NONE;
}
}
Deleted: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/TransactionObservationPhase.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/TransactionObservationPhase.java 2008-12-02 13:24:58 UTC (rev 389)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/TransactionObservationPhase.java 2008-12-02 22:26:39 UTC (rev 390)
@@ -1,29 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jboss.webbeans.event;
-
-/**
- * The known transactional phases a transactional event observer can be
- * interested in
- *
- * @author Nicklas Karlsson
- */
-public enum TransactionObservationPhase
-{
- NONE, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, AFTER_SUCCESS
-}
16 years
[webbeans-commits] Webbeans SVN: r389 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: introspector/jlr and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-02 08:24:58 -0500 (Tue, 02 Dec 2008)
New Revision: 389
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
Log:
minor misses, volume I
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java 2008-12-02 08:16:38 UTC (rev 388)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java 2008-12-02 13:24:58 UTC (rev 389)
@@ -25,6 +25,7 @@
import javax.webbeans.Destructor;
import javax.webbeans.Disposes;
import javax.webbeans.Initializer;
+import javax.webbeans.Observable;
import javax.webbeans.Observes;
import javax.webbeans.Produces;
@@ -116,7 +117,7 @@
*/
public Set<AnnotatedField<Object>> getEventFields()
{
- return getAnnotatedItem().getAnnotatedFields(Observes.class);
+ return getAnnotatedItem().getAnnotatedFields(Observable.class);
}
/**
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-02 08:16:38 UTC (rev 388)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-02 13:24:58 UTC (rev 389)
@@ -407,7 +407,7 @@
{
for (Annotation annotation : field.getAnnotations())
{
- annotatedFields.get(annotation.annotationType()).add(field);
+ annotatedFields.put(annotation.annotationType(), field);
for (Annotation metaAnnotation : annotation.annotationType().getAnnotations())
{
metaAnnotatedFields.put(metaAnnotation.annotationType(), field);
16 years
[webbeans-commits] Webbeans SVN: r388 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/event and 2 other directories.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-02 03:16:38 -0500 (Tue, 02 Dec 2008)
New Revision: 388
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanFactory.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
Log:
EventBean/EventImpl hammering
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-02 07:19:21 UTC (rev 387)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-02 08:16:38 UTC (rev 388)
@@ -26,7 +26,6 @@
import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedItem;
-import org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
@@ -42,9 +41,18 @@
private static LogProvider log = Logging.getLogProvider(EventBean.class);
+ // The debug location
private String location;
+ // The underlying annotated item
private AnnotatedField<EventImpl<T>> annotatedItem;
+ /**
+ * Constructor
+ *
+ * @param field The underlying field abstraction
+ * @param declaringBean
+ * @param manager The Web Beans manager
+ */
@SuppressWarnings("unchecked")
public EventBean(AnnotatedField<T> field, ManagerImpl manager)
{
@@ -58,106 +66,97 @@
*
* Calls super method and validates the annotated item
*/
- protected void init() {
+ protected void init()
+ {
super.init();
checkAnnotatedItem();
}
-
+
/**
* Validates the annotated item
*/
- private void checkAnnotatedItem() {
+ private void checkAnnotatedItem()
+ {
// TODO: checks
}
-
+
/**
- * Caches the constructor for this type of bean to avoid future reflections
- * during use.
+ * @see org.jboss.webbeans.bean.AbstractBean#initScopeType()
*/
- @SuppressWarnings("unused")
- private void initConstructor()
+ @Override
+ protected void initScopeType()
{
- try
- {
- // constructor = new SimpleConstructor<T>((Constructor<T>)
- // EventImpl.class.getConstructor((Class[])null));
- }
- catch (Exception e)
- {
- log.warn("Unable to get constructor for build-in Event implementation", e);
- }
+ this.scopeType = Dependent.class;
}
- /*
- * public BeanConstructor<T> getConstructor() { return constructor; }
+ /**
+ * @see org.jboss.webbeans.bean.AbstractBean#initDeploymentType()
*/
-
- public String getLocation()
- {
- if (location == null)
- {
- location = "type: Event Bean;";
- }
- return location;
- }
-
@Override
- public String toString()
+ protected void initDeploymentType()
{
- return "EventBean[" + getType().getName() + "]";
+ this.deploymentType = Standard.class;
}
+ /**
+ * @see org.jboss.webbeans.bean.AbstractBean#getAnnotatedItem()
+ */
@Override
- protected void initType()
- {
- log.trace("Bean type specified in Java");
- this.type = annotatedItem.getType();
- }
-
- @Override
protected AnnotatedItem<EventImpl<T>, Field> getAnnotatedItem()
{
return annotatedItem;
}
+ /**
+ * @see org.jboss.webbeans.bean.AbstractBean#getDefaultName()
+ */
@Override
protected String getDefaultName()
{
- // No name per 7.4
return null;
}
+ /**
+ * @see org.jboss.webbeans.bean.AbstractBean#initType()
+ */
@Override
- protected void initDeploymentType()
+ protected void initType()
{
- // This is always @Standard per 7.4
- this.deploymentType = Standard.class;
+ try
+ {
+ if (getAnnotatedItem() != null)
+ {
+ this.type = getAnnotatedItem().getType();
+ }
+ }
+ catch (ClassCastException e)
+ {
+ // TODO: Expand error
+ throw new IllegalArgumentException("Type mismatch");
+ }
}
- @Override
- protected void checkDeploymentType()
+ /**
+ * Gets the debug location
+ *
+ * @return A string describing the location
+ */
+ private String getLocation()
{
- // No - op
+ if (location == null)
+ {
+ location = "type: Event Bean;";
+ }
+ return location;
}
+ /**
+ * @see javax.webbeans.manager.Bean#create()
+ */
@Override
- protected void initName()
- {
- // No name per 7.4
- this.name = null;
- }
-
- @Override
- protected void initScopeType()
- {
- // This is always @Dependent per 7.4
- this.scopeType = Dependent.class;
- }
-
- @Override
public EventImpl<T> create()
{
- return new EventImpl<T>();
+ return new EventImpl<T>(getManager(), annotatedItem.getBindingTypesAsArray());
}
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-02 07:19:21 UTC (rev 387)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-12-02 08:16:38 UTC (rev 388)
@@ -18,157 +18,100 @@
package org.jboss.webbeans.event;
import java.lang.annotation.Annotation;
-import java.util.Arrays;
-import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.webbeans.BindingType;
-import javax.webbeans.Current;
-import javax.webbeans.Dependent;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
+import javax.webbeans.Observable;
import javax.webbeans.Observer;
-import javax.webbeans.Standard;
-import javax.webbeans.manager.Manager;
+import org.jboss.webbeans.ManagerImpl;
+
/**
- * Implementation of the {@link Event} interface used for the container provided
- * Web Bean to be injected for an observable event. See section 7.4 of the JSR
- * for more details on how this bean is provided by the container and used.
+ * Implementation of the Event interface
*
* @author David Allen
*
+ * @param <T>
+ * @see javax.webbeans.Event
*/
-@Standard
-@Dependent
public class EventImpl<T> implements Event<T>
{
- private Collection<? extends Annotation> eventBindings;
+ // The set of binding types
+ private Set<? extends Annotation> bindingTypes;
+ // The event type
private Class<T> eventType;
+ // The Web Beans manager
+ protected ManagerImpl manager;
- // The current WB manager
- @Current
- protected Manager webBeansManager;
-
/**
- * Used to set the event bindings for this type of event after it is
- * constructed with the default constructor.
+ * Constructor
*
- * @param eventBindings Annotations that are bindings for the event
+ * @param manager The Web Beans manager
+ * @param bindingTypes The binding types
*/
- public void setEventBindings(Annotation... eventBindings)
+ public EventImpl(ManagerImpl manager, Annotation... bindingTypes)
{
- // TODO Use constructor injection
- Set<Annotation> newEventBindings = new HashSet<Annotation>();
- addAnnotationBindings(newEventBindings, eventBindings);
- this.eventBindings = newEventBindings;
+ this.manager = manager;
+ this.bindingTypes = checkBindingTypes(bindingTypes);
}
- /*
- * (non-Javadoc)
- *
- * @see javax.webbeans.Event#fire(java.lang.Object,
- * java.lang.annotation.Annotation[])
- */
- public void fire(T event, Annotation... bindings)
- {
- // Combine the annotations passed here with the annotations (event
- // bindings)
- // specified on the @Observable object.
- Set<Annotation> eventBindings = new HashSet<Annotation>();
- eventBindings.addAll(this.getBindingTypes());
- // eventBindings.addAll(Arrays.asList(bindings));
- addAnnotationBindings(eventBindings, bindings);
-
- // Invoke the container method to fire the event per 7.2
- webBeansManager.fireEvent(event, eventBindings.toArray(new Annotation[0]));
- }
-
- public void observe(Observer<T> observer, Annotation... bindings)
- {
- // Register the observer with the web beans manager
-
- Set<Annotation> eventBindings = new HashSet<Annotation>();
- eventBindings.addAll(this.getBindingTypes());
- addAnnotationBindings(eventBindings, bindings);
- webBeansManager.addObserver(observer, eventType, bindings);
- }
-
/**
- * Adds each of the annotation bindings to the set, but if any binding
- * already exists in the set, a {@link DuplicateBindingTypeException} is
- * thrown.
+ * Validates the binding types
*
- * @param bindingsSet The set of annotation binding objects
- * @param bindings An array of annotation bindings to add to the set
- * @throws DuplicateBindingTypeException if any of bindings are duplicates
- * @throws IllegalArgumentException if any annotation is not a binding type
+ * Removes @Observable from the list
+ *
+ * @param annotations The annotations to validate
+ * @return A set of unique binding type annotations (minus @Observable, if it
+ * was present)
*/
- private void addAnnotationBindings(Set<Annotation> bindingsSet, Annotation[] bindings)
+ private Set<Annotation> checkBindingTypes(Annotation... annotations)
{
- if (bindings != null)
+ Set<Annotation> uniqueAnnotations = new HashSet<Annotation>();
+ for (Annotation annotation : annotations)
{
- Set<Class<? extends Annotation>> bindingTypes = new HashSet<Class<? extends Annotation>>();
- // Add the bindings types that are already in the set being added to.
- // This will
- // provide detection of duplicates across construction and later
- // invocations.
- for (Annotation annotation : bindingsSet)
+ if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
{
- bindingTypes.add(annotation.annotationType());
+ throw new IllegalArgumentException(annotation + " is not a binding type");
}
-
- // Now go through the new annotations being added to make sure these
- // are binding
- // types and are not duplicates
- for (Annotation annotation : bindings)
+ if (uniqueAnnotations.contains(annotation))
{
- // Check that the binding type is indeed a binding type
- Annotation[] bindingAnnotations = annotation.annotationType().getAnnotations();
- boolean isBindingType = false;
- for (Annotation bindingAnnotation : bindingAnnotations)
- {
- if (bindingAnnotation.annotationType().equals(BindingType.class))
- {
- isBindingType = true;
- }
- }
- if (!isBindingType)
- throw new IllegalArgumentException("Annotation " + annotation + " is not a binding type");
-
- // Check that no binding type was specified more than once in the
- // annotations
- if (bindingTypes.contains(annotation.annotationType()))
- {
- throw new DuplicateBindingTypeException();
- }
- else
- {
- bindingTypes.add(annotation.annotationType());
- }
+ throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list");
}
- bindingsSet.addAll(Arrays.asList(bindings));
+ if (!annotation.annotationType().equals(Observable.class))
+ {
+ uniqueAnnotations.add(annotation);
+ }
}
-
+ return uniqueAnnotations;
}
- private Collection<? extends Annotation> getBindingTypes()
+ /**
+ * Fires an event
+ *
+ * @param event The event object
+ * @param bindingTypes Additional binding types
+ */
+ public void fire(T event, Annotation... bindingTypes)
{
- // Get the binding types directly from the model for the bean
- return this.eventBindings;
+ Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
+ bindingParameters.addAll(this.bindingTypes);
+ manager.fireEvent(event, bindingParameters.toArray(new Annotation[0]));
}
- // TODO Remove the setter for the manager once WB injection is working
- public void setManager(Manager manager)
+ /**
+ * Registers an observer
+ *
+ * @param observer
+ * @param bindingTypes Additional binding types
+ */
+ public void observe(Observer<T> observer, Annotation... bindingTypes)
{
- this.webBeansManager = manager;
+ Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
+ bindingParameters.addAll(this.bindingTypes);
+ manager.addObserver(observer, eventType, bindingParameters.toArray(new Annotation[0]));
}
- // TODO Use constructor injection
- public void setEventType(Class<T> eventType)
- {
- this.eventType = eventType;
- }
-
-}
+}
\ No newline at end of file
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanFactory.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanFactory.java 2008-12-02 07:19:21 UTC (rev 387)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanFactory.java 2008-12-02 08:16:38 UTC (rev 388)
@@ -22,10 +22,12 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.AbstractClassBean;
import org.jboss.webbeans.bean.EnterpriseBean;
+import org.jboss.webbeans.bean.EventBean;
import org.jboss.webbeans.bean.ProducerMethodBean;
import org.jboss.webbeans.bean.SimpleBean;
import org.jboss.webbeans.bean.XmlEnterpriseBean;
import org.jboss.webbeans.bean.XmlSimpleBean;
+import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMethod;
/**
@@ -105,9 +107,8 @@
/**
* Creates a producer method Web Bean
*
- * @param <T> The type
- * @param type The class
- * @param method The underlying method
+ * @param type The type
+ * @param method The underlying method abstraction
* @param manager The Web Beans manager
* @param declaringBean The declaring bean abstraction
* @return A producer Web Bean
@@ -117,4 +118,19 @@
return new ProducerMethodBean<T>(method, declaringBean, manager);
}
+ /**
+ * Creates an event Web Bean
+ *
+ * @param <T>
+ * @param type The type
+ * @param field The observer field abstraction
+ * @param manager The Web Beans manager
+ * @param declaringBean The declaring bean abstraction
+ * @return An event Web Bean
+ */
+ public static <T> EventBean<T> createEventBean(Class<T> type, AnnotatedField<T> field, ManagerImpl manager)
+ {
+ return new EventBean<T>(field, manager);
+ }
+
}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java 2008-12-02 07:19:21 UTC (rev 387)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java 2008-12-02 08:16:38 UTC (rev 388)
@@ -2,16 +2,7 @@
import java.util.Set;
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.InvalidTransactionException;
-import javax.transaction.NotSupportedException;
-import javax.transaction.RollbackException;
import javax.transaction.Synchronization;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import javax.transaction.xa.XAResource;
import javax.webbeans.Observer;
import org.jboss.webbeans.event.DeferredEventNotification;
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-12-02 07:19:21 UTC (rev 387)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-12-02 08:16:38 UTC (rev 388)
@@ -59,93 +59,93 @@
manager.setEnabledDeploymentTypes(Standard.class, AnotherDeploymentType.class);
}
- /**
- * Tests the {@link Event#fire(Object, Annotation...)} method with a locally
- * instantiated implementation.
- */
- @SuppressWarnings("unchecked")
- @Test(groups = "event")
- @SpecAssertion(section = "7.6")
- public void testFireEvent()
- {
- DangerCall anEvent = new DangerCall();
- // Create a test annotation for the event and use it to construct the
- // event object
- Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
- EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
- eventComponent.setEventBindings(annotations);
- eventComponent.setManager(manager);
- eventComponent.fire(anEvent, new SynchronousAnnotationLiteral());
- assert anEvent.equals(manager.getEvent());
- assert Reflections.annotationSetMatches(manager.getEventBindings(),
- Tame.class, Synchronous.class);
+// /**
+// * Tests the {@link Event#fire(Object, Annotation...)} method with a locally
+// * instantiated implementation.
+// */
+// @SuppressWarnings("unchecked")
+// @Test(groups = "event")
+// @SpecAssertion(section = "7.6")
+// public void testFireEvent()
+// {
+// DangerCall anEvent = new DangerCall();
+// // Create a test annotation for the event and use it to construct the
+// // event object
+// Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
+// EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
+// eventComponent.setEventBindings(annotations);
+// eventComponent.setManager(manager);
+// eventComponent.fire(anEvent, new SynchronousAnnotationLiteral());
+// assert anEvent.equals(manager.getEvent());
+// assert Reflections.annotationSetMatches(manager.getEventBindings(),
+// Tame.class, Synchronous.class);
+//
+// // Test duplicate annotations on the fire method call
+// boolean duplicateDetected = false;
+// try
+// {
+// eventComponent.fire(anEvent, new TameAnnotationLiteral(),
+// new TameAnnotationLiteral());
+// } catch (DuplicateBindingTypeException e)
+// {
+// duplicateDetected = true;
+// }
+// assert duplicateDetected;
+//
+// // Test annotations that are not binding types
+// boolean nonBindingTypeDetected = false;
+// try
+// {
+// eventComponent.fire(anEvent, new FishStereotypeAnnotationLiteral());
+// } catch (IllegalArgumentException e)
+// {
+// nonBindingTypeDetected = true;
+// }
+// assert nonBindingTypeDetected;
+// }
- // Test duplicate annotations on the fire method call
- boolean duplicateDetected = false;
- try
- {
- eventComponent.fire(anEvent, new TameAnnotationLiteral(),
- new TameAnnotationLiteral());
- } catch (DuplicateBindingTypeException e)
- {
- duplicateDetected = true;
- }
- assert duplicateDetected;
+// /**
+// * Tests the {@link Event#observe(javax.webbeans.Observer, Annotation...)}
+// * method with a locally instantiated implementation.
+// */
+// @Test(groups = {"observerMethod"})
+// @SpecAssertion(section = "7.6")
+// public void testObserve()
+// {
+// // Create a test annotation for the event and use it to construct the
+// // event object
+// Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
+// EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
+// eventComponent.setEventType(DangerCall.class);
+// eventComponent.setEventBindings(annotations);
+// eventComponent.setManager(manager);
+// Observer<DangerCall> observer = new AnObserver<DangerCall>();
+// eventComponent.observe(observer, new SynchronousAnnotationLiteral());
+// assert manager.getEventType().equals(DangerCall.class);
+//
+// // Try duplicate annotation bindings
+// boolean duplicateDetected = false;
+// try
+// {
+// eventComponent.observe(observer,
+// new TameAnnotationLiteral());
+// } catch (DuplicateBindingTypeException e)
+// {
+// duplicateDetected = true;
+// }
+// assert duplicateDetected;
+//
+// // Try an invalid binding type
+// boolean nonBindingTypeDetected = false;
+// try
+// {
+// eventComponent.observe(observer,
+// new RiverFishStereotypeAnnotationLiteral());
+// } catch (IllegalArgumentException e)
+// {
+// nonBindingTypeDetected = true;
+// }
+// assert nonBindingTypeDetected;
+// }
- // Test annotations that are not binding types
- boolean nonBindingTypeDetected = false;
- try
- {
- eventComponent.fire(anEvent, new FishStereotypeAnnotationLiteral());
- } catch (IllegalArgumentException e)
- {
- nonBindingTypeDetected = true;
- }
- assert nonBindingTypeDetected;
- }
-
- /**
- * Tests the {@link Event#observe(javax.webbeans.Observer, Annotation...)}
- * method with a locally instantiated implementation.
- */
- @Test(groups = {"observerMethod"})
- @SpecAssertion(section = "7.6")
- public void testObserve()
- {
- // Create a test annotation for the event and use it to construct the
- // event object
- Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
- EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
- eventComponent.setEventType(DangerCall.class);
- eventComponent.setEventBindings(annotations);
- eventComponent.setManager(manager);
- Observer<DangerCall> observer = new AnObserver<DangerCall>();
- eventComponent.observe(observer, new SynchronousAnnotationLiteral());
- assert manager.getEventType().equals(DangerCall.class);
-
- // Try duplicate annotation bindings
- boolean duplicateDetected = false;
- try
- {
- eventComponent.observe(observer,
- new TameAnnotationLiteral());
- } catch (DuplicateBindingTypeException e)
- {
- duplicateDetected = true;
- }
- assert duplicateDetected;
-
- // Try an invalid binding type
- boolean nonBindingTypeDetected = false;
- try
- {
- eventComponent.observe(observer,
- new RiverFishStereotypeAnnotationLiteral());
- } catch (IllegalArgumentException e)
- {
- nonBindingTypeDetected = true;
- }
- assert nonBindingTypeDetected;
- }
-
}
16 years
[webbeans-commits] Webbeans SVN: r387 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: bootstrap and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-02 02:19:21 -0500 (Tue, 02 Dec 2008)
New Revision: 387
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java
Log:
hooking event beans into bootstrap process, changes in constructors
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java 2008-12-01 22:12:52 UTC (rev 386)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java 2008-12-02 07:19:21 UTC (rev 387)
@@ -100,7 +100,8 @@
/**
* Returns the producer methods
*
- * @return A set of producer methods
+ * @return A set of producer methods. An empty set is returned if there are
+ * none present
*/
public Set<AnnotatedMethod<Object>> getProducerMethods()
{
@@ -108,6 +109,17 @@
}
/**
+ * Returns @Observer annotated fields.
+ *
+ * @return A set of observing fields. An empty set is returned if there are
+ * none present.
+ */
+ public Set<AnnotatedField<Object>> getEventFields()
+ {
+ return getAnnotatedItem().getAnnotatedFields(Observes.class);
+ }
+
+ /**
* Initializes the injection points
*/
@Override
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-01 22:12:52 UTC (rev 386)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java 2008-12-02 07:19:21 UTC (rev 387)
@@ -45,14 +45,32 @@
private String location;
private AnnotatedField<EventImpl<T>> annotatedItem;
- public EventBean(Field field, ManagerImpl manager, AbstractClassBean<?> declaringBean)
+ @SuppressWarnings("unchecked")
+ public EventBean(AnnotatedField<T> field, ManagerImpl manager)
{
super(manager);
- this.annotatedItem = new AnnotatedFieldImpl<EventImpl<T>>(field, declaringBean.getAnnotatedItem());
+ this.annotatedItem = (AnnotatedField<EventImpl<T>>) field;
init();
}
/**
+ * Initializes the bean
+ *
+ * Calls super method and validates the annotated item
+ */
+ protected void init() {
+ super.init();
+ checkAnnotatedItem();
+ }
+
+ /**
+ * Validates the annotated item
+ */
+ private void checkAnnotatedItem() {
+ // TODO: checks
+ }
+
+ /**
* Caches the constructor for this type of bean to avoid future reflections
* during use.
*/
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java 2008-12-01 22:12:52 UTC (rev 386)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java 2008-12-02 07:19:21 UTC (rev 387)
@@ -20,6 +20,7 @@
import static org.jboss.webbeans.util.BeanFactory.createEnterpriseBean;
import static org.jboss.webbeans.util.BeanFactory.createProducerMethodBean;
import static org.jboss.webbeans.util.BeanFactory.createSimpleBean;
+import static org.jboss.webbeans.util.BeanFactory.createEventBean;
import java.util.Arrays;
import java.util.HashSet;
@@ -28,8 +29,10 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.AbstractBean;
import org.jboss.webbeans.bean.AbstractClassBean;
+import org.jboss.webbeans.bean.EventBean;
import org.jboss.webbeans.bean.ProducerMethodBean;
import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
+import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
@@ -139,11 +142,18 @@
beans.add(producerMethodBean);
manager.getResolver().addInjectionPoints(producerMethodBean.getInjectionPoints());
}
+ for (AnnotatedField<Object> eventField : bean.getEventFields())
+ {
+ EventBean<?> eventBean = createEventBean(eventField.getType(), eventField, manager);
+ beans.add(eventBean);
+ manager.getResolver().addInjectionPoints(eventBean.getInjectionPoints());
+ }
log.info("Web Bean: " + bean);
}
return beans;
}
+
/**
* Starts the boot process.
*
16 years
[webbeans-commits] Webbeans SVN: r386 - in ri/trunk/webbeans-ri/src: test/java/org/jboss/webbeans/test and 1 other directories.
by webbeans-commits@lists.jboss.org
Author: dallen6
Date: 2008-12-01 17:12:52 -0500 (Mon, 01 Dec 2008)
New Revision: 386
Removed:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
Log:
Fixed implementations for add/removeObserver() methods and added the corresponding test implementations.
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-01 21:30:33 UTC (rev 385)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-01 22:12:52 UTC (rev 386)
@@ -378,9 +378,7 @@
@SuppressWarnings("unchecked")
public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
{
- // TODO Using the eventType TypeLiteral<T>, the Class<T> object must be
- // retrieved
- this.eventManager.addObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.addObserver(observer, (Class<T>) eventType.getType(), bindings);
return this;
}
@@ -592,9 +590,7 @@
@SuppressWarnings("unchecked")
public <T> Manager removeObserver(Observer<T> observer, TypeLiteral<T> eventType, Annotation... bindings)
{
- // TODO The Class<T> for the event type must be retrieved from the
- // TypeLiteral<T> instance
- this.eventManager.removeObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.removeObserver(observer, (Class<T>) eventType.getType(), bindings);
return this;
}
Deleted: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java 2008-12-01 21:30:33 UTC (rev 385)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBeanModelTest.java 2008-12-01 22:12:52 UTC (rev 386)
@@ -1,86 +0,0 @@
-package org.jboss.webbeans.test;
-
-import java.lang.reflect.Field;
-
-import javax.webbeans.Standard;
-
-import org.jboss.webbeans.event.EventImpl;
-import org.jboss.webbeans.test.annotations.AnotherDeploymentType;
-import org.jboss.webbeans.test.beans.DangerCall;
-import org.jboss.webbeans.test.mock.MockManagerImpl;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-/**
- * Unit tests for the bean model used only for the container supplied
- * Event bean.
- *
- * @author David Allen
- *
- */
-public class EventBeanModelTest
-{
- private MockManagerImpl manager = null;
- //private EventBeanModel<EventImpl<DangerCall>> eventBeanModel = null;
- EventImpl<DangerCall> eventModelField = null;
-
- @BeforeMethod
- public void before() throws Exception
- {
- manager = new MockManagerImpl();
- manager.setEnabledDeploymentTypes(Standard.class, AnotherDeploymentType.class);
- Field eventModelField = this.getClass().getDeclaredField("eventModelField");
- /*eventBeanModel = new EventBeanModel<EventImpl<DangerCall>>(
- new SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
- new SimpleAnnotatedField<EventImpl<DangerCall>>(eventModelField),
- manager);*/
-
- }
-
- /**
- * The name should always be null since this type of bean is not allowed to have a name.
- */
- @Test(groups = "event")
- public void testName()
- {
- //assert eventBeanModel.getName() == null;
- }
-
- /**
- * The scope type should always be @Dependent
- */
- @Test(groups = "event")
- public void testScopeType()
- {
- //assert eventBeanModel.getScopeType().equals(Dependent.class);
- }
-
- /**
- * The deployment type should always be @Standard
- */
- @Test(groups = "event")
- public void testDeploymentType()
- {
- //assert eventBeanModel.getDeploymentType().equals(Standard.class);
- }
-
- @Test(groups = "event")
- public void testApiTypes()
- {
- //Set<Class<?>> apis = eventBeanModel.getApiTypes();
- //assert apis.size() >= 1;
- //for (Class<?> api : apis)
- //{
- // api.equals(Event.class);
- //}
- }
-
- // TODO Fix this @Test(groups = "event")
- public void testConstructor()
- {
- /*BeanConstructor<EventImpl<DangerCall>, ?> constructor = eventBeanModel.getConstructor();
- assert constructor != null;
- Event<DangerCall> event = constructor.invoke(manager, null);
- assert event != null;*/
- }
-}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java 2008-12-01 21:30:33 UTC (rev 385)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/NewEventTest.java 2008-12-01 22:12:52 UTC (rev 386)
@@ -1,5 +1,12 @@
package org.jboss.webbeans.test;
+import java.lang.annotation.Annotation;
+import java.util.Set;
+
+import javax.webbeans.Observer;
+import javax.webbeans.TypeLiteral;
+
+import org.jboss.webbeans.test.bindings.RoleBinding;
import org.testng.annotations.Test;
/**
@@ -11,6 +18,19 @@
@SpecVersion("PDR")
public class NewEventTest extends AbstractTest
{
+ public static class AnEventType
+ {
+ }
+
+ public static class AnObserver implements Observer<AnEventType>
+ {
+
+ public void notify(AnEventType event)
+ {
+ }
+
+ }
+
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.1")
public void testEventTypeIncludesAllSuperclassesAndInterfacesOfEventObject()
@@ -34,7 +54,7 @@
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.1")
- public void testConsumerNotifiedWhenEventTypeAndAllBindingMathces()
+ public void testConsumerNotifiedWhenEventTypeAndAllBindingsMatch()
{
assert false;
}
@@ -46,18 +66,83 @@
assert false;
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testManagerAddObserver()
{
- assert false;
+ Observer<AnEventType> observer = new AnObserver();
+
+ // First test with the Class<T> of the event type
+ manager.addObserver(observer, AnEventType.class);
+ Set<Observer<AnEventType>> resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 1;
+ assert resolvedObservers.iterator().next() == observer;
+
+ // Now test with the TypeLiteral<T> of the event type
+ observer = new AnObserver();
+ manager.addObserver(observer, new TypeLiteral<AnEventType>(){});
+ resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 2;
+ boolean foundObserver = false;
+ for (Observer<AnEventType> obs : resolvedObservers)
+ {
+ if (obs == observer)
+ {
+ foundObserver = true;
+ break;
+ }
+ }
+ assert foundObserver;
+
+ // Try adding an observer with some binding types
+ observer = new AnObserver();
+ Annotation[] bindingTypes = new Annotation[] { new RoleBinding("Admin"), new RoleBinding("Manager") };
+ manager.addObserver(observer, AnEventType.class, bindingTypes);
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindingTypes);
+ assert !resolvedObservers.isEmpty();
+ assert resolvedObservers.size() == 3;
+ foundObserver = false;
+ for (Observer<AnEventType> obs : resolvedObservers)
+ {
+ if (obs == observer)
+ {
+ foundObserver = true;
+ break;
+ }
+ }
+ assert foundObserver;
}
- @Test(groups={"stub", "events"})
+ @Test(groups={"events"})
@SpecAssertion(section="7.3")
public void testManagerRemoveObserver()
{
- assert false;
+ Observer<AnEventType> observer = new AnObserver();
+
+ // First test with the Class<T> of the event type
+ manager.addObserver(observer, AnEventType.class);
+ manager.removeObserver(observer, AnEventType.class);
+ Set<Observer<AnEventType>> resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert resolvedObservers.isEmpty();
+
+ // Now test with the TypeLiteral<T> of the event type
+ observer = new AnObserver();
+ manager.addObserver(observer, new TypeLiteral<AnEventType>(){});
+ manager.removeObserver(observer, new TypeLiteral<AnEventType>(){});
+ resolvedObservers = manager.resolveObservers(new AnEventType());
+ assert resolvedObservers.isEmpty();
+
+ // Also test with binding types
+ Annotation[] bindings = new Annotation[] { new RoleBinding("Admin") };
+ manager.addObserver(observer, AnEventType.class, bindings);
+ manager.removeObserver(observer, AnEventType.class);
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindings);
+ assert !resolvedObservers.isEmpty();
+ manager.removeObserver(observer, AnEventType.class, new RoleBinding("Admin"));
+ resolvedObservers = manager.resolveObservers(new AnEventType(), bindings);
+ assert resolvedObservers.isEmpty();
}
@Test(groups={"stub", "events"})
@@ -196,7 +281,7 @@
@Test(groups={"stub", "events"})
@SpecAssertion(section="7.5.4")
- public void testObserverMethodRecievesInjectionsOnNonObservesParameters()
+ public void testObserverMethodReceivesInjectionsOnNonObservesParameters()
{
assert false;
}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java 2008-12-01 21:30:33 UTC (rev 385)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockManagerImpl.java 2008-12-01 22:12:52 UTC (rev 386)
@@ -17,7 +17,6 @@
private Object event = null;
private Class<? extends Object> eventType = null;
private Annotation[] eventBindings = null;
- private Observer<?> observer = null;
/* (non-Javadoc)
* @see org.jboss.webbeans.ManagerImpl#fireEvent(java.lang.Object, java.lang.annotation.Annotation[])
@@ -28,35 +27,9 @@
// Record the event
this.event = event;
this.eventBindings = bindings;
+ super.fireEvent(event, bindings);
}
- /* (non-Javadoc)
- * @see org.jboss.webbeans.ManagerImpl#addObserver(javax.webbeans.Observer, java.lang.Class, java.lang.annotation.Annotation[])
- */
- @Override
- public <T> Manager addObserver(Observer<T> observer, Class<T> eventType,
- Annotation... bindings)
- {
- this.observer = observer;
- this.eventType = eventType;
- this.eventBindings = bindings;
- return this;
- }
-
- /* (non-Javadoc)
- * @see org.jboss.webbeans.ManagerImpl#addObserver(javax.webbeans.Observer, javax.webbeans.TypeLiteral, java.lang.annotation.Annotation[])
- */
- @Override
- public <T> Manager addObserver(Observer<T> observer,
- TypeLiteral<T> eventType, Annotation... bindings)
- {
- this.observer = observer;
- // TODO Fix the event type based on the type literal being passed. Not clear how to get the actual T.
- this.eventType = null;
- this.eventBindings = bindings;
- return this;
- }
-
/**
* Retrieves the event which was last fired with this manager.
* @return the event
@@ -85,14 +58,6 @@
return eventType;
}
- /**
- * @return the observer
- */
- public final Observer<?> getObserver()
- {
- return observer;
- }
-
public void setEnabledDeploymentTypes(Class<? extends Annotation>... enabledDeploymentTypes)
{
initEnabledDeploymentTypes(enabledDeploymentTypes);
16 years
[webbeans-commits] Webbeans SVN: r385 - ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-01 16:30:33 -0500 (Mon, 01 Dec 2008)
New Revision: 385
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
Log:
minor javadocs/comments and changes to map usage
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-01 07:51:37 UTC (rev 384)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-01 21:30:33 UTC (rev 385)
@@ -22,18 +22,18 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
-import javax.annotation.Resource;
import javax.transaction.Status;
import javax.transaction.SystemException;
-import javax.transaction.TransactionManager;
+import javax.transaction.UserTransaction;
import javax.webbeans.Observer;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.transaction.TransactionListener;
+import org.jboss.webbeans.util.JNDI;
+import org.jboss.webbeans.util.Strings;
import com.google.common.collect.ForwardingMap;
@@ -45,29 +45,57 @@
*/
public class EventManager
{
+ /**
+ * An event type -> observer list map
+ */
private class RegisteredObserversMap extends ForwardingMap<Class<?>, List<EventObserver<?>>>
{
+ // The map delegate
private Map<Class<?>, List<EventObserver<?>>> delegate;
+ /**
+ * Constructor. Initializes the delegate
+ */
public RegisteredObserversMap()
{
delegate = new ConcurrentHashMap<Class<?>, List<EventObserver<?>>>();
}
+ /**
+ * Returns the delegate for the ForwardingMap
+ *
+ * @return The delegate
+ */
@Override
protected Map<Class<?>, List<EventObserver<?>>> delegate()
{
return delegate;
}
+ /**
+ * Gets the list of observers for a given event type
+ *
+ * @param eventType The event type
+ * @return The list of interested observers. An empty list is returned if
+ * there are no matches.
+ */
@Override
- public CopyOnWriteArrayList<EventObserver<?>> get(Object key)
+ public CopyOnWriteArrayList<EventObserver<?>> get(Object eventType)
{
- CopyOnWriteArrayList<EventObserver<?>> observers = (CopyOnWriteArrayList<EventObserver<?>>) super.get(key);
+ CopyOnWriteArrayList<EventObserver<?>> observers = (CopyOnWriteArrayList<EventObserver<?>>) super.get(eventType);
return observers != null ? observers : new CopyOnWriteArrayList<EventObserver<?>>();
}
+ /**
+ * Adds an observer for a given event type
+ *
+ * Implicitly creates a new list if there is none for the event type. Only adds the observer if
+ * it is not already present
+ *
+ * @param eventType The event type
+ * @param observer The observer to add
+ */
public void put(Class<?> eventType, EventObserver<?> observer)
{
List<EventObserver<?>> observers = super.get(eventType);
@@ -76,46 +104,56 @@
observers = new CopyOnWriteArrayList<EventObserver<?>>();
super.put(eventType, observers);
}
- observers.add(observer);
+ if (!observers.contains(observer))
+ {
+ observers.add(observer);
+ }
}
+ /**
+ * Gets a string representation of the map
+ *
+ * @return A string representation
+ */
+ @Override
+ public String toString()
+ {
+ return Strings.mapToString("RegisteredObserversMap (event type -> observers list): ", delegate);
+ }
+
}
+ // The map of registered observers for a give
private final RegisteredObserversMap registeredObservers;
+ // The Web Beans manager
private ManagerImpl manager;
- // TODO: can we do this?
- @Resource
- TransactionManager transactionManager;
+ // The current UserTransaction
+ UserTransaction userTransaction;
/**
- * Initializes a new instance of the EventManager. This includes looking up
- * the transaction manager which is needed to defer events till the end of a
- * transaction.
+ * Initializes a new instance of the EventManager.
+ *
+ * @param manager The Web Beans manager
*/
public EventManager(ManagerImpl manager)
{
registeredObservers = new RegisteredObserversMap();
this.manager = manager;
+ // TODO. Check where to *really* get this from
+ userTransaction = (UserTransaction) JNDI.lookup("java:/UserTransaction");
}
/**
* Adds an observer to the event bus so that it receives event notifications.
*
* @param observer The observer that should receive events
+ * @param eventType The event type the observer is interested in
+ * @param bindings The bindings the observer wants to filter on
*/
public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
- CopyOnWriteArrayList<EventObserver<?>> eventTypeObservers = registeredObservers.get(eventType);
- if (eventTypeObservers.isEmpty())
- {
- eventTypeObservers = new CopyOnWriteArrayList<EventObserver<?>>();
- registeredObservers.put(eventType, eventTypeObservers);
- }
EventObserver<T> eventObserver = new EventObserver<T>(observer, eventType, bindings);
- if (!eventTypeObservers.contains(eventObserver))
- {
- eventTypeObservers.add(eventObserver);
- }
+ registeredObservers.put(eventType, eventObserver);
}
/**
@@ -124,7 +162,8 @@
*
* @param event The event object
* @param bindings Optional event bindings
- * @return A set of Observers
+ * @return A set of Observers. An empty set is returned if there are no
+ * matches.
*/
@SuppressWarnings("unchecked")
public <T> Set<Observer<T>> getObservers(T event, Annotation... bindings)
@@ -140,12 +179,16 @@
return interestedObservers;
}
+ /**
+ * Checks if there is currently a transaction active
+ *
+ * @return True if there is one, false otherwise
+ */
private boolean isTransactionActive()
{
try
{
- // TODO: Check NPE conditions;
- return transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE;
+ return userTransaction.getStatus() == Status.STATUS_ACTIVE;
}
catch (SystemException e)
{
@@ -154,13 +197,12 @@
}
/**
- * Notifies each observer immediately of the event unless a transaction is
- * currently in progress, in which case a deferred event is created and
- * registered.
+ * Iterates over the interested observers. If the observers is transactional
+ * and there is a transaction currently in progress, the event is deferred.
+ * In other cases, the observer is notified immediately.
*
- * @param <T>
- * @param observers
- * @param event
+ * @param observers The interested observers
+ * @param event The event type
*/
public <T> void notifyObservers(Set<Observer<T>> observers, T event)
{
@@ -177,6 +219,15 @@
}
}
+ /**
+ * Defers an event with regard to current transaction phase
+ *
+ * Gets the transaction listener, creates a deferred event representation and
+ * registers the deferred event.
+ *
+ * @param event The event type
+ * @param observer The interested observer
+ */
private <T> void deferEvent(T event, Observer<T> observer)
{
TransactionListener transactionListener = manager.getInstanceByType(TransactionListener.class);
@@ -188,6 +239,8 @@
* Removes an observer from the event bus.
*
* @param observer The observer to remove
+ * @param eventType The event type of the observer to remove
+ * @param bindings The bindings of the observer to remove
*/
public <T> void removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
@@ -201,17 +254,7 @@
{
StringBuffer buffer = new StringBuffer();
buffer.append("Event manager\n");
- buffer.append("Registered observers: " + registeredObservers.size() + "\n");
- int i = 1;
- for (Entry<Class<?>, List<EventObserver<?>>> entry : registeredObservers.entrySet())
- {
- for (EventObserver<?> observer : entry.getValue())
- {
- buffer.append(i + " - " + entry.getKey().getName() + ": " + observer.toString() + "\n");
- }
- i++;
- }
- buffer.append("Transaction manager: " + transactionManager + "\n");
+ buffer.append(registeredObservers.toString());
return buffer.toString();
}
}
16 years
[webbeans-commits] Webbeans SVN: r384 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: introspector/jlr and 1 other directory.
by webbeans-commits@lists.jboss.org
Author: nickarls
Date: 2008-12-01 02:51:37 -0500 (Mon, 01 Dec 2008)
New Revision: 384
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
Log:
forwaringmap for event observers
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-01 07:21:12 UTC (rev 383)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-01 07:51:37 UTC (rev 384)
@@ -27,16 +27,16 @@
import java.util.concurrent.CopyOnWriteArrayList;
import javax.annotation.Resource;
-import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
-import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.webbeans.Observer;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.transaction.TransactionListener;
+import com.google.common.collect.ForwardingMap;
+
/**
* The event bus is where observers are registered and events are fired.
*
@@ -45,7 +45,43 @@
*/
public class EventManager
{
- private final Map<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> registeredObservers;
+ private class RegisteredObserversMap extends ForwardingMap<Class<?>, List<EventObserver<?>>>
+ {
+
+ private Map<Class<?>, List<EventObserver<?>>> delegate;
+
+ public RegisteredObserversMap()
+ {
+ delegate = new ConcurrentHashMap<Class<?>, List<EventObserver<?>>>();
+ }
+
+ @Override
+ protected Map<Class<?>, List<EventObserver<?>>> delegate()
+ {
+ return delegate;
+ }
+
+ @Override
+ public CopyOnWriteArrayList<EventObserver<?>> get(Object key)
+ {
+ CopyOnWriteArrayList<EventObserver<?>> observers = (CopyOnWriteArrayList<EventObserver<?>>) super.get(key);
+ return observers != null ? observers : new CopyOnWriteArrayList<EventObserver<?>>();
+ }
+
+ public void put(Class<?> eventType, EventObserver<?> observer)
+ {
+ List<EventObserver<?>> observers = super.get(eventType);
+ if (observers == null)
+ {
+ observers = new CopyOnWriteArrayList<EventObserver<?>>();
+ super.put(eventType, observers);
+ }
+ observers.add(observer);
+ }
+
+ }
+
+ private final RegisteredObserversMap registeredObservers;
private ManagerImpl manager;
// TODO: can we do this?
@Resource
@@ -58,7 +94,7 @@
*/
public EventManager(ManagerImpl manager)
{
- registeredObservers = new ConcurrentHashMap<Class<?>, CopyOnWriteArrayList<EventObserver<?>>>();
+ registeredObservers = new RegisteredObserversMap();
this.manager = manager;
}
@@ -70,7 +106,7 @@
public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
CopyOnWriteArrayList<EventObserver<?>> eventTypeObservers = registeredObservers.get(eventType);
- if (eventTypeObservers == null)
+ if (eventTypeObservers.isEmpty())
{
eventTypeObservers = new CopyOnWriteArrayList<EventObserver<?>>();
registeredObservers.put(eventType, eventTypeObservers);
@@ -167,7 +203,7 @@
buffer.append("Event manager\n");
buffer.append("Registered observers: " + registeredObservers.size() + "\n");
int i = 1;
- for (Entry<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> entry : registeredObservers.entrySet())
+ for (Entry<Class<?>, List<EventObserver<?>>> entry : registeredObservers.entrySet())
{
for (EventObserver<?> observer : entry.getValue())
{
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-01 07:21:12 UTC (rev 383)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/jlr/AnnotatedClassImpl.java 2008-12-01 07:51:37 UTC (rev 384)
@@ -537,7 +537,7 @@
*
* @param annotationType The annotation type to match
* @return A set of abstracted constructors with given annotation type. If
- * the constructors set is empty, initialize it first .Returns an
+ * the constructors set is empty, initialize it first. Returns an
* empty set if there are no matches.
*
* @see org.jboss.webbeans.introspector.AnnotatedClass#getAnnotatedConstructors(Class)
16 years, 1 month