dcsimg
How do you programatically get access to the names and contents of all variables in the current variable scope ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Niklas_Andersson
Posted On:   Thursday, June 14, 2001 03:12 PM

How do you programatically get access to the names and contents of all variables in the current variable scope ? (attributes and local variables in the current method) I am usually mostly interested in Strings and the primitive datatype int , but any objects toString() representation might also be of interest. What I would like to do, is to *avoid* manually concatenating here and there in the code like the ugly code example below, and being able to let java itself figure out what attributes and local variables that currently exist in the method with the code. As an example, I would like to be able to replace hardcoded code like this: try { // ... some code...   More>>

How do you programatically get access to the names and contents of all variables in the current variable scope ?
(attributes and local variables in the current method)

I am usually mostly interested in Strings and the primitive datatype
int , but any objects toString() representation might also be of interest.

What I would like to do, is to *avoid* manually concatenating here and there in
the code like the ugly code example below, and being able to let
java itself figure out what attributes and local variables that currently exist in the
method with the code.

As an example, I would like to be able to replace hardcoded code like this:

			
try {
// ... some code...
}
catch(myException e) {
String s = "An exception occurred in myClass.myMethod, when the
attributes and local variables had the following values: ";
s += " myStringAttribute = " + myStringAttribute ;
s += ", myIntAttribute = " + myIntAttribute;
s += ", myLocalString = " + myLocalString;
s += ", myLocalInt = " + myLocalInt;
writeToLogfile(s);
}


with call to some general method which automatically retrieves a string with the names
and the contents of all attributes and local variables, kind of like this:
			
try {
// ... some code...
}
catch(myException e) {
String s = "An exception occurred in myClass.myMethod, when the attributes and local variables had the following values: ";
s += SomeClass.getStringWithAllVariableNamesAndValues(); // Here the requested method is called
writeToLogfile(s);
}


Does it exist such a class and method in the standard java distribution,
or as a third party class ?    <<Less

Re: How do you programatically get access to the names and contents of all variables in the current variable scope ?

Posted By:   Sandip_Chitale  
Posted On:   Saturday, June 16, 2001 11:03 AM

Your best bet is JVMDI -


http://java.sun.com/j2se/1.3/docs/guide/jpda/jvmdi-spec.html


Other alternative would be write a
preprocessor on your java code and
generates the code. Then compile it.


More to come...

About | Sitemap | Contact