Access to Java class variable during runtime

124 views
Skip to first unread message

Michael Vogt

unread,
Jan 11, 2013, 7:59:46 AM1/11/13
to google-we...@googlegroups.com
Hello.

I would like to have to possibility to access a Java class variable from JavaScript, without changing the Java class itself. The idea is, to display the values of the variables continuously while the application is running. The usecase for me right now would be, that I want to check the calculations of a drag while I do the drag. Sure I can add GWT.log() calls, but accessing these values without code changes would be so much nicer.

I already tried to find a way, but failed so far. Any tips how to get there? Maybe by providing a specific Generator that adds some JSNI code automatically?


Thanks,
Michael

RyanZA

unread,
Jan 11, 2013, 9:05:53 AM1/11/13
to google-we...@googlegroups.com
Hi

No need to go so extreme - https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#methods-fields

You can access static java methods from javascript as described in that link.

RyanZA

unread,
Jan 11, 2013, 9:10:04 AM1/11/13
to google-we...@googlegroups.com
Just to add a bit more in case that wasn't clear:

If you have a class 'Class1' that you cannot modify with a static field:

public final Class1 {
 public static int counter = 55;
}

You can make a new class, ie. 'Class1Exposer'

public class Class1Exposer {
 public static int getClass1Counter() {
  return Class1.counter;
 }
 
    public static native void exportStaticMethod() /*-{
       $wnd.counter =
          $entry(@mypackage.
Class1Exposer::getClass1Counter());
    }-*/
;

Michael Vogt

unread,
Jan 11, 2013, 1:24:36 PM1/11/13
to google-we...@googlegroups.com
Hello.

Thank you for your answer.


No need to go so extreme - https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#methods-fields
You can access static java methods from javascript as described in that link.

 Yes, what you suggest works, of course. What I would like to achieve is to also access private and non-static fields. This can easily be done by adding a native function to every class, who's fields I want to access. That works already (well, except that entry does only handle functions, but this is an easy change). But I would like to get there without the need to write extra code for this, if possible.

When looking at the application with a JavaScript debugger, I see the classes and variable, just did not find a way to access them by JavaScript yet.


Cheers,
Michael

Jens

unread,
Jan 11, 2013, 2:04:54 PM1/11/13
to google-we...@googlegroups.com

Firefox specific:
- FireBug: Lets mark a method to be logged (incl. method parameters) every time it is called. See http://getfirebug.com/javascript and scroll down to "Logging function calls". Maybe that would be enough for you?

Browsers DevTools only shows you variable content when a breakpoint is active and you step through the script line by line (variable watches). But while in a breakpoint you can't do your drag/drop action you want to observe.

If FireBug does not work for you, you are probably faster in simply using java.util.logging.Logger in your code and disable logging for production compilations.

-- J.

Michael Vogt

unread,
Jan 12, 2013, 3:20:15 AM1/12/13
to google-we...@googlegroups.com
Very helpful. Thanks.

I think I go with the proxies.
Reply all
Reply to author
Forward
0 new messages