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

Discussion in 'Java' started by dvdavins@pobox.com, Jul 6, 2007.

  1. Guest

    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?
     
    , Jul 6, 2007
    #1
    1. Advertising

  2. Twisted Guest

    On Jul 6, 5:17 pm, wrote:
    > 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?


    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.
     
    Twisted, Jul 6, 2007
    #2
    1. Advertising

  3. Sideswipe Guest

    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
     
    Sideswipe, Jul 7, 2007
    #3
  4. Guest

    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;
    }
    return stack;
    }

    ...

    }
     
    , Jul 9, 2007
    #4
  5. Roedy Green Guest

    Roedy Green, Jul 9, 2007
    #5
  6. Re: Impossible to pass implicitly vairable names of values byexplicitly passing the other?

    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;
    > }


    How about System.arraycopy(bloatedStack, 1, stack, 0, len); ?
     
    Joshua Cranmer, Jul 9, 2007
    #6
  7. Guest

    On Jul 9, 5:06 pm, Joshua Cranmer <> wrote:
    > 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;
    > > }

    >
    > 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.
     
    , Jul 10, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Slater

    Session Vairable Problem

    Mike Slater, Jun 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    395
    Mike Slater
    Jun 27, 2003
  2. alydapan
    Replies:
    2
    Views:
    2,843
    Big Jim
    Apr 21, 2005
  3. Joseph Barillari
    Replies:
    4
    Views:
    344
    Bruno Desthuilliers
    Jul 10, 2008
  4. Alf P. Steinbach
    Replies:
    36
    Views:
    961
    Gerhard Fiedler
    Aug 1, 2009
  5. Replies:
    5
    Views:
    261
    Michele Dondi
    Jun 30, 2006
Loading...

Share This Page