commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: jakarta-commons-sandbox/attributes/site/xdocs index.xml
Date Sun, 09 May 2004 13:57:10 GMT
leosutic    2004/05/09 06:57:10

  Modified:    attributes/api/src/java/org/apache/commons/attributes
                        Attributes.java Util.java
               attributes/site/xdocs index.xml
  Added:       attributes/api/src/java/org/apache/commons/attributes
                        InvalidAttributeTargetError.java Target.java
  Log:
  Added ability to specify attribute targets.
  
  Revision  Changes    Path
  1.12      +2 -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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Attributes.java	20 Mar 2004 00:47:21 -0000	1.11
  +++ Attributes.java	9 May 2004 13:57:10 -0000	1.12
  @@ -135,6 +135,7 @@
                       attributeRepo = Class.forName (clazz.getName () + "$__attributeRepository",
true, clazz.getClassLoader ());
                       AttributeRepositoryClass repo = (AttributeRepositoryClass) attributeRepo.newInstance
();
                       
  +                    Util.validateRepository (clazz, repo);
                       cached = new DefaultCachedRepository (clazz, repo);
                   } catch (ClassNotFoundException cnfe) {
                       cached = CachedRepository.EMPTY;
  @@ -550,6 +551,7 @@
           if (classRepositories.get (clazz) != null) {
               throw new IllegalStateException (clazz.getName ());
           }
  +        Util.validateRepository (clazz, repo);
           
           DefaultCachedRepository cached = new DefaultCachedRepository (clazz, repo);
           classRepositories.put (clazz, cached);
  
  
  
  1.6       +71 -0     jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/Util.java
  
  Index: Util.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/Util.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Util.java	19 Feb 2004 14:49:13 -0000	1.5
  +++ Util.java	9 May 2004 13:57:10 -0000	1.6
  @@ -19,6 +19,11 @@
   import java.lang.reflect.Constructor;
   import java.lang.reflect.Method;
   
  +import java.util.List;
  +import java.util.Iterator;
  +import java.util.Set;
  +import java.util.Collection;
  +
   class Util {
       
       public static String getSignature (Method m) {
  @@ -66,5 +71,71 @@
               sb.append (decodedClassName (params[i].getName ()));                
           }
           return sb.toString ();
  +    }
  +    
  +    public static void checkTarget (int target, Object attribute, String element) {
  +        Target targetAttr = Attributes.getAttribute (attribute.getClass (), Target.class);
  +        if (targetAttr == null) {
  +            return;
  +        }
  +        
  +        if (targetAttr.getFlags () & target == 0) {
  +            throw new InvalidAttributeTargetError (attribute.getClass ().getName (), element,
targetAttr.getFlags ());
  +        }
  +    }
  +    
  +    public static void checkTarget (int target, Set attributes, String element) {
  +        Iterator iter = attributes.iterator ();
  +        while (iter.hasNext ()) {
  +            checkTarget (target, iter.next (), element);
  +        }
  +    }
  +    
  +    public static void validateRepository (Class owningClass, AttributeRepositoryClass
repo) {
  +        checkTarget (Target.CLASS, repo.getClassAttributes (), owningClass.getName ());
  +        
  +        Map fieldAttrs = repo.getFieldAttributes ();
  +        for (Iterator iter = fieldAttrs.keySet (); iter.hasNext ();) {
  +            String fieldName = (String) iter.next ();
  +            checkTarget (Target.FIELD, (Collection) fieldAttrs.get (fieldName), owningClass.getName
() + "." + fieldName);
  +        }
  +        
  +        Map ctorAttrs = repo.getConstructorAttributes ();
  +        for (Iterator iter = ctorAttrs.keySet (); iter.hasNext ();) {
  +            String ctorName = (String) iter.next ();
  +            List bundle = (List) ctorAttrs.get (ctorName);
  +            
  +            for (int i = 0; i < bundle.size (); i++) {
  +                switch (i) {
  +                case 0: 
  +                    checkTarget (Target.CONSTRUCTOR, (Collection) bundle.get (0), owningClass.getName
() + "." + ctorName);
  +                    break;
  +                    
  +                default:
  +                    checkTarget (Target.CONSTRUCTOR_PARAMETER, (Collection) bundle.get
(i), "parameter " + (i) + " of " + owningClass.getName () + ctorName);
  +                }
  +            }
  +        }
  +        
  +        Map methodAttrs = repo.getMethodAttributes ();
  +        for (Iterator iter = methodAttrs.keySet (); iter.hasNext ();) {
  +            String methodName = (String) iter.next ();
  +            List bundle = (List) methodAttrs.get (methodName);
  +            
  +            for (int i = 0; i < bundle.size (); i++) {
  +                switch (i) {
  +                case 0: 
  +                    checkTarget (Target.METHOD, (Collection) bundle.get (0), owningClass.getName
() + "." + methodName);
  +                    break;
  +                    
  +                case 1:
  +                    checkTarget (Target.RETURN, (Collection) bundle.get (1), "return value
of " + owningClass.getName () + "." + methodName);
  +                    break;
  +                    
  +                default:
  +                    checkTarget (Target.METHOD_PARAMETER, (Collection) bundle.get (i),
"parameter " + (i - 1) + " of " + owningClass.getName () + "." + methodName);
  +                }
  +            }
  +        }
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/InvalidAttributeTargetError.java
  
  Index: InvalidAttributeTargetError.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *     http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.attributes;
  
  /**
   * Thrown when one of the Attributes.getAttribute methods find more
   * than one instance of the specified attribute class.
   */
  public class InvalidAttributeTargetError extends Error {
      
      public MultipleAttributesError (String attributeClass, String element, int targetFlags)
{
          super ("Attributes of type " + attributeClass + " can't be applied to " + element
+ ". " + 
              "They can only be applied to: " + flagsToString (targetFlags));
      }
      
      private final static String flagsToString (int flags) {
          List targetNames = new ArrayList ();
          if (flags & Target.CLASS > 0) {
              targetNames.add ("CLASS");
          }
          if (flags & Target.FIELD > 0) {
              targetNames.add ("FIELD");
          }
          if (flags & Target.METHOD > 0) {
              targetNames.add ("METHOD");
          }
          if (flags & Target.CONSTRUCTOR > 0) {
              targetNames.add ("CONSTRUCTOR");
          }
          if (flags & Target.METHOD_PARAMETER > 0) {
              targetNames.add ("METHOD_PARAMETER");
          }
          if (flags & Target.CONSTRUCTOR_PARAMETER > 0) {
              targetNames.add ("CONSTRUCTOR_PARAMETER");
          }
          if (flags & Target.RETURN > 0) {
              targetNames.add ("RETURN");
          }
          
          StringBuffer sb = new StringBuffer ();
          for (int i = 0; i < targetNames.size (); i++) {
              sb.append (targetNames.get (i));
              if (i < targetNames.size ()) {
                  sb.append (" | ");
              }
          }
          return sb.toString ();
      }
      
  }
  
  
  1.1                  jakarta-commons-sandbox/attributes/api/src/java/org/apache/commons/attributes/Target.java
  
  Index: Target.java
  ===================================================================
  package org.apache.commons.attributes;
  
  /**
   * Attribute idicating what elements an attribute may be applied to.
   * This is checked at runtime. If the attribute is absent, it defaults
   * to Target.ALL.
   *
   * <pre><code>
   * / **
   *   * MyAttribute can only be applied to classes and fields, not methods.
   *   * @@Target(Target.CLASS | Target.FIELD)
   *   * /
   * public class MyAttribute { ... }
   * </code></pre>
   */
  public class Target {
      
      /**
       * Indicates that the attribute can be applied to a class or interface.
       */
      public static final int CLASS = 1;
      
      /**
       * Indicates that the attribute can be applied to a field.
       */
      public static final int FIELD = 2;
      
      /**
       * Indicates that the attribute can be applied to a method.
       */
      public static final int METHOD = 4;
      
      /**
       * Indicates that the attribute can be applied to a constructor.
       */
      public static final int CONSTRUCTOR = 8;
      
      /**
       * Indicates that the attribute can be applied to a method parameter.
       */
      public static final int METHOD_PARAMETER = 16;
      
      /**
       * Indicates that the attribute can be applied to a constructor parameter.
       */
      public static final int CONSTRUCTOR_PARAMETER = 32;
      
      /**
       * Indicates that the attribute can be applied to a method return value.
       */
      public static final int RETURN = 64;
      
      /**
       * Indicates that the attribute can be applied to a parameter of a method or a constructor.
       * It is equal to <code>METHOD_PARAMETER | CONSTRUCTOR_PARAMETER</code>.
       */
      public static final int PARAMETER = METHOD_PARAMETER | CONSTRUCTOR_PARAMETER;
      
      /**
       * Indicates that the attribute can be applied to any program element.
       */
      public static final int ALL = CLASS | FIELD | METHOD | CONSTRUCTOR | PARAMETER | RETURN;
      
      private final int flags;
      
      public Target (int flags) {
          this.flags = flags;
      }
      
      public int getFlags () {
          return flags;
      }
      
  }
  
  
  1.13      +4 -0      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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- index.xml	8 May 2004 20:37:22 -0000	1.12
  +++ index.xml	9 May 2004 13:57:10 -0000	1.13
  @@ -172,6 +172,10 @@
                       <td>Yes</td>
                   </tr>
                   <tr>
  +                    <td>Ability to restrict targets</td>
  +                    <td>Yes, add an appropriate @@Target() attribute.</td>
  +                </tr>
  +                <tr>
                       <td>Attribute inheritance</td>
                       <td>Yes, add the @@Inheritable() attribue to any attribute you
wish to be inheritable.</td>
                   </tr>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message