This is the scenario:
I have instantiated a String in a class, then I have obtained its position in HEAP:
public class UnsafeExperiment {
static String s = "ciao";
public UnsafeExperiment() throws Exception {
Unsafe unsafe = getUnsafeInstance();
Field field = this.getClass().getDeclaredField("s");
field.setAccessible(true);
long position = unsafe.staticFieldOffset(field);
wait();
}
private Unsafe getUnsafeInstance() throws Exception {
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
return (Unsafe) theUnsafeInstance.get(Unsafe.class);
}
}
From another class, how can I call the String in the HEAP using its address ? (please, I am not interested in consideration about opportunity to use this approach.. I know from the library involved it is unsafe )