Overview

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.

News

Please see the change log for summaries of code changes.

2006-08-03

2004-08-15

  • Attributes 2.1 released!

2004-07-12

  • Attributes promoted out of sandbox to Commons proper.

Download and Installation

Full Distribution

You can download the full Commons Attributes distribution here.

Ant Users

Download the following files and put them in your $ANT_HOME/lib directory:

Maven Users

Download the following files:

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.

A Quick Sample

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.

Features

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