org.apache.commons.attributes
Class RuntimeAttributeRepository

java.lang.Object
  extended byorg.apache.commons.attributes.RuntimeAttributeRepository
All Implemented Interfaces:
AttributeRepositoryClass

public class RuntimeAttributeRepository
extends java.lang.Object
implements AttributeRepositoryClass

Class used to define attributes programmatically for a class. It is recommended that this class is used in the static initializer for a class:


 public class RuntimeSample extends SuperSample implements SampleIFJoin {

     static {
         try {
             RuntimeAttributeRepository rar = 
                 new RuntimeAttributeRepository (RuntimeSample.class);

             rar.addClassAttribute (new ThreadSafe ());
             
             rar.addFieldAttribute ("field", new ThreadSafe ());
             
             rar.addMethodAttribute ("someMethod", new Class[]{}, 
                 new Dependency ( SampleService.class, "sample-some-method1" ));
             
             rar.addParameterAttribute ("methodWithAttributes", 
                 new Class[]{ Integer.TYPE, Integer.TYPE }, 1, 
                 new ThreadSafe ());
 
             rar.addReturnAttribute ("methodWithAttributes", 
                 new Class[]{ Integer.TYPE, Integer.TYPE }, 
                 new Dependency ( SampleService.class, "sample-return" ));
             
             rar.addMethodAttribute ("someMethod", 
                 new Class[]{ Integer.TYPE },  
                 new Dependency ( SampleService.class, "sample-some-method2" ));
             
             Attributes.setAttributes (rar);
         } catch (Exception e) {
             throw new Error ("Unable to set attribute information: " + e.toString ());
         }
     }
 


Field Summary
private  java.util.Set classAttributes
          Set of class attributes.
private  java.lang.Class clazz
          Class we are defining attributes for.
private  java.util.Map constructorAttributes
          Set of ctor attributes.
private  java.util.Map fieldAttributes
          Set of field attributes.
private  java.util.Map methodAttributes
          Set of method attributes.
private  boolean sealed
          Flag indicating whether this repository is modifiable.
 
Constructor Summary
RuntimeAttributeRepository(java.lang.Class clazz)
          Create a new runtime repository.
 
Method Summary
 void addClassAttribute(java.lang.Object attribute)
          Adds a new attribute to the class itself.
 void addConstructorAttribute(java.lang.Class[] parameters, java.lang.Object attribute)
          Adds an attribute to a constructor.
 void addConstructorAttribute(java.lang.reflect.Constructor c, java.lang.Object attribute)
          Adds an attribute to a constructor.
 void addFieldAttribute(java.lang.reflect.Field f, java.lang.Object attribute)
          Adds an attribute to a field.
 void addFieldAttribute(java.lang.String name, java.lang.Object attribute)
          Adds an attribute to a field.
 void addMethodAttribute(java.lang.reflect.Method m, java.lang.Object attribute)
           
 void addMethodAttribute(java.lang.String name, java.lang.Class[] parameters, java.lang.Object attribute)
          Adds an attribute to a method.
 void addParameterAttribute(java.lang.Class[] parameters, int parameterIndex, java.lang.Object attribute)
          Adds an attribute to a parameter of a constructor.
 void addParameterAttribute(java.lang.reflect.Constructor c, int parameterIndex, java.lang.Object attribute)
          Adds an attribute to a parameter of a constructor.
 void addParameterAttribute(java.lang.reflect.Method m, int parameterIndex, java.lang.Object attribute)
          Adds an attribute to a parameter of a method.
 void addParameterAttribute(java.lang.String name, java.lang.Class[] parameters, int parameterIndex, java.lang.Object attribute)
          Adds an attribute to a parameter of a method.
 void addReturnAttribute(java.lang.reflect.Method m, java.lang.Object attribute)
          Adds an attribute to the return value of a method.
 void addReturnAttribute(java.lang.String name, java.lang.Class[] parameters, java.lang.Object attribute)
          Adds an attribute to the return value of a method.
private  void checkSealed()
          Convenience function to check if the repository is sealed.
 java.util.Set getClassAttributes()
          Returns a set containing all attributes (instances) associated with this class.
private  java.util.List getConstructorAttributeBundle(java.lang.reflect.Constructor c)
          Convenience method to get and initialize an entry in the constructor map.
 java.util.Map getConstructorAttributes()
          Returns a map with String keys and List values.
 java.lang.Class getDefinedClass()
          Gets the class this repository defines attributes for.
 java.util.Map getFieldAttributes()
          Returns a map with String keys and Set values.
private  java.util.List getMethodAttributeBundle(java.lang.reflect.Method m)
          Convenience method to get and initialize an entry in the method map.
 java.util.Map getMethodAttributes()
          Returns a map with String keys and List values.
private  java.util.List getMethodOrConstructorAttributeBundle(java.util.Map map, java.lang.String signature, int numSlots)
          Convenience method to get and initialize an enry in the method or constructor attribute map.
 void seal()
          Seals this repository.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sealed

private boolean sealed
Flag indicating whether this repository is modifiable. Once sealed, a repository can't be un-sealed.


classAttributes

private final java.util.Set classAttributes
Set of class attributes. See javadoc for AttributeRepositoryClass for structure.


fieldAttributes

private final java.util.Map fieldAttributes
Set of field attributes. See javadoc for AttributeRepositoryClass for structure.


constructorAttributes

private final java.util.Map constructorAttributes
Set of ctor attributes. See javadoc for AttributeRepositoryClass for structure.


methodAttributes

private final java.util.Map methodAttributes
Set of method attributes. See javadoc for AttributeRepositoryClass for structure.


clazz

private final java.lang.Class clazz
Class we are defining attributes for.

Constructor Detail

RuntimeAttributeRepository

public RuntimeAttributeRepository(java.lang.Class clazz)
Create a new runtime repository.

Method Detail

addClassAttribute

public void addClassAttribute(java.lang.Object attribute)
Adds a new attribute to the class itself.


checkSealed

private void checkSealed()
                  throws java.lang.IllegalStateException
Convenience function to check if the repository is sealed.

Throws:
java.lang.IllegalStateException - if sealed

getMethodOrConstructorAttributeBundle

private java.util.List getMethodOrConstructorAttributeBundle(java.util.Map map,
                                                             java.lang.String signature,
                                                             int numSlots)
Convenience method to get and initialize an enry in the method or constructor attribute map.


getMethodAttributeBundle

private java.util.List getMethodAttributeBundle(java.lang.reflect.Method m)
Convenience method to get and initialize an entry in the method map.

Returns:
a fully initialized List (as defined for the AttributeRepositoryClass interface) for the given method.

getConstructorAttributeBundle

private java.util.List getConstructorAttributeBundle(java.lang.reflect.Constructor c)
Convenience method to get and initialize an entry in the constructor map.

Returns:
a fully initialized List (as defined for the AttributeRepositoryClass interface) for the given constructor.

addFieldAttribute

public void addFieldAttribute(java.lang.String name,
                              java.lang.Object attribute)
                       throws java.lang.NoSuchFieldException,
                              java.lang.SecurityException
Adds an attribute to a field.

Throws:
java.lang.NoSuchFieldException
java.lang.SecurityException

addFieldAttribute

public void addFieldAttribute(java.lang.reflect.Field f,
                              java.lang.Object attribute)
Adds an attribute to a field.


addConstructorAttribute

public void addConstructorAttribute(java.lang.Class[] parameters,
                                    java.lang.Object attribute)
                             throws java.lang.NoSuchMethodException,
                                    java.lang.SecurityException
Adds an attribute to a constructor. The constructor is obtained via the getDeclaredConstrutor method of the class this repository defines.

Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException

addConstructorAttribute

public void addConstructorAttribute(java.lang.reflect.Constructor c,
                                    java.lang.Object attribute)
Adds an attribute to a constructor.


addMethodAttribute

public void addMethodAttribute(java.lang.String name,
                               java.lang.Class[] parameters,
                               java.lang.Object attribute)
                        throws java.lang.NoSuchMethodException,
                               java.lang.SecurityException
Adds an attribute to a method. The method is obtained via the getDeclaredMethod method of the class this repository defines.

Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException

addMethodAttribute

public void addMethodAttribute(java.lang.reflect.Method m,
                               java.lang.Object attribute)

addParameterAttribute

public void addParameterAttribute(java.lang.Class[] parameters,
                                  int parameterIndex,
                                  java.lang.Object attribute)
                           throws java.lang.NoSuchMethodException,
                                  java.lang.SecurityException
Adds an attribute to a parameter of a constructor. The constructor is obtained via the getDeclaredConstrutor method of the class this repository defines.

Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException

addParameterAttribute

public void addParameterAttribute(java.lang.reflect.Constructor c,
                                  int parameterIndex,
                                  java.lang.Object attribute)
Adds an attribute to a parameter of a constructor.


addParameterAttribute

public void addParameterAttribute(java.lang.String name,
                                  java.lang.Class[] parameters,
                                  int parameterIndex,
                                  java.lang.Object attribute)
                           throws java.lang.NoSuchMethodException,
                                  java.lang.SecurityException
Adds an attribute to a parameter of a method. The method is obtained via the getDeclaredMethod method of the class this repository defines.

Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException

addParameterAttribute

public void addParameterAttribute(java.lang.reflect.Method m,
                                  int parameterIndex,
                                  java.lang.Object attribute)
Adds an attribute to a parameter of a method. The method is obtained via the getDeclaredMethod method of the class this repository defines.


addReturnAttribute

public void addReturnAttribute(java.lang.String name,
                               java.lang.Class[] parameters,
                               java.lang.Object attribute)
                        throws java.lang.NoSuchMethodException,
                               java.lang.SecurityException
Adds an attribute to the return value of a method. The method is obtained via the getDeclaredMethod method of the class this repository defines.

Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException

addReturnAttribute

public void addReturnAttribute(java.lang.reflect.Method m,
                               java.lang.Object attribute)
Adds an attribute to the return value of a method. The method is obtained via the getDeclaredMethod method of the class this repository defines.


getDefinedClass

public java.lang.Class getDefinedClass()
Gets the class this repository defines attributes for.


getClassAttributes

public java.util.Set getClassAttributes()
Description copied from interface: AttributeRepositoryClass
Returns a set containing all attributes (instances) associated with this class. Should not return any attributes of superclasses etc.

Specified by:
getClassAttributes in interface AttributeRepositoryClass

getFieldAttributes

public java.util.Map getFieldAttributes()
Description copied from interface: AttributeRepositoryClass
Returns a map with String keys and Set values. The keys correspond to field names, and their associated Set values are the set of all attributes (instances) associated with that field. Should not return any attributes of superclasses etc.

Specified by:
getFieldAttributes in interface AttributeRepositoryClass

getMethodAttributes

public java.util.Map getMethodAttributes()
Description copied from interface: AttributeRepositoryClass
Returns a map with String keys and List values. The keys correspond to method signatures, given by get Util.getSignature method, and the lists are as follows:

list.get(0) = A Set with the attributes associated with the method.

list.get(1) = A Set with the attributes associated with the method's return value.

list.get(2) = A Set with the attributes associated with the method's first parameter.

list.get(n) = A Set with the attributes associated with the method's (n - 1) th parameter.

All slots in the list must be filled, not just those where there are attributes. Should not return any attributes of superclasses etc.

Specified by:
getMethodAttributes in interface AttributeRepositoryClass

getConstructorAttributes

public java.util.Map getConstructorAttributes()
Description copied from interface: AttributeRepositoryClass
Returns a map with String keys and List values. The keys correspond to constructor signatures, given by get Util.getSignature method, and the lists are as follows:

list.get(0) = A Set with the attributes associated with the constructor.

list.get(1) = A Set with the attributes associated with the constructor's first parameter.

list.get(n) = A Set with the attributes associated with the constructor's (n - 1) th parameter.

All slots in the list must be filled, not just those where there are attributes. Should not return any attributes of superclasses etc.

Specified by:
getConstructorAttributes in interface AttributeRepositoryClass

seal

public void seal()
Seals this repository. A sealed repository can't be modified.