The Wayback Machine - https://web.archive.org/web/20050526230023/http://dssg.cs.umb.edu:80/resources/attribute-oriented-programming.html

An Introduction to Attribute-Oriented Programming

Hiroshi Wada and Jun Suzuki
University of Massachusetts, Boston
Department of Computer Science

Attribute-oriented programming is a program-level marking technique. Programmers can mark program elements (e.g. classes and methods) to indicate that they maintain application-specific or domain-specific semantics. For example, some programmers may define a "logging" attribute and associate it with a method to indicate the method should implement a logging function, while other programmers may define a "web service" attribute and associate it with a class to indicate the class should be implemented as a web service. Attributes separate application's core logic (or business logic) from application-specific or domain-specific semantics (e.g. logging and web service functions). By hiding the implementation details of those semantics from program code, attributes increase the level of programming abstraction and reduce programming complexity, resulting in simpler and more readable programs. The program elements associated with attributes are transformed to more detailed programs by a supporting tool (e.g. pre-processor). For example, a pre-processor may insert a logging program into the methods associated with a "logging" attribute.

The notion of attribute-oriented programming is becoming well accepted in several languages and tools, such as Java 2 standard edition (J2SE) 5.0, C# and XDoclet. For example, J2SE 5.0 implements attributes as annotations, and the Enterprise Java Bean (EJB) 3.0 extensively uses annotations to make EJB programming easier. Here is an example code using an EJB 3.0 annotation.

@entity class Customer{
  String name;
}

The @entity annotation is associated with the class Customer. This annotation indicates that the class Customer will be implemented as an entity bean in EJB. The pre-processor EJB provides, called annotation processor, takes an annotated code as an input and transforms it into the final compilable code as an output. In this example, the annotation processor generates several interfaces and classes required to implement an entity bean (i.e. a remote interface, home interface and implementation class).

A transformation of annotated code is performed based on a certain transformation rule. The EJB specification predefines a set of annotations and transformation rules for them. The EJB annotation processor follows the transformation rules predefined in the EJB specification.

In addition to predefined annotations, J2SE 5.0 allows developers to define and use their own (i.e. user-defined) annotations. There are two types of user-defined annotations; marker annotations and member annotations. Here is an example marker annotation, named Logging.

public @interface Logging{ }

A marker annotation is defined with the keyword @interface.

public class Customer{
  @Logging public void setName(...){...}
}

In this example, the Logging annotation is associated with the method setName(), indicating that the method logs method invocations. Then, a developer who defines this Logging annotation specifies a transformation rule for the annotation, and creates a user-defined annotation processor that implements the transformation rule (J2SE 5.0 provides a set of classes ot help developers build user-defined annotation processors). The annotation processor may replace each annotated method with a method implementing a logging function .

A member annotation, the second type of user-defined annotations, is an annotation that has member variables.

public @interface Persistent{
  String connection();
  String tableName();
}

Here, the Persistent annotation has two member variables: connection and tableName.

@Persistent(
  connection = "jdbc:http://localhost/",
  tableName = "customer"
)
public class Customer{}

The Persistent annotation is associated with the class Customer, indicating that the instances of Customer will be stored in a database with a particular database connection and table name. Then, a developer who defines this annotation specifies a transformation rule for the annotation, and implements a user-defined annotation processor that takes annotated code and generates additional classes implementing a database access function3.


Distributed Software Systems Group
Department of Computer Science
University of Massachusetts, Boston