Commons Attributes enables Java programmers to use C#/.Net-style attributes in their code. Please see the tutorial and reference for a thorough explanation of the features and how the project integrates into the development process.
Please see the change log for summaries of code changes.
2006-08-03
Attributes 2.2 is released (release notes)
2004-08-15
Attributes 2.1 released!
2004-07-12
Attributes promoted out of sandbox to Commons proper.
Download the following files and put them in your $ANT_HOME/lib
directory:
Client API: commons-attributes-api-2.2.jar
Ant task: commons-attributes-compiler-2.2.jar
qDox 1.5: qdox-1.5.jar
Download the following files:
Client API: commons-attributes-api-2.2.jar
Ant task: commons-attributes-compiler-2.2.jar
Maven plugin: commons-attributes-plugin-2.2.jar
Drop the -api
and -compiler
jars into your ${maven repository}/commons-attributes/jars/
directory, and the Maven -plugin
in your ${maven home}/plugins/
directory.
You can now use attributes in your Java code, provided that you declare a dependency on the client API:
<dependency> <groupId>commons-attributes</groupId> <artifactId>commons-attributes-api</artifactId> <version>2.2</version> </dependency>
The attribute compiler will act as a precompiler to the java:compile goal, so you do not need to do anything else.
Commons Attributes enables you to add attributes to your code:
/** * Make this attribute inheritable... * * @@Inheritable() */ public class MyAttribute { private final float value; public MyAttribute( float value ) { this.value = value; } public float getValue() { return value; } } /** * Add a MyAttribute with value 0.8. * * @@MyAttribute( 0.8 ) */ public class MyClass { public static void main( String[] args ) { System.out.println( "MyClass has the following attributes:" + Attributes.getAttributes( MyClass.class ) ); } }
As the example shows, the attributes are type-safe and provides for validation of values.
API | |
---|---|
Can add attributes to classes | Yes |
Can add attributes to nested classes | Yes |
Can add attributes to methods and constructors | Yes |
Can add attributes to return values of methods | Yes |
Can add attributes to method parameters | Yes |
Can add attributes to fields | Yes |
Type safe attributes | Yes |
Ability to restrict targets | Yes, add an appropriate @@Target() attribute. |
Attribute inheritance | Yes, add the @@Inheritable() attribue to any attribute you wish to be inheritable. |
Inheritance without access to superclass source code | Yes |
Named parameters in attribute declaration | Yes, corresponds to setter methods. A Sealable interface enables the instance to become read-only when all relevant setters have been called. |
Ability to quickly find all classes with a specific attribute | Yes, via attribute indexes. |
Object attributes don't have to be serializable | Yes |
Multiple attributes with same tag on an element | Yes, this is controlled by the attribute itself |
Tools | |
Ant task | Yes |
Maven plugin | Yes |
Javadoc Taglet | Yes (Note: Taglets only exist in Javadoc 1.4 and later) |
Incremental compilation | Yes |
Compile-time Validation | Yes |
Implementation | |
Attribute storage | Generated classes |
Runtime code size | 36kB |
Unit test coverage | Excellent |