leosutic 2003/08/10 08:05:24
Modified: attributes/api/src/test/org/apache/avalon/attributes/test
AttributeDemo.java Sample.java
attributes/api/src/java/org/apache/avalon/attributes
Attributes.java DefaultCachedRepository.java
Log:
Added support for inheriting attributes via interfaces. Added support for adding
attributes to constructors and fields.
Revision Changes Path
1.2 +7 -1
avalon-sandbox/attributes/api/src/test/org/apache/avalon/attributes/test/AttributeDemo.java
Index: AttributeDemo.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/api/src/test/org/apache/avalon/attributes/test/AttributeDemo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AttributeDemo.java 10 Aug 2003 13:46:08 -0000 1.1
+++ AttributeDemo.java 10 Aug 2003 15:05:24 -0000 1.2
@@ -12,7 +12,13 @@
System.out.println ("Sample has the following class attributes:\n" +
Attributes.getAttributes (sample));
System.out.println ("Getting attributes for the method
Sample.someMethod(int)...");
- System.out.println ("Sample.someMethod(int) has the following class
attributes:\n" + Attributes.getAttributes (sample.getMethod ("someMethod", new
Class[]{ Integer.TYPE })));
+ System.out.println ("Sample.someMethod(int) has the following
attributes:\n" + Attributes.getAttributes (sample.getMethod ("someMethod", new
Class[]{ Integer.TYPE })));
+
+ System.out.println ("Getting attributes for the field Sample.field...");
+ System.out.println ("Sample.field has the following attributes:\n" +
Attributes.getAttributes (sample.getField ("field")));
+
+ System.out.println ("Getting attributes for the constructor Sample()...");
+ System.out.println ("Sample() has the following attributes:\n" +
Attributes.getAttributes (sample.getConstructor (new Class[0])));
}
}
1.2 +2 -2
avalon-sandbox/attributes/api/src/test/org/apache/avalon/attributes/test/Sample.java
Index: Sample.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/api/src/test/org/apache/avalon/attributes/test/Sample.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Sample.java 10 Aug 2003 13:46:08 -0000 1.1
+++ Sample.java 10 Aug 2003 15:05:24 -0000 1.2
@@ -4,12 +4,12 @@
* @ThreadSafe ()
* @Dependency ( SampleService.class, "sample" )
*/
-public class Sample extends SuperSample {
+public class Sample extends SuperSample implements SampleIFJoin {
/**
* @ThreadSafe ()
*/
- public Object aaaa;
+ public Object field;
/**
1.2 +10 -1
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Attributes.java 10 Aug 2003 13:46:08 -0000 1.1
+++ Attributes.java 10 Aug 2003 15:05:24 -0000 1.2
@@ -14,8 +14,17 @@
protected synchronized static CachedRepository getCachedRepository (Class
clazz) throws Exception {
if (classRepositories.containsKey (clazz)) {
- return (CachedRepository) classRepositories.get (clazz);
+ CachedRepository cr = (CachedRepository) classRepositories.get (clazz);
+ if (cr == null) {
+ // Circular references.
+ throw new ClassCircularityError (clazz.getName ());
+ } else {
+ return cr;
+ }
} else {
+ // Indicates we're loading it.
+ classRepositories.put (clazz, null);
+
Class attributeRepo;
CachedRepository cached;
try {
1.2 +73 -20
avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/DefaultCachedRepository.java
Index: DefaultCachedRepository.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/api/src/java/org/apache/avalon/attributes/DefaultCachedRepository.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultCachedRepository.java 10 Aug 2003 13:46:08 -0000 1.1
+++ DefaultCachedRepository.java 10 Aug 2003 15:05:24 -0000 1.2
@@ -15,11 +15,10 @@
public DefaultCachedRepository (Class clazz, AttributeRepositoryClass repo)
throws Exception {
// ---- Fix up class attributes
this.classAttributes.addAll (repo.getClassAttributes ());
-
- Class c = clazz.getSuperclass ();
- while (c != null) {
- this.classAttributes.addAll (getInheritableAttributes
(Attributes.getAttributes (c)));
- c = c.getSuperclass ();
+ this.classAttributes.addAll (getInheritableClassAttributes
(clazz.getSuperclass ()));
+ Class[] ifs = clazz.getInterfaces ();
+ for (int i = 0; i < ifs.length; i++) {
+ this.classAttributes.addAll (getInheritableClassAttributes (ifs[i]));
}
// ---- Fix up method attributes
@@ -27,26 +26,32 @@
for (int i = 0; i < methods.length; i++) {
Method m = methods[i];
String key = Util.getSignature (m);
+
Set attributes = new HashSet ();
attributes.addAll ((Collection) repo.getMethodAttributes ().get (key));
-
- c = clazz.getSuperclass ();
- while (c != null) {
-
- try {
- // Get equivalent method in superclass
- Method m2 = c.getMethod (m.getName (), m.getParameterTypes ());
- if (m2.getDeclaringClass () == c) {
- attributes.addAll (getInheritableAttributes
(Attributes.getAttributes (m2)));
- }
- } catch (NoSuchMethodException nsme) {
- }
-
- c = c.getSuperclass ();
+ attributes.addAll (getInheritableMethodAttributes (clazz.getSuperclass
(), m.getName (), m.getParameterTypes ()));
+ for (int j = 0; j < ifs.length; j++) {
+ attributes.addAll (getInheritableMethodAttributes (ifs[j],
m.getName (), m.getParameterTypes ()));
}
this.methods.put (m, attributes);
}
+
+ // --- Just copy constructor attributes (they aren't inherited)
+ Constructor[] constructors = clazz.getDeclaredConstructors ();
+ for (int i = 0; i < constructors.length; i++) {
+ Constructor ctor = constructors[i];
+ String key = Util.getSignature (ctor);
+ this.constructors.put (ctor, repo.getConstructorAttributes ().get
(key));
+ }
+
+ // --- Just copy field attributes (they aren't inherited)
+ Field[] fields = clazz.getDeclaredFields ();
+ for (int i = 0; i < fields.length; i++) {
+ Field f = fields[i];
+ String key = f.getName ();
+ this.fields.put (f, repo.getFieldAttributes ().get (key));
+ }
}
private static Collection getInheritableAttributes (Collection attrs) throws
Exception {
@@ -62,6 +67,54 @@
return result;
}
+ private static Collection getInheritableClassAttributes (Class c) throws
Exception {
+ if (c == null) {
+ return new ArrayList (0);
+ }
+
+ HashSet result = new HashSet ();
+ result.addAll (getInheritableAttributes (Attributes.getAttributes (c)));
+
+ // Traverse the class hierarchy
+ result.addAll (getInheritableClassAttributes (c.getSuperclass ()));
+
+ // Traverse the interface hierarchy
+ Class[] ifs = c.getInterfaces ();
+ for (int i = 0; i < ifs.length; i++) {
+ result.addAll (getInheritableClassAttributes (ifs[i]));
+ }
+
+ return result;
+ }
+
+ private static Collection getInheritableMethodAttributes (Class c, String
methodName, Class[] methodParams) throws Exception {
+ if (c == null) {
+ return new ArrayList (0);
+ }
+
+ HashSet result = new HashSet ();
+
+ try {
+ // Get equivalent method in c
+ Method m = c.getMethod (methodName, methodParams);
+ if (m.getDeclaringClass () == c) {
+ result.addAll (getInheritableAttributes (Attributes.getAttributes
(m)));
+ }
+ } catch (NoSuchMethodException nsme) {
+ }
+
+ // Traverse the class hierarchy
+ result.addAll (getInheritableMethodAttributes (c.getSuperclass (),
methodName, methodParams));
+
+ // Traverse the interface hierarchy
+ Class[] ifs = c.getInterfaces ();
+ for (int i = 0; i < ifs.length; i++) {
+ result.addAll (getInheritableMethodAttributes (ifs[i], methodName,
methodParams));
+ }
+
+ return result;
+ }
+
public Collection getAttributes () throws Exception {
return classAttributes;
}
@@ -76,5 +129,5 @@
public Collection getAttributes (Constructor c) throws Exception {
return (Collection) constructors.get (c);
- }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]