leosutic 2003/08/19 17:17:03
Modified: attributes/unittest/src/cl2 TestClass.java
attributes/unittest/src/test/org/apache/avalon/attributes/test
Sample.java AttributesTestCase.java
attributes/api/src/java overview.html
attributes/api/src/java/org/apache/avalon/attributes
Attributes.java
attributes/compiler/src/java/org/apache/avalon/attributes/compiler
AttributeIndexer.java AttributeCompiler.java
Log:
1. Added support for inner classes.
2. Changed the name of the generated attribute repository class (no point
in calling it org_apache_avalon... if the project is going to move to
commons).
Revision Changes Path
1.2 +5 -0 avalon-sandbox/attributes/unittest/src/cl2/TestClass.java
Index: TestClass.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/attributes/unittest/src/cl2/TestClass.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestClass.java 14 Aug 2003 22:08:31 -0000 1.1
+++ TestClass.java 20 Aug 2003 00:17:03 -0000 1.2
@@ -4,4 +4,9 @@
*/
public class TestClass {
+ /**
+ * @TestAttribute ("2inner")
+ */
+ public static class Inner {}
+
}
1.2 +6 -0
avalon-sandbox/attributes/unittest/src/test/org/apache/avalon/attributes/test/Sample.java
Index: Sample.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/unittest/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 14 Aug 2003 22:09:08 -0000 1.1
+++ Sample.java 20 Aug 2003 00:17:03 -0000 1.2
@@ -29,4 +29,10 @@
public void methodWithNoAttributes () {
}
+
+ /**
+ * @@Dependency ( SampleService.class, "inner-sample" )
+ */
+ public static class InnerSample {
+ }
}
1.3 +14 -1
avalon-sandbox/attributes/unittest/src/test/org/apache/avalon/attributes/test/AttributesTestCase.java
Index: AttributesTestCase.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/unittest/src/test/org/apache/avalon/attributes/test/AttributesTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AttributesTestCase.java 15 Aug 2003 18:43:25 -0000 1.2
+++ AttributesTestCase.java 20 Aug 2003 00:17:03 -0000 1.3
@@ -6,6 +6,7 @@
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
+import java.util.Collection;
import org.apache.avalon.attributes.Attributes;
import org.apache.avalon.attributes.AttributeIndex;
import junit.framework.TestCase;
@@ -152,6 +153,18 @@
public void testAttributeIndex () throws Exception {
URLClassLoader cl2 = new URLClassLoader (new URL[]{new File
("unittest/target/cl2/cl2.jar").toURL ()}, getClass().getClassLoader ());
AttributeIndex index = Attributes.getAttributeIndex (cl2);
- assertEquals ("[TestClass]", index.getClassesWithAttribute
("TestAttribute").toString ());
+ Collection classes = index.getClassesWithAttribute ("TestAttribute");
+ System.out.println (classes);
+ assertEquals (2, classes.size ());
+ assertTrue (classes.contains ("TestClass"));
+ assertTrue (classes.contains ("TestClass.Inner"));
+ }
+
+ public void testInnerClasses () throws Exception {
+ Class c = Sample.InnerSample.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, "inner-sample" )));
}
}
1.4 +2 -6 avalon-sandbox/attributes/api/src/java/overview.html
Index: overview.html
===================================================================
RCS file: /home/cvs/avalon-sandbox/attributes/api/src/java/overview.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- overview.html 17 Aug 2003 15:49:20 -0000 1.3
+++ overview.html 20 Aug 2003 00:17:03 -0000 1.4
@@ -208,15 +208,11 @@
<p><code>@@AttributeName</code>, since this explicitely tells the compiler
that this is an attribute, and eliminates a lot of guesswork.</p>
- <h3>What are the requirements for an attrbute class?</h3>
+ <h3>What are the requirements for an attribute class?</h3>
<p>It must have a public constructor. That's all.</p>
-
- <h3>I tried adding attributes to an inner class and it didn't work.</h3>
-
- <p>That's not supported (yet).</p>
-
+
<h3>I tried adding attributes to an anonymous class and it didn't work.</h3>
1.9 +1 -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.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Attributes.java 15 Aug 2003 18:43:25 -0000 1.8
+++ Attributes.java 20 Aug 2003 00:17:03 -0000 1.9
@@ -55,7 +55,7 @@
Class attributeRepo;
CachedRepository cached;
try {
- attributeRepo = Class.forName (clazz.getName () +
"$__org_apache_avalon_Attributes", true, clazz.getClassLoader ());
+ attributeRepo = Class.forName (clazz.getName () +
"$__attributeRepository", true, clazz.getClassLoader ());
AttributeRepositoryClass repo = (AttributeRepositoryClass)
attributeRepo.newInstance ();
cached = new DefaultCachedRepository (clazz, repo);
1.4 +4 -3
avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeIndexer.java
Index: AttributeIndexer.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeIndexer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AttributeIndexer.java 15 Aug 2003 21:41:21 -0000 1.3
+++ AttributeIndexer.java 20 Aug 2003 00:17:03 -0000 1.4
@@ -73,7 +73,7 @@
return classPath;
}
- private static final String SUFFIX = "$__org_apache_avalon_Attributes";
+ private static final String SUFFIX = "$__attributeRepository";
private static final String CLASS_SUFFIX = SUFFIX + ".class";
private static final String SOURCE_SUFFIX = SUFFIX + ".java";
@@ -113,9 +113,10 @@
while (enum.hasMoreElements ()) {
JarEntry entry = (JarEntry) enum.nextElement ();
if (!entry.isDirectory ()) {
- String className = entry.getName ().replace ('/',
'.').replace ('\\', '.');
+ String className = entry.getName ();
if (className.endsWith (CLASS_SUFFIX)) {
- String baseClassName = className.substring (0,
className.length () - CLASS_SUFFIX.length ());
+ className = className.replace ('/', '.').replace ('\\',
'.');
+ String baseClassName = className.substring (0,
className.length () - CLASS_SUFFIX.length ()).replace ('$', '.');
className = className.substring (0, className.length ()
- 6);
Class repoClass = cl.loadClass (className);
AttributeRepositoryClass repo =
(AttributeRepositoryClass) repoClass.newInstance ();
1.6 +29 -9
avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeCompiler.java
Index: AttributeCompiler.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/attributes/compiler/src/java/org/apache/avalon/attributes/compiler/AttributeCompiler.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- AttributeCompiler.java 15 Aug 2003 21:41:44 -0000 1.5
+++ AttributeCompiler.java 20 Aug 2003 00:17:03 -0000 1.6
@@ -121,13 +121,37 @@
}
protected void generateClass (XClass xClass) throws Exception {
+ if (xClass.isAnonymous ()) {
+ log (xClass.getName () + " is anonymous - ignoring.");
+ }
+
if (!hasAttributes (xClass)) {
return;
}
- String name = xClass.getQualifiedName ();
- File sourceFile = getSourceFile (name);
- File destFile = new File (destDir, name.replace ('.', '/') +
"$__org_apache_avalon_Attributes.java");
+ String name = null;
+ File sourceFile = null;
+ File destFile = null;
+ String packageName = null;
+ String className = null;
+
+ packageName = xClass.getContainingPackage().getName ();
+
+ if (xClass.isInner ()) {
+ name = xClass.getQualifiedName ().substring (packageName.length ());
+ StringTokenizer tok = new StringTokenizer (name, ".");
+ String outermostClass = packageName + (packageName.length () > 0 ? "."
: "") + tok.nextToken ();
+ sourceFile = getSourceFile (outermostClass);
+
+ className = xClass.getName ().replace ('.', '$');
+ name = packageName + (packageName.length () > 0 ? "." : "") +
className;
+ } else {
+ name = xClass.getQualifiedName ();
+ sourceFile = getSourceFile (name);
+ className = xClass.getName ();
+ }
+
+ destFile = new File (destDir, name.replace ('.', '/') +
"$__attributeRepository.java");
if (destFile.exists () && destFile.lastModified () >=
sourceFile.lastModified ()) {
return;
@@ -135,8 +159,6 @@
numGenerated++;
- String packageName = xClass.getContainingPackage().getName ();
- String className = xClass.getName ();
destFile.getParentFile ().mkdirs ();
PrintWriter pw = new PrintWriter (new FileWriter (destFile));
@@ -147,7 +169,7 @@
copyImports (sourceFile, pw);
- pw.println ("public class " + className + "$__org_apache_avalon_Attributes
implements org.apache.avalon.attributes.AttributeRepositoryClass {");
+ pw.println ("public class " + className + "$__attributeRepository
implements org.apache.avalon.attributes.AttributeRepositoryClass {");
{
pw.println (" private static final java.util.Set classAttributes =
new java.util.HashSet ();");
pw.println (" private static final java.util.Map fieldAttributes =
new java.util.HashMap ();");
@@ -288,9 +310,7 @@
while (iter.hasNext ()) {
XClass xClass = (XClass) iter.next ();
- if (!xClass.isInner ()) {
- generateClass (xClass);
- }
+ generateClass (xClass);
}
} catch (Exception e) {
throw new BuildException (e.toString (), e);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]