InfoQ

News

Aspects: An Easy Tool for Annotation Handling?

Posted by Srini Penchikala on Sep 08, 2008 07:30 PM

Community
Architecture,
Java
Topics
AOP
Tags
Annotations ,
AOP ,
Aspect Oriented Programming ,
Metadata

While many think of Aspects for cross-cutting concerns such as transaction management, caching, persistence, role based security etc, Ramnivas Laddad says another key value for them is as an enabler for Annotations for ordinary projects.

Annotations, added in Java SE 5.0 as Java Metadata facility (JSR 175), provide a way to add metadata to program elements. They are used to configure containers, describe persistence configuration, set security roles, and are defined by nearly every recent JSR standard. They also include a mechanism for adding custom annotations to the Java code, as well as providing a programmatic access to metadata annotation through reflection.

Aspect-oriented Programming (AOP) has been used to implement cross-cutting concerns ranging from simple a use case like logging to advanced use cases like application security and transaction management. Using Aspects as a way to implement annotation handlers is a different way to think of them than as the traditional architect's "cross cutting concerns" view. Ramnivas talked about leveraging Annotations with Aspects and AOP on using custom annotations to add cross-cutting behavior to Java applications. He discussed what metadata and AOP bring to each other. Metadata brings additional information to selecting join points where Pointcuts use annotations to capture join points. It also helps in creating loosely-coupled aspects in certain use cases. And AOP brings a systematic way to consume and supply annotations. Supplying metadata using AOP also gives the benefit of uncluttering code. Consuming metadata using AOP has several advantages over using Annotation Processing Tool (APT) option.

Ramnivas cautioned that the use of metadata to extend the Java language can be both powerful and dangerous. On the one hand, annotations allow us to add new features to the Java language without modifying the core language, thus making it an open language; in the best-case scenario, principled extensions could overcome some of the limitations of the host language. On the other hand, a nonstandard, ad hoc, or incoherent set of annotations could result in incomprehensible code.

One of the best practices of getting most out of AOP is to use to metadata to capture join points for cross-cutting concerns. Keep in mind that Annotations should describe what is true at the join point (condition) - not what should happen at those points (action). He also suggested that the developers use annotations that are already there (like @Entity, @Table, @WebService etc). Consider pointcut without metadata as the first choice and rely on programming elements themselves. Also, avoid implementation-specific annotations. It also heps to understand the cost and benefit of using metadata and AOP together. Metadata can be consumed in a variety of ways, and understanding these uses will help put the combination of AOP and metadata into perspective. Ramnivas covered some of the best practices as well as suggestions for developers on not going overboard with custom annotations in his AOP and metadata articles (Part 1 and Part 2).

The main design considerations in implementing custom annotations are what (metadata, behavior), when (compile time, run-time) and how (APT, Run-time Reflection, AOP) to apply the annotations in Java applications. John Heintz recently did a presentation on adding behavior to Java Annotations where he compared different design techniques to implement custom annotations in Java applications. John discussed byte code transformers, which include Aspects, as one of the three types of Annotation processing options.

  • Generators: This annotation processing option includes reading the source code and generating new source code or modifying existing source code. APT and XDoclet fall into this category.
  • Bytecode Transformers: These annotation handlers parse the class files with Annotations and emit modified classes and newly generated classes. They can also generate non-class artifacts like XML configuration files. Bytecode transformer examples include AspectJ, Spring, Hibernate, CGLib and BCEL.
  • Runtime reflection: This option uses Reflection API to programmatically inspect the objects at runtime. Runtime reflection examples are libraries like Java 5+ reflection and Commons Attributes. Testing frameworks JUnit and TestNG use runtime reflection for processing the Annotations.

InfoQ spoke with John about the role of Aspects in implementing custom Annotations. He said it's really just a balance of forces (engineering trade-offs) that should be taken into consideration when implementing custom annotations. He also mentioned that Aspect-based implementations for Annotations provide the most concise way to implement behavior for annotation and a centralized definition. But the developers must clearly document non-local semantics and the impact on the build or deployment tool-chain (compile or runtime weaving).


Aspect Oriented Annotations by Bill Burke Posted Sep 9, 2008 6:37 PM
Re: Aspect Oriented Annotations by Srini Penchikala Posted Sep 11, 2008 12:48 PM
annotations not meta-data by phloid domino Posted Sep 16, 2008 2:11 PM
  1. Back to top

    Aspect Oriented Annotations

    Sep 9, 2008 6:37 PM by Bill Burke

    I wrote an article in 2004 on Aspect Oriented Annotations and reached the some of same conclusions.

    Because of the fragile pointcut problem that is pervasive in most applications of AOP, I think constraining AOP to solely being an annotation enable might be a good thing.

    Bill Burke
    http://bill.burkecentral.com

  2. Back to top

    Re: Aspect Oriented Annotations

    Sep 11, 2008 12:48 PM by Srini Penchikala

    Hi Bill, My experience has been the same. I have been using Aspects and AOP in Java applications for various use cases for last 3 years and I prefer Aspects to implement the Annotations compared to other approaches (APT, Reflection etc). I think Aspects are relatively less intrusive and less complicated (from the implementation stand-point) design solution for Annotation handling. -Srini

  3. Back to top

    annotations not meta-data

    Sep 16, 2008 2:11 PM by phloid domino

    Annotations are not meta-data. Think about it. Annotations are embedded in the code, and the code must be re-compiled if changing an annotation.

    Annotations *are* meta-syntax.

    Both have their place, but being clear about the difference can avoid a lot of confusion.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.