leosutic 2003/08/10 14:47:57
Modified: attributes/api/src/java/org/apache/avalon/attributes
Attributes.java
Added: attributes/api/src/test/org/apache/avalon/attributes/test
AttributesTestCase.java
Removed: attributes/api/src/test/org/apache/avalon/attributes/test
AttributeDemo.java
Log:
1. Moved all build stuff into Maven.
2. Wrote a proper test case.
3. Included support for attributes attached to fields and constructors.
4. Slight code cleanups.
Revision Changes Path
1.1
avalon-sandbox/attributes/api/src/test/org/apache/avalon/attributes/test/AttributesTestCase.java
Index: AttributesTestCase.java
===================================================================
package org.apache.avalon.attributes.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import org.apache.avalon.attributes.Attributes;
import junit.framework.TestCase;
public class AttributesTestCase extends TestCase {
public void testClassAttributes () throws Exception {
/**
* @Dependency ( SampleService.class, "super-sample" )
*/
Class c = SuperSample.class;
assertEquals (1, Attributes.getAttributes (c).size ());
assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (c, Dependency.class));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "super-sample" )));
}
public void testMethodAttributes () throws Exception {
/**
* @Dependency ( SampleService.class, "super-some-method-sample" )
* @ThreadSafe ()
*/
Method m = SuperSample.class.getMethod ("someMethod", new Class[]{
Integer.TYPE });
assertEquals (2, Attributes.getAttributes (m).size ());
assertEquals (1, Attributes.getAttributes (m, Dependency.class).size ());
assertEquals (1, Attributes.getAttributes (m, ThreadSafe.class).size ());
assertTrue (Attributes.hasAttributeType (m, Dependency.class));
assertTrue (Attributes.hasAttributeType (m, ThreadSafe.class));
assertTrue (Attributes.hasAttribute (m, new Dependency (
SampleService.class, "super-some-method-sample" )));
assertTrue (Attributes.hasAttribute (m, new ThreadSafe ()));
}
public void testFieldAttributes () throws Exception {
/**
* @ThreadSafe ()
* @Dependency ( SampleService.class, "super-field" )
*/
Field f = SuperSample.class.getField ("field");
assertEquals (2, Attributes.getAttributes (f).size ());
assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
assertTrue (Attributes.hasAttribute (f, new Dependency (
SampleService.class, "super-field" ) ));
assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
assertTrue (Attributes.hasAttributeType (f, Dependency.class));
}
public void testDefaultConstructorAttributes () throws Exception {
/**
* @Dependency ( SampleService.class, "sample-ctor1" )
*/
Constructor c = SuperSample.class.getDeclaredConstructor (new Class[0]);
assertEquals (1, Attributes.getAttributes (c).size ());
assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (c, Dependency.class));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "sample-ctor1" )));
}
public void testConstructorAttributes () throws Exception {
/**
* @Dependency ( SampleService.class, "sample-ctor2" )
*/
Constructor c = SuperSample.class.getDeclaredConstructor (new Class[]{
String.class, (new String[0][0]).getClass () } );
assertEquals (1, Attributes.getAttributes (c).size ());
assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (c, Dependency.class));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "sample-ctor2" )));
}
public void testClassInheritance () throws Exception {
Class c = Sample.class;
assertEquals (5, Attributes.getAttributes (c).size ());
assertEquals (4, Attributes.getAttributes (c, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (c, Dependency.class));
assertTrue (Attributes.hasAttributeType (c, ThreadSafe.class));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "sample" )));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "super-sample" )));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "sample-if-1-c" )));
assertTrue (Attributes.hasAttribute (c, new Dependency (
SampleService.class, "sample-if-2-c" )));
assertTrue (Attributes.hasAttribute (c, new ThreadSafe ()));
}
public void testMethodInheritance () throws Exception {
Method m = Sample.class.getMethod ("someMethod", new Class[]{ Integer.TYPE
});
assertEquals (4, Attributes.getAttributes (m).size ());
assertEquals (4, Attributes.getAttributes (m, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (m, Dependency.class));
assertTrue (Attributes.hasAttribute (m, new Dependency (
SampleService.class, "super-some-method-sample" )));
assertTrue (Attributes.hasAttribute (m, new Dependency (
SampleService.class, "sample-some-method2" ) ));
assertTrue (Attributes.hasAttribute (m, new Dependency (
SampleService.class, "sample-if-1" ) ));
assertTrue (Attributes.hasAttribute (m, new Dependency (
SampleService.class, "sample-if-2" ) ));
}
public void testFieldNonInheritance () throws Exception {
Field f = SuperSample.class.getField ("field");
assertEquals (2, Attributes.getAttributes (f).size ());
assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
assertTrue (Attributes.hasAttributeType (f, Dependency.class));
assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
assertTrue (Attributes.hasAttribute (f, new Dependency (
SampleService.class, "super-field" )));
f = Sample.class.getField ("field");
assertEquals (1, Attributes.getAttributes (f).size ());
assertEquals (1, Attributes.getAttributes (f, ThreadSafe.class).size ());
assertTrue (Attributes.hasAttributeType (f, ThreadSafe.class));
assertTrue (Attributes.hasAttribute (f, new ThreadSafe ()));
f = SuperSample.class.getField ("noAttributesInSubClass");
assertEquals (1, Attributes.getAttributes (f).size ());
assertEquals (1, Attributes.getAttributes (f, Dependency.class).size ());
assertTrue (Attributes.hasAttribute (f, new Dependency (
SampleService.class, "super-noattrs" )));
assertTrue (Attributes.hasAttributeType (f, Dependency.class));
f = Sample.class.getField ("noAttributesInSubClass");
assertEquals (0, Attributes.getAttributes (f).size ());
assertEquals (0, Attributes.getAttributes (f, Dependency.class).size ());
assertTrue (!Attributes.hasAttribute (f, new Dependency (
SampleService.class, "super-noattrs" )));
assertTrue (!Attributes.hasAttributeType (f, Dependency.class));
}
public void testNoAttributes () throws Exception {
Method m = Sample.class.getMethod ("methodWithNoAttributes", new Class[0]);
assertEquals (0, Attributes.getAttributes (m).size ());
}
}
1.3 +100 -36
avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/Attributes.java
Index: Attributes.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/Attributes.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Attributes.java 10 Aug 2003 15:05:24 -0000 1.2
+++ Attributes.java 10 Aug 2003 21:47:57 -0000 1.3
@@ -3,41 +3,57 @@
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
-import java.util.*;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
/**
* API for accessing attributes.
*/
public class Attributes {
- private final static HashMap classRepositories = new HashMap ();
+ private final static Map classRepositories = new HashMap ();
- protected synchronized static CachedRepository getCachedRepository (Class
clazz) throws Exception {
+ private static List initList = new ArrayList ();
+
+ private synchronized static CachedRepository getCachedRepository (Class clazz)
throws RepositoryError, CircularDependencyError {
if (classRepositories.containsKey (clazz)) {
CachedRepository cr = (CachedRepository) classRepositories.get (clazz);
if (cr == null) {
// Circular references.
- throw new ClassCircularityError (clazz.getName ());
+ List dependencyList = new ArrayList ();
+ dependencyList.addAll (initList);
+ throw new CircularDependencyError (clazz.getName (),
dependencyList);
} else {
return cr;
}
} else {
- // Indicates we're loading it.
+ // Indicate that we're loading it.
+ initList.add (clazz.getName ());
classRepositories.put (clazz, null);
Class attributeRepo;
CachedRepository cached;
try {
- attributeRepo = Class.forName (clazz.getName () + "$Attributes",
true, clazz.getClassLoader ());
+ attributeRepo = Class.forName (clazz.getName () +
"$__org_apache_avalon_Attributes", true, clazz.getClassLoader ());
AttributeRepositoryClass repo = (AttributeRepositoryClass)
attributeRepo.newInstance ();
cached = new DefaultCachedRepository (clazz, repo);
} catch (ClassNotFoundException cnfe) {
cached = CachedRepository.EMPTY;
+ } catch (InstantiationException ie) {
+ throw new RepositoryError (ie);
+ } catch (IllegalAccessException iae) {
+ throw new RepositoryError (iae);
}
classRepositories.put (clazz, cached); // Should be keyed on
ClassLoader as well.
+ initList.removeLast ();
+
return cached;
}
}
@@ -45,29 +61,29 @@
/**
* Gets all attributes for a class.
*/
- public static Collection getAttributes (Class clazz) throws Exception {
- return getCachedRepository (clazz).getAttributes ();
+ public static Collection getAttributes (Class clazz) {
+ return Collections.unmodifiableCollection (getCachedRepository
(clazz).getAttributes ());
}
/**
* Gets all attributes for a method.
*/
- public static Collection getAttributes (Method method) throws Exception {
- return getCachedRepository (method.getDeclaringClass()).getAttributes
(method);
+ public static Collection getAttributes (Method method) {
+ return Collections.unmodifiableCollection (getCachedRepository
(method.getDeclaringClass()).getAttributes (method));
}
/**
* Gets all attributes for a field.
*/
- public static Collection getAttributes (Field field) throws Exception {
- return getCachedRepository (field.getDeclaringClass()).getAttributes
(field);
+ public static Collection getAttributes (Field field) {
+ return Collections.unmodifiableCollection (getCachedRepository
(field.getDeclaringClass()).getAttributes (field));
}
/**
* Gets all attributes for a constructor.
*/
- public static Collection getAttributes (Constructor cons) throws Exception {
- return getCachedRepository (cons.getDeclaringClass()).getAttributes (cons);
+ public static Collection getAttributes (Constructor cons) {
+ return Collections.unmodifiableCollection (getCachedRepository
(cons.getDeclaringClass()).getAttributes (cons));
}
/**
@@ -83,34 +99,38 @@
}
}
- return result;
+ return Collections.unmodifiableCollection (result);
}
/**
- * Get all attributes of a given type from a class.
+ * Get all attributes of a given type from a class. For all objects o in the
returned
+ * collection, <code>o.getClass() == attributeClass</code>.
*/
- public static Collection getAttributes (Class clazz, Class attributeClass)
throws Exception {
+ public static Collection getAttributes (Class clazz, Class attributeClass) {
return getAttributes (getAttributes (clazz), attributeClass);
}
/**
- * Get all attributes of a given type from a field.
+ * Get all attributes of a given type from a field. For all objects o in the
returned
+ * collection, <code>o.getClass() == attributeClass</code>.
*/
- public static Collection getAttributes (Field field, Class attributeClass)
throws Exception {
+ public static Collection getAttributes (Field field, Class attributeClass) {
return getAttributes (getAttributes (field), attributeClass);
}
/**
- * Get all attributes of a given type from a constructor.
+ * Get all attributes of a given type from a constructor. For all objects o in
the returned
+ * collection, <code>o.getClass() == attributeClass</code>.
*/
- public static Collection getAttributes (Constructor ctor, Class attributeClass)
throws Exception {
+ public static Collection getAttributes (Constructor ctor, Class attributeClass)
{
return getAttributes (getAttributes (ctor), attributeClass);
}
/**
- * Get all attributes of a given type from a method.
+ * Get all attributes of a given type from a method. For all objects o in the
returned
+ * collection, <code>o.getClass() == attributeClass</code>.
*/
- public static Collection getAttributes (Method method, Class attributeClass)
throws Exception {
+ public static Collection getAttributes (Method method, Class attributeClass) {
return getAttributes (getAttributes (method), attributeClass);
}
@@ -118,7 +138,7 @@
* Convenience function to test whether a collection of attributes contain
* an attribute of a given class.
*/
- private static boolean hasAttribute (Collection attrs, Class attributeClass)
throws Exception {
+ private static boolean hasAttributeType (Collection attrs, Class
attributeClass) {
Iterator iter = attrs.iterator ();
while (iter.hasNext ()) {
Object attr = iter.next ();
@@ -131,31 +151,75 @@
}
/**
- * Tests if a class has an attribute of a given type.
+ * Tests if a class has an attribute of a given type. That is, is there any
attribute
+ * <code>attr</code> such that <code>attr.getClass() == attributeClass</code>?
*/
- public static boolean hasAttribute (Class clazz, Class attributeClass) throws
Exception {
- return hasAttribute (getAttributes (clazz), attributeClass);
+ public static boolean hasAttributeType (Class clazz, Class attributeClass) {
+ return hasAttributeType (getAttributes (clazz), attributeClass);
}
/**
- * Tests if a class has an attribute of a given type.
+ * Tests if a field has an attribute of a given type. That is, is there any
attribute
+ * <code>attr</code> such that <code>attr.getClass() == attributeClass</code>?
+ */
+ public static boolean hasAttributeType (Field field, Class attributeClass) {
+ return hasAttributeType (getAttributes (field), attributeClass);
+ }
+
+ /**
+ * Tests if a constructor has an attribute of a given type. That is, is there
any attribute
+ * <code>attr</code> such that <code>attr.getClass() == attributeClass</code>?
+ */
+ public static boolean hasAttributeType (Constructor ctor, Class attributeClass)
{
+ return hasAttributeType (getAttributes (ctor), attributeClass);
+ }
+
+ /**
+ * Tests if a method has an attribute of a given type. That is, is there any
attribute
+ * <code>attr</code> such that <code>attr.getClass() == attributeClass</code>?
+ */
+ public static boolean hasAttributeType (Method method, Class attributeClass) {
+ return hasAttributeType (getAttributes (method), attributeClass);
+ }
+
+ /**
+ * Convenience function to test whether a collection of attributes contain
+ * an attribute.
+ */
+ private static boolean hasAttribute (Collection attrs, Object attribute) {
+ return attrs.contains (attribute);
+ }
+
+ /**
+ * Tests if a class has an attribute. That is, is there any attribute
+ * <code>attr</code> such that <code>attr.equals(attribute)</code>?
+ */
+ public static boolean hasAttribute (Class clazz, Object attribute) {
+ return hasAttribute (getAttributes (clazz), attribute);
+ }
+
+ /**
+ * Tests if a field has an attribute. That is, is there any attribute
+ * <code>attr</code> such that <code>attr.equals(attribute)</code>?
*/
- public static boolean hasAttribute (Field field, Class attributeClass) throws
Exception {
- return hasAttribute (getAttributes (field), attributeClass);
+ public static boolean hasAttribute (Field field, Object attribute) {
+ return hasAttribute (getAttributes (field), attribute);
}
/**
- * Tests if a class has an attribute of a given type.
+ * Tests if a constructor has an attribute. That is, is there any attribute
+ * <code>attr</code> such that <code>attr.equals(attribute)</code>?
*/
- public static boolean hasAttribute (Constructor ctor, Class attributeClass)
throws Exception {
- return hasAttribute (getAttributes (ctor), attributeClass);
+ public static boolean hasAttribute (Constructor ctor, Object attribute) {
+ return hasAttribute (getAttributes (ctor), attribute);
}
/**
- * Tests if a class has an attribute of a given type.
+ * Tests if a method has an attribute. That is, is there any attribute
+ * <code>attr</code> such that <code>attr.equals(attribute)</code>?
*/
- public static boolean hasAttribute (Method method, Class attributeClass) throws
Exception {
- return hasAttribute (getAttributes (method), attributeClass);
+ public static boolean hasAttribute (Method method, Object attribute) {
+ return hasAttribute (getAttributes (method), attribute);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]