5

Lets say i have a a button somewhere in the code: "JButton closeButton". I dont know that it's called "closeButton" but that's what i want to find out.

At runtime, that button gets clicked and once it does i can find out a lot about it through reflection and the AWT api - BUT what i can't do is find out where it is - how it's called in the code, what name is it declared as ("closeButton").

Is it possible to find this out from the JVM?

Is there a way to compile and run the code in such a way that the names for the instances get preserved at runtime?

Is there perhaps some type of 'javaagent' out there (free if possible) that can help me out in this situation?

Thanks

EDIT (14:23 EDT):

I am using a button as an example but it could be any type of component that can hold a value and have ActionListeners attached to it. I can obtain every bit of information via reflection about that component but i cant find it in the code. Even if there are 10 components that have been declared with the same name, that still gives me a lead, i can eliminate possibilities.

3
  • not possible without running java debugger - why do you want to do this? Commented Jan 31, 2011 at 19:11
  • The application that i am working on is massive (millions of lines), components get created and passed down various classes. Their labels are created on the fly. In this case it's actually a field whose value i need do something with, i just used a button as an example. Commented Jan 31, 2011 at 19:20
  • 2
    My deepest condolences to you. Commented Jan 31, 2011 at 19:26

4 Answers 4

5


import java.lang.reflect.*;

public class field1 { private double d; public static final int i = 37; String s = "testing";

  public static void main(String args[])
  {
     try {
        Class cls = Class.forName("field1");

        Field fieldlist[] 
          = cls.getDeclaredFields();
        for (int i 
          = 0; i < fieldlist.length; i++) {
           Field fld = fieldlist[i];
           System.out.println("name
              = " + fld.getName());
           System.out.println("decl class = " +
                       fld.getDeclaringClass());
           System.out.println("type
              = " + fld.getType());
           int mod = fld.getModifiers();
           System.out.println("modifiers = " +
                      Modifier.toString(mod));
           System.out.println("-----");
        }
      }
      catch (Throwable e) {
         System.err.println(e);
      }
   }

}

This should give you a list of all the fields of the class.

Sign up to request clarification or add additional context in comments.

8 Comments

sorry but the formatting got messed up and I don't have much time to fix it right now
Very nice - I love reflections.
Just add 4 spaces on the blank lines and that will fix it.
@Michael - I do as well, I abuse them on a daily basis although this is actually the first time I've done it in Java for an actual purpose rather than just to see if I could.
that's a good try but it doesnt work, i already tried that this morning. "getdeclaringClass()" returns null.
|
3

If your "variable" is a field, then the solution using reflection (mentioned in another answer) works.

If your variable is a local variable, you can in theory use a JVMTI agent to get its name, provided that you compiled your class with debug info. And that your local variable is in scope when you check. And that nobody changed its value and there's no other variable with the same value (in which case you couldn't decide which is the one you need).

However, you should absolutely not do any of them. They are theoretical possibilities, akin to punching yourself so hard that you pass out.

Comments

0

Short answer: it is not possible.

Details: What you call name has in fact nothing to do with the instance of JButton. It's just the variable that holds the referece of which allows to acces this instance at runtime. This sample code will show this mor evidently:

JButton newButton = new JButton();
JButton otherButton = newButton;

Here both variables holds a reference to teh same instance of JButton.

3 Comments

typo: newButton(); --> newButton;
is there a way to map the reference to a name at runtime?
all AWT components have a name property, may be that is what you're looking for.
0

I think you have 2 ways:

  1. use java debugging API, i.e. turn your program into a small debugger.
  2. Use byte code engineering library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.