Skip to main content

Creating Eclipse Warnings with AspectJ

Posted by felipegaucho on March 9, 2006 at 3:48 AM PST

Debugging with println is an ancient habit in programming - even
the novice developers know about how fragile is such strategy facing the
robust debugging tools provided by the most common IDEs. Despite that,
everybody sometimes includes some commands like
System.out.println("step #1"), or

System.out.println("remember to remove this...")
. The code smell gets unveiled when a client observes his console printing
friendly words like this is just a test or even worst when a system
administrator opens a server log and finds hundreds of debug lines printed with
undeciphered numbers and letters. The most common strategy to avoid such programming
deviation is to use guard tools like Checkstyle and PMD. These tools are excellent
vigilants on the best practices of the code-style and patterns usage - but they seem weak
facing non-Java concerns like the disabling println policy.

On the
Cejug-Classifieds Project
, we have made some experiments with the creation
of an Eclipse Warning to detect the usage of the println
into the code. Every time a developer type System.out.prinltn(...)
the warning icon appears beside his code - and we are very proud about the
zero-warning status of our code. One can ask why do not simply require the developers
to adopt the project code-style?
. The answer is simple: it doesn't work, never
worked and I don't believe a human being is able to obey such non intuitive
rules whithout an ubiquitous advisor. The code-style maintenance can be a very boring
task and every time a developer becomes tired he will try to get past the rules. An IDE
guard can be (and must be) a good friend of the developers. The image below shows an
example of warning generated when the developer types println anywhere on the code:


Image 1: The Eclipse Problems View

Creating an Eclipse
with AspectJ

The decision about using a pure Java implementation, based on annotations,
or about using an external tool like AspectJ is very controversial
3, 4,
I will leave this discussion to a further blog entry - for now I will simply
enumerate the steps required to create a new warning on the Eclipse IDE.

  • Download and install your preferred flavor of the Eclipe 3.1.+ (I'm using the WTP 0.7).
  • Use the Help/Software Update feature to be sure you have the AspectJ installed in your Eclipse.
  • Create a Java Project.
  • Right Click on the project name and choose Convert to AspectJ Project.
  • Done! You are ready to start to write the aspects definitions.

A demonstration Project

For a demonstration, I created a simple project with just one class and one aspect definition.
The project can be downloaded here. Just download the demo and use
the Import feature for assembling the project into your Eclipse. A tip: after installing
the project on the eclipse, type some println commands on the test class, and observe the
behaviour of the IDE.

Below is the aspect code used in the example project. Note the call(* *.println(..)))
- this command indicates that every println command executed by the JVM will be intercepted by
the aspect definition. Other interesting code fragment is the warning definition, which declares
a warning guard for the Eclipse View.


public aspect Test {
/** Everything outside the aspect package. */
pointcut not_aspect(): !within(*);

/** Pointcut to get any 'println' occurence (except in aspects). */
pointcut all_print_not_aspect():
(call(* *.println(..))) && not_aspect();

/** A warning definition. */
declare warning : all_print_not_aspect() :
"Sorry, we can't use println in our project";

Simple, isn't it? Not that much, the above example is just a bit of AOP. More sophisticated
controls over the code, including complete use cases implementations are deeply discussed
for a while by the software community. The aim of this blog entry is just registering an
approach we are adopting in our project to try a better code-style and to evaluate its
impact on the day by day of developers. Cejug-Classifieds has a lot of aspect definitions right now,
and we would like to invite you to visit our project and to test by yourself - your
feedback is very important to us.

Related Topics >>