Featured in
Architecture & Design

Mini-talks: The Machine Intelligence Landscape: A Venture Capital Perspective by David Beyer. The future of global, trustless transactions on the largest graph: blockchain by Olaf Carlson-Wee. Algorithms for Anti-Money Laundering by Richard Minerich.

Featured in
Process & Practices

In-App Subscriptions Made Easy

There are various types of subscriptions: recurring, non-recurring, free-trial periods, various billing cycles and any possible billing variation one can imagine. But with lack of information online, you might discover that mobile subscriptions behave differently from what you expected. This article will make your life somewhat easier when addressing an in-app subscriptions implementation.

Featured in
Operations & Infrastructure

Mini-talks: The Machine Intelligence Landscape: A Venture Capital Perspective by David Beyer. The future of global, trustless transactions on the largest graph: blockchain by Olaf Carlson-Wee. Algorithms for Anti-Money Laundering by Richard Minerich.

Featured in
Enterprise Architecture

Mini-talks: The Machine Intelligence Landscape: A Venture Capital Perspective by David Beyer. The future of global, trustless transactions on the largest graph: blockchain by Olaf Carlson-Wee. Algorithms for Anti-Money Laundering by Richard Minerich.

Aspects: An Easy Tool for Annotation Handling?

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.

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).

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.