Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Impossible to pass implicitly vairable names of values by explicitly passing the other?

Thread view: 
dvdavins@pobox.com - 06 Jul 2007 22:17 GMT
I think what I'd like to do is impossible, but I'll check here before
giving up.

I'm wiriting a utility method to assist in debugging. It will display
variable names and the values of those variables.

I don't think there's any way for a method to access the values that
exist in the calling context of variable names that the mothod is
passed. And I'm nearly certain there's no way for the method to tell
whether it was passed literals or vaiiables, let alone what the
variable names were.

So I think I'm stuck with requiring redundancy such as
displayVars("key", key, "fullName", fullName);

Am I missing something?
Twisted - 06 Jul 2007 22:36 GMT
On Jul 6, 5:17 pm, dvdav...@pobox.com wrote:
> I think what I'd like to do is impossible, but I'll check here before
> giving up.
[quoted text clipped - 12 lines]
>
> Am I missing something?

No.

If you can code an automatic check for whether the values are screwy,
though, you can do the next best thing -- throw an Error, and the
traceback will lead right to the offending part of the code, and with
a decent IDE as soon as you get the exception and click in the stack
trace you can be staring at the variable names in the culprit code.
With a good debugging IDE like Eclipse it will even be possible to
have it suspend on the throw statement and then use the debugger
interface to inspect the variables that are fubar and other state
relating to the method that was executing, and up the call chain too,
as well as the contents of various collections and suchlike at the
time the problem was detected.
Sideswipe - 07 Jul 2007 00:34 GMT
I have thought about a similar trick before. You don't need to wait
for an error to find your stack call just do: new
Throwable().getStackTrace();

>From there you can get the class and method used. You can use to class
to ask the classloader to give you the full path to the class file,
which you can load the bytes of and interrogate. What I have never
figured out though is how to get the instance of the class that
invoked your method.

When you say variables and their "values" -- the term "value" is
actually ambiguous. In the case of an Integer or BigDecimal it's
clear, in the case of a ActionEvent object, what would the "value" be?
Such an object is inherently 'valueless' . If your objects are popular
Java library objects, this won't be a problem. Simply calling the
"toString()" method will produce what that object declares it's value
is. You can try it on Map objects, List objects, anything in the
Collections package. They will produce the expected result. I have
routinely used this to output arrays as such:

System.out.println(Arrays.asList(myArray)); // not advised for
performance

Also, any of the boxed primitive types or anything descending from
Number will produce what you're looking for

If the object in question are your own, override the toString()
method. I have frequently used this trick in combination with my
debugger. I use IntelliJ and by default it invokes the toString()
method on any object in it's watch window.

You can also override the toString() method in an anonymous inner
class for sorta '1-off' behavior. Such as:

BigInteger myInteger = new BigInteger("123456") {
    public void toString() {
       return "whatever you want";
    }
};

As far as your literal v. variables question goes. The only 'literal'
that can be declared in Java, that is also an object (and thus subject
to any kind of trickery) is String -- "Christian". Literal 'int' such
as 'myMethod(10)'
can not be interrogated at all because it's a primitive type. However,
any string output will produce the correct value.

Bottom line: I have tried to do something similar and stopped when my
Devilish plan required me to somehow divine the meaning of the byte
code of a Java class file. The above method may help you as a stop gap
measure.

Christian Bongiorno
http://christian.bongiorno.org
dvdavins@pobox.com - 09 Jul 2007 00:17 GMT
Thanks. It doesn't do what i was looking for, but I have added access
to the stack as part of my Debug class in my utilites package.
Debug.getStac()  accesses the following:

package net.alltimers.util;

public class Debug {

    ...

    public static StackTraceElemen[] getStack() {
        Throwable stackHolder = new Throwable();
        StackTraceElement[] bloatedStack = stackHolder.getStackTrace();
        int len = bloatedStack.length - 1;
        StackTraceElement[] stack = new StackTraceElement[len];

        for (int i = 1; i <= len; i++) {
            stack[i - 1] = bloatedStack[i];
        }
        return stack;
    }

      ...

}
Joshua Cranmer - 09 Jul 2007 22:06 GMT
On Sun, 08 Jul 2007 16:17:31 -0700, dvdavins wrote:
>         StackTraceElement[] stack = new StackTraceElement[len];
>
>         for (int i = 1; i <= len; i++) {
>             stack[i - 1] = bloatedStack[i];
>         }

How about System.arraycopy(bloatedStack, 1, stack, 0, len); ?
dvdavins@pobox.com - 10 Jul 2007 06:18 GMT
> On Sun, 08 Jul 2007 16:17:31 -0700, dvdavins wrote:
> >            StackTraceElement[] stack = new StackTraceElement[len];
[quoted text clipped - 4 lines]
>
> How about System.arraycopy(bloatedStack, 1, stack, 0, len); ?

Thank you, muchly. arraycopy() is something I'm glad to be reminded
of. It's been a few years since I've programmed full time. I think I'd
once known about it, but had long since forgotten.
Roedy Green - 09 Jul 2007 01:36 GMT
>Am I missing something?

Note how debuggers can often display variable names.  Look at a class
file and you will see quite a few of the names embedded.

See http://mindprod.com/jgloss/debugger.html
http://mindprod.com/jgloss/jpda.html
http://mindprod.com/jgloss/javaclassformat.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.