I want to trace automatically all java methods entry and exit with my own logging system. But I don't want to pollute all the code with this.
Do you know if it exist a way to automatically add that at compile time ?
I want to trace automatically all java methods entry and exit with my own logging system. But I don't want to pollute all the code with this.
Do you know if it exist a way to automatically add that at compile time ?
you may want to check AspectJ and the concept of aspect programming.
Aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development.
Here's AspectJ project page.
In synthesis you will be able to define where your additional logging code will need to be plugged by the compiler at annotation processing time.
For example:
@Pointcut("call(* aspects.trace.demo.*.*(..))")
public void traceMethodsInDemoPackage() {}
This will include all the calls to any class in those packages with any argument. After the pointcut definition you can specify advices like:
@Before("traceMethodsInDemoPackage()")
public void beforeTraceMethods(JoinPoint joinPoint) {
// trace logic ..
}
You will be able to access the actual parameters and other objects through the JointPoint interface.
Be aware though that input/output operations may be expensive, so you will need to balance the amount of information to be logged with very specific pointcuts.
Hope this helps.
Cheers!
You can use the below mentioned steps: