How to create custom java annotation to log method parameters
I am writting JavaEE application and I would like to use and create custom annotation, which will log data, when annotated method will be called. All I would like to do is, that when annotated method is called, code iterate through all passed method parameters and writes to standard output parameter keys and values.
Some example:
public class Test {
@LogMethodData
public int sum(int first, int second) {
return first + second;
}
}
I would like to achieve, that when a custom metod will be annotated with @LogMethodData, that code behind will take care and log passed method parameters to standard output (something like "Method data: first - 4, second - 5" if parameter first contains value 4 and parameter second contains value 5), independent from number of passed parameters to methods.
I would be very happy if someone could help me with that, because I have been already searching for a solution, but I didn't found anything usefull. And last, I am not familiar with this things.
Regards, Dahakka
There is no need to define your own Annotation, you can use the @Interceptors
Annotation in the EE Container as explained here.
@Interceptors(LoggingInterceptor.class)
Within the Interceptor you'll get a Context that contains your Parameters
public class LoggingInterceptor {
...
@AroundInvoke
public Object modifyGreeting(InvocationContext ctx) throws Exception {
....
Object[] parameters = ctx.getParameters();
try {
return ctx.proceed();
} catch (Exception e) {
logger.warning("Error calling ctx.proceed in modifyGreeting()");
return null;
}
}
}
another example : here
链接地址: http://www.djcxy.com/p/76664.html上一篇: 在R中有不同形状的Wordcloud
下一篇: 如何创建自定义Java注释来记录方法参数