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.

[528 byte] By [Thiru_Javaa] at [2007-10-3 5:05:09]
# 1
>I shouldn't use reflection API to call the method.Why not?
brucechapmana at 2007-7-14 23:11:04 > top of Java-index,Core,Core APIs...
# 2
Becoz I will call the annoted method by the calss object.Ex: Test testObj = new Test();testObj.annotedMethod(some parameters); Here I can't use refelection API.
Thiru_Javaa at 2007-7-14 23:11:04 > top of Java-index,Core,Core APIs...
# 3

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

brucechapmana at 2007-7-14 23:11:04 > top of Java-index,Core,Core APIs...
# 4
Thank you Bruce. I will try your method.-Thiru
Thiru_Javaa at 2007-7-14 23:11:05 > top of Java-index,Core,Core APIs...