org.apache.commons.attributes

Class RuntimeAttributeRepository

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 ());
         }
     }
 
Since:
2.1

Field Summary

private Set
classAttributes
Set of class attributes.
private Class
clazz
Class we are defining attributes for.
private Map
constructorAttributes
Set of ctor attributes.
private Map
fieldAttributes
Set of field attributes.
private Map
methodAttributes
Set of method attributes.
private boolean
sealed
Flag indicating whether this repository is modifiable.
private Object
syncLock
Synchronization lock for sealing the repository.

Constructor Summary

RuntimeAttributeRepository(Class clazz)
Create a new runtime repository.

Method Summary

void
addClassAttribute(Object attribute)
Adds a new attribute to the class itself.
void
addConstructorAttribute(Class[] parameters, Object attribute)
Adds an attribute to a constructor.
void
addConstructorAttribute(Constructor c, Object attribute)
Adds an attribute to a constructor.
void
addFieldAttribute(Field f, Object attribute)
Adds an attribute to a field.
void
addFieldAttribute(String name, Object attribute)
Adds an attribute to a field.
void
addMethodAttribute(Method m, Object attribute)
Adds an attribute to a method.
void
addMethodAttribute(String name, Class[] parameters, Object attribute)
Adds an attribute to a method.
void
addParameterAttribute(Class[] parameters, int parameterIndex, Object attribute)
Adds an attribute to a parameter of a constructor.
void
addParameterAttribute(Constructor c, int parameterIndex, Object attribute)
Adds an attribute to a parameter of a constructor.
void
addParameterAttribute(Method m, int parameterIndex, Object attribute)
Adds an attribute to a parameter of a method.
void
addParameterAttribute(String name, Class[] parameters, int parameterIndex, Object attribute)
Adds an attribute to a parameter of a method.
void
addReturnAttribute(Method m, Object attribute)
Adds an attribute to the return value of a method.
void
addReturnAttribute(String name, Class[] parameters, Object attribute)
Adds an attribute to the return value of a method.
private void
checkSealed()
Convenience function to check if the repository is sealed.
Set
getClassAttributes()
private List
getConstructorAttributeBundle(Constructor c)
Convenience method to get and initialize an entry in the constructor map.
Map
getConstructorAttributes()
Class
getDefinedClass()
Gets the class this repository defines attributes for.
Map
getFieldAttributes()
private List
getMethodAttributeBundle(Method m)
Convenience method to get and initialize an entry in the method map.
Map
getMethodAttributes()
private List
getMethodOrConstructorAttributeBundle(Map map, String signature, int numSlots)
Convenience method to get and initialize an enry in the method or constructor attribute map.
void
seal()
Seals this repository.

Field Details

classAttributes

private final Set classAttributes

clazz

private final Class clazz
Class we are defining attributes for.

constructorAttributes

private final Map constructorAttributes

fieldAttributes

private final Map fieldAttributes

methodAttributes

private final Map methodAttributes

sealed

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

syncLock

private final Object syncLock
Synchronization lock for sealing the repository. We use this instead of synchronizing on the instance (this), since the sync:ing was added in 2.2 and we don't want to break any code.
Since:
2.2

Constructor Details

RuntimeAttributeRepository

public RuntimeAttributeRepository(Class clazz)
Create a new runtime repository.
Since:
2.1

Method Details

addClassAttribute

public void addClassAttribute(Object attribute)
Adds a new attribute to the class itself.
Since:
2.1

addConstructorAttribute

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

addConstructorAttribute

public void addConstructorAttribute(Constructor c,
                                    Object attribute)
Adds an attribute to a constructor.
Since:
2.1

addFieldAttribute

public void addFieldAttribute(Field f,
                              Object attribute)
Adds an attribute to a field.
Since:
2.1

addFieldAttribute

public void addFieldAttribute(String name,
                              Object attribute)
            throws NoSuchFieldException,
                   SecurityException
Adds an attribute to a field.
Since:
2.1

addMethodAttribute

public void addMethodAttribute(Method m,
                               Object attribute)
Adds an attribute to a method.
Since:
2.1

addMethodAttribute

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

addParameterAttribute

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

addParameterAttribute

public void addParameterAttribute(Constructor c,
                                  int parameterIndex,
                                  Object attribute)
Adds an attribute to a parameter of a constructor.
Since:
2.1

addParameterAttribute

public void addParameterAttribute(Method m,
                                  int parameterIndex,
                                  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.
Since:
2.1

addParameterAttribute

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

addReturnAttribute

public void addReturnAttribute(Method m,
                               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.
Since:
2.1

addReturnAttribute

public void addReturnAttribute(String name,
                               Class[] parameters,
                               Object attribute)
            throws NoSuchMethodException,
                   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.
Since:
2.1

checkSealed

private void checkSealed()
            throws IllegalStateException
Convenience function to check if the repository is sealed.
Since:
2.1

getClassAttributes

public Set getClassAttributes()
Specified by:
getClassAttributes in interface AttributeRepositoryClass

getConstructorAttributeBundle

private List getConstructorAttributeBundle(Constructor c)
Convenience method to get and initialize an entry in the constructor map.
Since:
2.1

getConstructorAttributes

public Map getConstructorAttributes()
Specified by:
getConstructorAttributes in interface AttributeRepositoryClass

getDefinedClass

public Class getDefinedClass()
Gets the class this repository defines attributes for.
Since:
2.1

getFieldAttributes

public Map getFieldAttributes()
Specified by:
getFieldAttributes in interface AttributeRepositoryClass

getMethodAttributeBundle

private List getMethodAttributeBundle(Method m)
Convenience method to get and initialize an entry in the method map.
Since:
2.1

getMethodAttributes

public Map getMethodAttributes()
Specified by:
getMethodAttributes in interface AttributeRepositoryClass

getMethodOrConstructorAttributeBundle

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

seal

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