aspose file tools*
The moose likes Java in General and the fly likes Finding Objects in JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding Objects in JVM" Watch "Finding Objects in JVM" New topic
Author

Finding Objects in JVM

Vinayak Sharma
Greenhorn

Joined: Jan 28, 2003
Posts: 13
Hi,

Is there a way by which I can get a reference to any object in JVM?

OR

Is there way I can get reference to all the active object instances that are currently available in the JVM ?

OR

Is there a way I can get a reference of all the objects active of a given class in JVM?

For Example :
---------------------

class A {
static void main(String args[])
{
String str = new String("Test");
}
}

class FindAObject {
static void main(String args[])
{
String strObjFound = (String) getReferenceOfObject("str");
}

Object getReference(String objName)
{

/*Steps/API call to get reference to "str" object of Class A*/;

}
}

Here I am assuming that both the classes A and FindObject are active in JVM at a given time. Its really not important to find object by name ... Any other way to find objects will do.

Thanks,
Vinayak
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

The answers: no and yes.

No, Java does not have functionality for this sort of thing. Arbitrarily retreiving objects from the heap is not safe (They could be garbage collected and then nothing would be there...)

What you could do is implement this yourself, however. A java.util.Map (probably of the HashMap or WeakHashMap variety) will allow you to map two objects together.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

...Or depending on how you do this, you could use reflection. (See the java.lang.reflect package). But Reflection would not work in this case, as the "str" variable is a local, not a member, variable.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Or you could use the debugger interface (JPDA) and do this from another JVM altogether; this is how modern debuggers and profilers work. Start here to learn about this.


[Jess in Action][AskingGoodQuestions]
Jerry Crothers
Ranch Hand

Joined: Mar 06, 2001
Posts: 34
I asked this question myself in the Advanced Java forum. Some guy gave one idea: declare the variables in a class and use reflection: Class.getField("str");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding Objects in JVM