leosutic 2003/09/29 14:17:08 Modified: attributes/api/src/java/org/apache/commons/attributes Attributes.java CachedRepository.java DefaultCachedRepository.java attributes/compiler/src/java/org/apache/commons/attributes/compiler AttributeCompiler.java attributes/unittest/src/test/org/apache/commons/attributes/test AttributesTestCase.java SuperSample.java Log: Added the ability to add attributes to the parameters of a constructor (a fairly obvious complement to the ability to add attributes to method parameters that was somehow forgotten). Revision Changes Path 1.5 +31 -0 jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/Attributes.java Index: Attributes.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/Attributes.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Attributes.java 20 Sep 2003 15:11:34 -0000 1.4 +++ Attributes.java 29 Sep 2003 21:17:08 -0000 1.5 @@ -153,6 +153,13 @@ } /** + * Gets all attributes for a parameter of a constructor. + */ + public static Collection getParameterAttributes (Constructor constructor, int parameter) { + return getCachedRepository (constructor.getDeclaringClass()).getParameterAttributes (constructor, parameter); + } + + /** * Gets all attributes for the return value of a method. */ public static Collection getReturnAttributes (Method method) { @@ -230,6 +237,14 @@ } /** + * Get all attributes of a given type from a method's parameter. For all objects o in the returned + * collection, <code>o.getClass() == attributeClass</code>. + */ + public static Collection getParameterAttributes (Constructor constructor, int parameter, Class attributeClass) { + return getAttributes (getParameterAttributes (constructor, parameter), attributeClass); + } + + /** * Get all attributes of a given type from a method's return value. For all objects o in the returned * collection, <code>o.getClass() == attributeClass</code>. */ @@ -292,6 +307,14 @@ public static boolean hasParameterAttributeType (Method method, int parameter, Class attributeClass) { return hasAttributeType (getParameterAttributes (method, parameter), attributeClass); } + + /** + * Tests if a constructor's parameter 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 hasParameterAttributeType (Constructor constructor, int parameter, Class attributeClass) { + return hasAttributeType (getParameterAttributes (constructor, parameter), attributeClass); + } /** * Tests if a method's return value has an attribute of a given type. That is, is there any attribute @@ -347,6 +370,14 @@ */ public static boolean hasParameterAttribute (Method method, int parameter, Object attribute) { return hasAttribute (getParameterAttributes (method, parameter), attribute); + } + + /** + * Tests if a constructor's parameter has an attribute. That is, is there any attribute + * <code>attr</code> such that <code>attr.equals(attribute)</code>? + */ + public static boolean hasParameterAttribute (Constructor constructor, int parameter, Object attribute) { + return hasAttribute (getParameterAttributes (constructor, parameter), attribute); } /** 1.5 +1 -0 jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/CachedRepository.java Index: CachedRepository.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/CachedRepository.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CachedRepository.java 20 Sep 2003 15:11:34 -0000 1.4 +++ CachedRepository.java 29 Sep 2003 21:17:08 -0000 1.5 @@ -73,6 +73,7 @@ public Collection getAttributes (Field f); public Collection getAttributes (Method m); public Collection getParameterAttributes (Method m, int parameter); + public Collection getParameterAttributes (Constructor c, int parameter); public Collection getReturnAttributes (Method m); public Collection getAttributes (Constructor c); } 1.5 +20 -2 jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/DefaultCachedRepository.java Index: DefaultCachedRepository.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/DefaultCachedRepository.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultCachedRepository.java 20 Sep 2003 15:11:34 -0000 1.4 +++ DefaultCachedRepository.java 29 Sep 2003 21:17:08 -0000 1.5 @@ -188,7 +188,17 @@ String key = Util.getSignature (ctor); if (repo.getConstructorAttributes ().containsKey (key)) { - this.constructors.put (ctor, Collections.unmodifiableCollection ((Collection) repo.getConstructorAttributes ().get (key))); + List attributeBundle = null; + attributeBundle = (List) repo.getConstructorAttributes ().get (key); + MethodAttributeBundle bundle = new MethodAttributeBundle (); + + bundle.setAttributes ((Collection) attributeBundle.get (0)); + // ---- Parameter attributes + int numParameters = ctor.getParameterTypes().length; + for (int k = 0; k < numParameters; k++) { + bundle.addParameterAttributes ((Collection) attributeBundle.get (k + 1)); + } + this.constructors.put (ctor, bundle); } } @@ -341,6 +351,14 @@ } } + public Collection getParameterAttributes (Constructor c, int parameter) { + if (constructors.containsKey (c)) { + return ((MethodAttributeBundle) constructors.get (c)).getParameterAttributes (parameter); + } else { + return EMPTY_COLLECTION; + } + } + public Collection getParameterAttributes (Method m, int parameter) { if (methods.containsKey (m)) { return ((MethodAttributeBundle) methods.get (m)).getParameterAttributes (parameter); @@ -359,7 +377,7 @@ public Collection getAttributes (Constructor c) { if (constructors.containsKey (c)) { - return (Collection) constructors.get (c); + return ((MethodAttributeBundle) constructors.get (c)).getAttributes (); } else { return EMPTY_COLLECTION; } 1.5 +15 -2 jakarta-commons-sandbox/attributes/compiler/src/java/org/apache/commons/attributes/compiler/AttributeCompiler.java Index: AttributeCompiler.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/compiler/src/java/org/apache/commons/attributes/compiler/AttributeCompiler.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- AttributeCompiler.java 20 Sep 2003 15:11:34 -0000 1.4 +++ AttributeCompiler.java 29 Sep 2003 21:17:08 -0000 1.5 @@ -367,6 +367,7 @@ pw.println (" private static void initConstructorAttributes () {"); pw.println (" java.util.Set attrs = null;"); + pw.println (" java.util.List bundle = null;"); for (Iterator iter = xClass.getConstructors ().iterator (); iter.hasNext ();) { XConstructor member = (XConstructor) iter.next (); if (member.getDoc ().getTags ().size () > 0) { @@ -376,10 +377,22 @@ sb.append (")"); String key = sb.toString (); + pw.println (" bundle = new java.util.ArrayList ();"); pw.println (" attrs = new java.util.HashSet ();"); - addExpressions (member.getDoc ().getTags (), pw, "attrs", sourceFile.getPath ()); - pw.println (" constructorAttributes.put (\"" + key + "\", attrs);"); + addExpressions (member.getDoc ().getTags (), null, pw, "attrs", sourceFile.getPath ()); + pw.println (" bundle.add (attrs);"); pw.println (" attrs = null;"); + + for (Iterator parameters = member.getParameters ().iterator (); parameters.hasNext ();) { + XParameter parameter = (XParameter) parameters.next (); + pw.println (" attrs = new java.util.HashSet ();"); + addExpressions (member.getDoc ().getTags (), parameter.getName (), pw, "attrs", sourceFile.getPath ()); + pw.println (" bundle.add (attrs);"); + pw.println (" attrs = null;"); + } + + pw.println (" constructorAttributes.put (\"" + key + "\", bundle);"); + pw.println (" bundle = null;"); pw.println (); } } 1.4 +5 -0 jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java Index: AttributesTestCase.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/AttributesTestCase.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- AttributesTestCase.java 20 Sep 2003 15:11:34 -0000 1.3 +++ AttributesTestCase.java 29 Sep 2003 21:17:08 -0000 1.4 @@ -75,6 +75,11 @@ assertEquals (1, Attributes.getAttributes (c, Dependency.class).size ()); assertTrue (Attributes.hasAttributeType (c, Dependency.class)); assertTrue (Attributes.hasAttribute (c, new Dependency ( SampleService.class, "sample-ctor2" ))); + + assertEquals (1, Attributes.getParameterAttributes (c, 1).size ()); + assertEquals (1, Attributes.getParameterAttributes (c, 1, ThreadSafe.class).size ()); + assertTrue (Attributes.hasParameterAttributeType (c, 1, ThreadSafe.class)); + assertTrue (Attributes.hasParameterAttribute (c, 1, new ThreadSafe ())); } public void testClassInheritance () throws Exception { 1.2 +1 -0 jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/SuperSample.java Index: SuperSample.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test/SuperSample.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SuperSample.java 20 Aug 2003 23:16:16 -0000 1.1 +++ SuperSample.java 29 Sep 2003 21:17:08 -0000 1.2 @@ -25,6 +25,7 @@ /** * @Dependency ( SampleService.class, "sample-ctor2" ) + * @@.array ThreadSafe() */ public SuperSample (String input, String[][] array) {
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]