You can use AspectJ with weaving load times (javaagent). You can, for example, write aspects for tracking constructor calls (call points / execution points) and access to the monitor field (set / get points).
I use annotation based development. For example, to control the setting of all non-static non-final and non-transition fields in all classes in a given package, you can create an aspect:
@Aspect public class MonitorAspect { @Around(" set(!static !final !transient * (*) . *) && args(newVal) && target(t) && within(your.target.package.*) ") public void aroundSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable{ Signature signature = jp.getSignature(); String fieldName = signature.getName(); Field field = t.getClass().getDeclaredField(fieldName); field.setAccessible(true); Object oldVal = field.get(t); System.out.println("Before set field. " + "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass()); jp.proceed(); } }
in META-INF place aop.xml:
<?xml version="1.0" encoding="UTF-8"?> <aspectj> <aspects> <aspect name="your.package.MonitorAspect" /> </aspects> </aspectj>
Put acpectjrt.jar and aspectjweaver.jar in the class path and start the JVM with the -javaagent:lib/aspectjweaver.jar . Here are some examples and documentation http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj.html
source share