Logging the data - Most Urgent
Hi,
I want to write a Annotation which log the data before and after executing the method.
I shouldn't use reflection API to call the method.
Ex:
@LogAnnotation
public static String testMethod(String str, String str2)
{
return str + str2;
}
if I call the above testMethod("test", "test1") method the data "test" and "test1" should log into a logfile. Then after I need to log the return type result(ex:testtest1) also.
How I can do this with Annotations.
Annotations don't do stuff, they just is stuff.
You need a processor to read the annotation data and do stuff.
Here's a first attempt
@Target(ElementType.METHOD)
@interface LogMe {
int urgency();
}
and use it @LogMe(urgency=5)
Your processor will need to find all the elements annotated with LogMe and find the most urgent one by refering to the urgency member.
YIATTP
Then you need to find a way to intercept it. You could use dynamic proxies if you are coding to interfaces (but that's sort of reflection), or an AOP framework. I don't have any experience with AOP but others tell me Spring does it quite well.
Bruce
Bruce