Since this is for debugging, you can use instrumentation with aspectj , your code remains clean from the debug output pins, and you can discard the aspect if necessary.
Define a set(FieldPattern) point abbreviation set(FieldPattern) to catch all field assignments (join points)
public aspect TestAssignmentAspect { pointcut assigmentPointCut() : set(* *); after() : assigmentPointCut() { System.out.printf("%s = %s%n", thisJoinPoint.getSignature().getName(), String.valueOf(Arrays.toString(thisJoinPoint.getArgs()))); } }
Here is a test class
public class Test { public static String activityState = "stopped"; public static void main(String[] args) { activityState = "start"; doSomething(); activityState = "pause"; doSomeOtherthing(); activityState = "resume"; System.out.printf("the end!%n"); } private static void doSomeOtherthing() { System.out.printf("doing some other thing...%n"); } private static void doSomething() { System.out.printf("doing something...%n"); } }
If you run this example based on the format, the output will be
activityState = [stopped] activityState = [start] doing something... activityState = [pause] doing some other thing... activityState = [resume] the end!
Explanation
pointcut assigmentPointCut() : set(* *);
set binding points to fishing, binding points to any variable with any name, can also be in the example
pointcut assigmentPointCut() : set(String activityState);
advice , desired behavior when a given point crop matches
after() : assigmentPointCut() { ... }
Information about connecting to a point can be obtained using the special thisJoinPoint link.
source share