6

Hello is there a way to know the caller class name of a function, specifically for a Java-GWT application?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • possible duplicate of [Java: Find Caller Class](http://stackoverflow.com/questions/2887607/java-find-caller-class) – JB Nizet Jan 23 '12 at 09:18
  • 1
    Reflection and related APIs are not emulated by GWT. You need to find another way to accomplish what you want. – Strelok Jan 23 '12 at 09:24

2 Answers2

4

Thread.currentThread is not supported in GWT (remember that java code gets compiled to javascript), so this is a possible duplicate of:

How do you find out the caller function in JavaScript?

In GWT you'd simply write a jsni wrapper:

public static native void whosMyCaller() /*-{
    $wnd.alert(arguments.callee.caller.toString());
}-*/;
Community
  • 1
  • 1
milan
  • 11,872
  • 3
  • 42
  • 49
  • Is there any "utility" function to do that? – quarks Jan 23 '12 at 09:40
  • there's no such 'utility' in GWT (2.4.0), you could simply make a jsni method – milan Jan 23 '12 at 09:53
  • I'm getting this kind of result: function __gwt_isInvoke(thisObj, methodName){...} – quarks Jan 23 '12 at 10:41
  • 1
    @xybrek in DevMode that's pretty much all you're going to be seeing. In web mode (compiled) you will most likely see just obfuscated function names like `f(l,K,v){...}`. Basically not helpful at all. If you explained in the question what you actually need to accomplish, perhaps you would get a better solution. – Strelok Jan 23 '12 at 22:41
  • Is there any way to get this as a String for diagnostic purposes? – Jared Hooper Aug 17 '15 at 16:19
  • @TheJaredHooper instead of $wnd.alert try return – milan Aug 18 '15 at 06:52
  • @milan did not see the toString().... -_- thanks haha. If I change it to `public static native String whosMyCaller()/*-{ return arguments.callee.caller.toString(); }-*/`; then it works for me – Jared Hooper Aug 18 '15 at 14:50
0

It is not really possible in GWT, since in production mode code is heavily optimized with in-lining and removal of unreachable code. E.g. in compiled mode most of functions doesn't belong to any class, since GWT considered the class definition redundant. Theoretically it is possible to find the caller class (if you try to analyze compile report or if via JSNI), but because of in-lining optimization you will get a lot of strange results (for example even if you function is called only in some specific class, in compiled mode you might find that it is directly called from entry point onLoad method)

jusio
  • 9,850
  • 1
  • 42
  • 57
  • You meannormal java? it is possible via Thread.currentThread().getStackTrace() as other people said. In DevMode this might be possible as well, but I'm not sure if Thread class is allowed in DevMode. You might try to create an Exceptino and analyze it's stacktrace. – jusio Jan 23 '12 at 09:48