Search code examples
javaaspectjinstrumentation

Java code instrumentation to record every access to classes fields


I would like to ask if please someone can suggest me which is the most suitable instrumentation library for accomplishing the following task:

Given a huge Java program source code I need to instrument it in order to run the program and log the following information:

For each class and for each access to its fields

  • if is a read or a write access
  • the value that has been read or wrote
  • the line code number of the class where the access is located
  • the name of the method where the access has been performed

Solution

  • Probably the best way to do this kind of trace is using AOP. There are several mature libraries for AOP in java. To start something with, take a look at this example.

    I'm not sure about logging direct access to fields, but you could read them with getters (private or protected if you don't want to expose these fields to public).