Using the @Loggable annotation

October 14, 2009

With @Loggable you can tell AnnoJ to generate logging method calls at the beggining of some methods. For example if you have the following class definition

@Loggable
public class InterestingClass{
     public void importantMethod(){
          //some code here
     }
}

AnnoJ will modify importantMethod() to look like this:

importantMethod(){
     log("importantMethod called");
     //some code here
}

That is you can log when a method gets called without manually writing the logging code. What’s better AnnoJ will log the actual parameter values. Combined with the @ToString annotation this can be a powerful tool.

As with @ToString if you place @Loggable before a class definition then all methods will be logged. If you want to exclude some methods use the @Exclude annotation (just put it before the method you want to leave out). Alternatively you can specify in a String array which methods to include:

@Loggable(methods={"importantMethod"})
public class InterestingClass{
     public void importantMethod(){
          //other methods
     }
}

In this case only importantMethod() will be logged.

If you want that all methods of all classes of a package be logged just define a public class called PackageInfo and annotate it with @Loggable (you cant use parameters here).

The @Loggable annotation uses Log4j so you must provide a properly configured log4j.properties file in the root of your application.

As with the other annotations to use @Loggable you must bootstrap your application.


Using the @ToString annotation

October 14, 2009

One of the annotation types of AnnoJ is @ToString. With @ToString you can leave the implementation of the toString() methods to AnnoJ. After bootstrapping your application you can use toString() as if you implemented it manually.

To generate the toString() method in a class simply put the @ToString annotation just before the class definition:


@ToString
	public class InterestingClass{
		private Integer x;
                private String s;
                // more code
	}

After bootstrapping the application AnnoJ will generate a toString() method similar to this:


public String toString(){

return "[" + (this.x==null?"":"x="+this.x) + "," + (this.s==null?"":"s="+this.s) + "]";

}

If you want to leave x from the method just place the @Exclude annotation before the field:


@ToString
	public class InterestingClass{
                @Exclude
		private Integer x;
                private String s;
                // more code
	}

To generate a toString() method in all classes of a package you should define a public class named PackageInfo and annotate it with @ToString:


@ToString public class PackageInfo{}

In the future versions of AnnoJ there will be an option to leave out from toString() generation all fields with a given visiblity and you will be able to use the @Include annotation together with @ToString.


How to use AnnoJ?

October 14, 2009

As I mentioned in the previous post in AnnoJ everything’s implemented with annotations. More precisely: with runtime annotations. This means that you can place annotations on your packages (I will explain how), classes, methods and fields and these are evaluated at runtime. In the current version of AnnoJ the problem of runtime annotation evaluation is solved with the use of a custom classloader. You must load all your annotated classes with this classloader. To make this task easy AnnoJ defines the MainMethodBootstrap class. This class is responsible for the corrent processing of the annotations. The only thing you have to do is to tell it the name of the main class of your application and the command-line arguments you want to pass. An example:

 public class StarterClass{

		public static void main(String[] args){
			MainMethodBootstrap.callMainMethod("ClassWithMainMethod",
						 new String[]{"something"});
		}
	}

Here ClassWithMainMethod is the main class (ie. contains a public static void main(String[]) method).The command-line arguments to be passed to the main method are listed in the second argument of the callMainMethod() method. And that’s all!

One important thing is that because of the custom classloader at the moment AnnoJ can’t be used in application servers. Hopefully this will change in the future versions.


Follow

Get every new post delivered to your Inbox.