0

Recently I have got a need to peek at some class field value. The class looks somewhat like this:

public class Foo(){

   @Autowired
   protected MessageResolver resolver;

   protected static final String INJECTED_TEXT_PROPERTY_CODE = "code";
   protected String injectedText;

   @PostConstruct
   public void initialize() {
    injectedText = resolver.resolveMessage(INJECTED_TEXT_PROPERTY_CODE);
   }
}

Then I have some property source like this:

code=injectedCode

Now the difficulty of the task is that it won't be on my machine but on the client machine. Therefore ideally I need some easy command to execute which will not mess with a working system.

EDIT: The field which I want to peak is injectedText.

EDIT2: Most interesting solution was introduced by @apangin (jmap and dump analyzis offline). Thanks!

pokemzok
  • 1,659
  • 1
  • 19
  • 29
  • 1
    What about making a heap dump with `jmap` and then analyzing the dump offline? – apangin Jan 25 '18 at 18:02
  • It is possible to find instances of a particular class like in [this answer](https://stackoverflow.com/questions/38042962/java-method-hooking-finding-object-instances/38044372#38044372) but this is a bit more complicated. – apangin Jan 25 '18 at 18:05

2 Answers2

0

On startup of the application you need to load the property of the client machine. Look this http://www.baeldung.com/properties-with-spring

Also, you can use @Value annotation from spring to inject a property value

import org.springframework.beans.factory.annotation.Value;

class MyClass {

   @Value("${value.from.property}")
   private String valueFromProperty;

}
0

As @apangin suggested I used jmap utility to create heap dump of my application.

Firstly I had to figure out what was my application PID. I did it with the command:

jps

Than I created a heap dump of my app ( my PID was 303):

jmap -dump:live,file=C:\dump.bin 303

Finally I analyzed heap dump with a jhat utility:

jhat C:\dump.bin
pokemzok
  • 1,659
  • 1
  • 19
  • 29