You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by le...@apache.org on 2003/08/24 19:48:00 UTC

cvs commit: jakarta-commons-sandbox/attributes/unittest/src/test/org/apache/commons/attributes/test AttributesTestCase.java

leosutic    2003/08/24 10:48:00

  Modified:    attributes/api/src/java/org/apache/commons/attributes
                        AttributeIndex.java Attributes.java
                        CachedRepository.java DefaultCachedRepository.java
               attributes/plugin plugin.jelly
               attributes/site/xdocs index.xml
               attributes/unittest maven.xml
               attributes/unittest/src/test/org/apache/commons/attributes/test
                        AttributesTestCase.java
  Added:       attributes/api/src/java/org/apache/commons/attributes
                        AttributeUtil.java
  Log:
  Some refactoring intended to get a cleaner API.
  
  Revision  Changes    Path
  1.3       +4 -1      jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/AttributeIndex.java
  
  Index: AttributeIndex.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/AttributeIndex.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AttributeIndex.java	22 Aug 2003 23:48:15 -0000	1.2
  +++ AttributeIndex.java	24 Aug 2003 17:47:59 -0000	1.3
  @@ -78,7 +78,10 @@
       
       private final HashMap index = new HashMap ();
       
  -    AttributeIndex (ClassLoader cl) throws Exception {
  +    /**
  +     * Creates a new AttributeIndex for the given ClassLoader.
  +     */
  +    public AttributeIndex (ClassLoader cl) throws Exception {
           Enumeration enum = cl.getResources ("META-INF/attrs.index");
           while (enum.hasMoreElements ()) {
               URL url = (URL) enum.nextElement ();
  
  
  
  1.3       +33 -38    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Attributes.java	22 Aug 2003 23:48:15 -0000	1.2
  +++ Attributes.java	24 Aug 2003 17:47:59 -0000	1.3
  @@ -135,28 +135,28 @@
        * Gets all attributes for a class.
        */
       public static Collection getAttributes (Class clazz) {
  -        return Collections.unmodifiableCollection (getCachedRepository (clazz).getAttributes ());
  +        return getCachedRepository (clazz).getAttributes ();
       }
       
       /**
        * Gets all attributes for a method.
        */
       public static Collection getAttributes (Method method) {
  -        return Collections.unmodifiableCollection (getCachedRepository (method.getDeclaringClass()).getAttributes (method));
  +        return getCachedRepository (method.getDeclaringClass()).getAttributes (method);
       }
       
       /**
        * Gets all attributes for a field.
        */
       public static Collection getAttributes (Field field) {
  -        return Collections.unmodifiableCollection (getCachedRepository (field.getDeclaringClass()).getAttributes (field));
  +        return getCachedRepository (field.getDeclaringClass()).getAttributes (field);
       }
       
       /**
        * Gets all attributes for a constructor.
        */
       public static Collection getAttributes (Constructor cons) {
  -        return Collections.unmodifiableCollection (getCachedRepository (cons.getDeclaringClass()).getAttributes (cons));
  +        return getCachedRepository (cons.getDeclaringClass()).getAttributes (cons);
       }
       
       /**
  @@ -208,40 +208,6 @@
       }
       
       /**
  -     * Filters a collection of <code>Class</code> objects. The returned collection
  -     * only contains those classes that have an attribute of the specified type.
  -     */
  -    public static Collection getClassesWithAttributeType (Collection classes, Class attributeClass) {
  -        ArrayList result = new ArrayList ();
  -        Iterator iter = classes.iterator ();
  -        while (iter.hasNext ()) {
  -            Class clazz = (Class) iter.next ();
  -            if (hasAttributeType (clazz, attributeClass)) {
  -                result.add (clazz);
  -            }
  -        }
  -        
  -        return result;
  -    }
  -    
  -    /**
  -     * Filters a collection objects. The returned collection
  -     * only contains those objects that have an attribute of the specified type.
  -     */
  -    public static Collection getObjectsWithAttributeType (Collection objects, Class attributeClass) {
  -        ArrayList result = new ArrayList ();
  -        Iterator iter = objects.iterator ();
  -        while (iter.hasNext ()) {
  -            Class clazz = (Class) iter.next ().getClass ();
  -            if (hasAttributeType (clazz, attributeClass)) {
  -                result.add (clazz);
  -            }
  -        }
  -        
  -        return result;
  -    }
  -    
  -    /**
        * Convenience function to test whether a collection of attributes contain
        * an attribute of a given class.
        */
  @@ -333,8 +299,37 @@
        * Constructs an <code>AttributeIndex</code> for the given <code>ClassLoader</code>.
        * An AttributeIndex allows oyu to quickly find all classes that have a given
        * attribute.
  +     *
  +     * @deprecated This doesn't really belong here, and since I can move it into
  +     *             a separate class, I will. This class should concentrate on retrieving
  +     *             attributes from various elements, and not get into things like
  +     *             indexes and so on. Use <code>new AttributeIndex (cl)</code> instead. 
  +     *             <b>This method will be removed before 2.0.</b>
        */
       public static AttributeIndex getAttributeIndex (ClassLoader cl) throws Exception {
           return new AttributeIndex (cl);
  +    }
  +    
  +    
  +    /**
  +     * Filters a collection of <code>Class</code> objects. The returned collection
  +     * only contains those classes that have an attribute of the specified type.
  +     *
  +     * @deprecated moved to <code>AttributeUtil</code>. <b>This method will be removed
  +     *             before 2.0</b>
  +     */
  +    public static Collection getClassesWithAttributeType (Collection classes, Class attributeClass) {
  +        return AttributeUtil.getClassesWithAttributeType (classes, attributeClass);
  +    }
  +    
  +    /**
  +     * Filters a collection objects. The returned collection
  +     * only contains those objects that have an attribute of the specified type.
  +     *
  +     * @deprecated moved to <code>AttributeUtil</code>.
  +     *             <b>This method will be removed before 2.0.</b>
  +     */
  +    public static Collection getObjectsWithAttributeType (Collection objects, Class attributeClass) {
  +        return AttributeUtil.getObjectsWithAttributeType (objects, attributeClass);
       }
   }
  
  
  
  1.3       +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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CachedRepository.java	22 Aug 2003 23:48:15 -0000	1.2
  +++ CachedRepository.java	24 Aug 2003 17:47:59 -0000	1.3
  @@ -63,6 +63,7 @@
   
   /**
    * An attribute repository cache. Used internally to speed up operation.
  + * All collections returned should be unmodifiable.
    */
   interface CachedRepository {
       
  
  
  
  1.3       +14 -9     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultCachedRepository.java	22 Aug 2003 23:48:15 -0000	1.2
  +++ DefaultCachedRepository.java	24 Aug 2003 17:47:59 -0000	1.3
  @@ -61,6 +61,7 @@
   import java.lang.reflect.Method;
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.Collections;
   import java.util.Iterator;
   import java.util.Map;
   import java.util.Set;
  @@ -71,19 +72,23 @@
       
       private final static Collection EMPTY_COLLECTION = new ArrayList (0);
       
  -    private final Set classAttributes = new HashSet ();
  +    private final Collection classAttributes;
       private final Map fields = new HashMap ();
       private final Map methods = new HashMap ();
       private final Map constructors = new HashMap ();
       
       public DefaultCachedRepository (Class clazz, AttributeRepositoryClass repo) {
  +        
           // ---- Fix up class attributes
  -        this.classAttributes.addAll (repo.getClassAttributes ());
  -        this.classAttributes.addAll (getInheritableClassAttributes (clazz.getSuperclass ()));
  +        Set tempClassAttributes = new HashSet ();
  +        
  +        tempClassAttributes.addAll (repo.getClassAttributes ());
  +        tempClassAttributes.addAll (getInheritableClassAttributes (clazz.getSuperclass ()));
           Class[] ifs = clazz.getInterfaces ();
           for (int i = 0; i < ifs.length; i++) {
  -            this.classAttributes.addAll (getInheritableClassAttributes (ifs[i]));
  -        }
  +            tempClassAttributes.addAll (getInheritableClassAttributes (ifs[i]));
  +        }        
  +        this.classAttributes = Collections.unmodifiableCollection (tempClassAttributes);
           
           // ---- Fix up method attributes
           Method[] methods = clazz.getDeclaredMethods ();
  @@ -102,7 +107,7 @@
               }
               
               if (attributes.size () > 0) {
  -                this.methods.put (m, attributes);
  +                this.methods.put (m, Collections.unmodifiableCollection (attributes));
               }
           }
           
  @@ -113,9 +118,9 @@
               String key = Util.getSignature (ctor);
               
               if (repo.getConstructorAttributes ().containsKey (key)) {
  -                this.constructors.put (ctor, repo.getConstructorAttributes ().get (key));
  +                this.constructors.put (ctor, Collections.unmodifiableCollection ((Collection) repo.getConstructorAttributes ().get (key)));
               }
  -        }
  +        }        
           
           // --- Just copy field attributes (they aren't inherited)
           Field[] fields = clazz.getDeclaredFields ();
  @@ -123,7 +128,7 @@
               Field f = fields[i];
               String key = f.getName ();
               if (repo.getFieldAttributes ().containsKey (key)) {
  -                this.fields.put (f, repo.getFieldAttributes ().get (key));
  +                this.fields.put (f, Collections.unmodifiableCollection ((Collection) repo.getFieldAttributes ().get (key)));
               }
           }
       }
  
  
  
  1.1                  jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/AttributeUtil.java
  
  Index: AttributeUtil.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.attributes;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.WeakHashMap;
  
  /**
   * Commonly used convenience functions.
   */
  public class AttributeUtil {
      
      /**
       * Filters a collection of <code>Class</code> objects. The returned collection
       * only contains those classes that have an attribute of the specified type.
       */
      public static Collection getClassesWithAttributeType (Collection classes, Class attributeClass) {
          ArrayList result = new ArrayList ();
          Iterator iter = classes.iterator ();
          while (iter.hasNext ()) {
              Class clazz = (Class) iter.next ();
              if (Attributes.hasAttributeType (clazz, attributeClass)) {
                  result.add (clazz);
              }
          }
          
          return result;
      }
      
      /**
       * Filters a collection objects. The returned collection
       * only contains those objects that have an attribute of the specified type.
       */
      public static Collection getObjectsWithAttributeType (Collection objects, Class attributeClass) {
          ArrayList result = new ArrayList ();
          Iterator iter = objects.iterator ();
          while (iter.hasNext ()) {
              Class clazz = (Class) iter.next ().getClass ();
              if (Attributes.hasAttributeType (clazz, attributeClass)) {
                  result.add (clazz);
              }
          }
          
          return result;
      }
  }
  
  
  1.3       +19 -0     jakarta-commons-sandbox/attributes/plugin/plugin.jelly
  
  Index: plugin.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/plugin/plugin.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- plugin.jelly	23 Aug 2003 21:07:11 -0000	1.2
  +++ plugin.jelly	24 Aug 2003 17:47:59 -0000	1.3
  @@ -61,4 +61,23 @@
               id="maven.test.compile.src.set"  
               refid="jakarta.commons.attributes.generated.dir.test"/>
       </goal>
  +
  +    <postGoal name="jar:jar">
  +
  +        <ant:echo message="JvZ says: ${pom.artifactId}-${pom.currentVersion}.jar"/>
  +        <ant:echo message="JvZ also says: ${maven.final.name}.jar"/>
  +
  +        <!--
  +        For the life of me I can't figure out how to get the
  +        filename of the snapshot jar that jar:jar has just created.
  +
  +        The maven.final.name variable is only set locally by the jar:snapshot
  +        goal, and thus all I see here is the projectname-version.jar name.
  +        -->
  +        <ant:attribute-indexer jarFile="${maven.build.dir}/${maven.final.name}.jar">
  +            <classpath>
  +                <path refid="maven.dependency.classpath"/>
  +            </classpath>
  +        </ant:attribute-indexer>
  +    </postGoal>
   </project>
  
  
  
  1.5       +1 -1      jakarta-commons-sandbox/attributes/site/xdocs/index.xml
  
  Index: index.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/site/xdocs/index.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- index.xml	23 Aug 2003 21:53:18 -0000	1.4
  +++ index.xml	24 Aug 2003 17:48:00 -0000	1.5
  @@ -155,7 +155,7 @@
                   </tr>
                   <tr>
                       <td>Runtime code size</td>
  -                    <td>14kB</td>
  +                    <td>16kB</td>
                   </tr>
                   <tr>
                       <td>Unit test coverage</td>
  
  
  
  1.4       +0 -23     jakarta-commons-sandbox/attributes/unittest/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/unittest/maven.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- maven.xml	23 Aug 2003 21:07:11 -0000	1.3
  +++ maven.xml	24 Aug 2003 17:48:00 -0000	1.4
  @@ -8,29 +8,6 @@
       <property name="pom.organization.identifier" value="ASF"/>
       <property name="pom.specificationVersion" value="1.0"/>
       
  -    <postGoal name="jar:jar">
  -        <ant:taskdef resource="org/apache/commons/attributes/anttasks.properties">
  -            <classpath>
  -                <path refid="maven.dependency.classpath"/>
  -            </classpath>
  -        </ant:taskdef>
  -        
  -        <ant:echo message="JVZ says: ${pom.artifactId}-${pom.currentVersion}.jar"/>
  -        
  -        <!--
  -        For the life of me I can't figure out how to get the
  -        filename of the snapshot jar that jar:jar has just created.
  -        
  -        The maven.final.name variable is only set locally by the jar:snapshot
  -        goal, and thus all I see here is the projectname-version.jar name.
  -        -->
  -        <ant:attribute-indexer jarFile="${maven.build.dir}/${maven.final.name}.jar">
  -            <classpath>
  -                <path refid="maven.dependency.classpath"/>
  -            </classpath>
  -        </ant:attribute-indexer>
  -    </postGoal>
  -    
       <goal name="wrapper">
           <taskdef resource="org/apache/commons/attributes/anttasks.properties">
               <classpath>
  
  
  
  1.2       +1 -1      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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AttributesTestCase.java	20 Aug 2003 23:13:55 -0000	1.1
  +++ AttributesTestCase.java	24 Aug 2003 17:48:00 -0000	1.2
  @@ -152,7 +152,7 @@
       
       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);
  +        AttributeIndex index = new AttributeIndex (cl2);
           Collection classes = index.getClassesWithAttribute ("TestAttribute");
           System.out.println (classes);
           assertEquals (2, classes.size ());