Since this is for debugging you could use instrumentation with aspectj, your code remains clean from the the debugging output statements and you can waeve the aspect as needed.
Define a set(FieldPattern)
point cut to catch all field assignements (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 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 with the aspect weaved 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
point cut to catch assignments, the point joins, to any variable with any name, could also in the example be
pointcut assigmentPointCut() : set(String activityState);
The advice, the desired behavior when the given point cut matches
after() : assigmentPointCut() { ... }
Informations about the point join can be accessed using the special reference thisJoinPoint.