1

Is it possible to get an Object from the Heap in a JVM and call a method on it.

Lets say I have this:

public class TestObjectOnHeap {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        }
    }
}

And I created a web application that instantiated the class like this

TestObjectOnHeap obj = new TestObjectOnHeap();
obj.setName("created in webapp");

Then I wanted to create a different application (maybe via a javaagent?) that read that value and printed "created in webapp"

Is this possible?

mkobit
  • 43,979
  • 12
  • 156
  • 150
tinytelly
  • 279
  • 2
  • 4
  • 14
  • 1
    Are you talking about trying to acquire a new reference to an otherwise completely unknown object that exists on the JVM's heap? – chrylis -cautiouslyoptimistic- Mar 06 '15 at 00:09
  • I will know the name and type of the Object in the Heap. But I actually want to get the value of name from that object after it was created by a totally different application. So I hope to be able to do this: `TestObjectOnHeap obj = someMagicToGetObjectFromHeap(); Assert.equals("created in webapp", obj.getName()); ` – tinytelly Mar 06 '15 at 01:01
  • 1
    What you're talking about is called a *forged reference* and would completely break the JVM security model if permitted. – chrylis -cautiouslyoptimistic- Mar 06 '15 at 01:14
  • it might be possible with `sun.misc.Unsafe`, but that's basically playing a game of chicken against the VM. And the VM has no brakes. – the8472 Mar 06 '15 at 11:51

1 Answers1

1

No, it is not possible. You'd have to pass it to the other program, or make it available to the other program through a service, or save it to a database where the other program could find it. In any case, it's going to involve some kind of serialization.

Community
  • 1
  • 1
gknicker
  • 5,509
  • 2
  • 25
  • 41