1

Im trying to read the return value of a Java Method and save it into a JS variable. According to the documentation, this should do the job:

Native Java method that returns the value:

static public double getValue() {
    return 21.0;
}

Creation of a reference to call the native Java from handwritten JS:

$wnd.showValue=function() {
    val=$entry(@whateverpackage.thisclass::getValue());
    alert("Value: "+val);
}

And finally, in plain JS:

showValue();

The output shown in the alert box is this:

Value: function(){try{return hh(c,this,arguments)}catch(b){throw b}}

Im guessing that instead of getting the return value, it gets the function that GWT compiler produces itself and dumps it on the variable. What is wrong in this? Like I said, there is a very similar example in the official documentation, so this should be the way. Thanks in advance.

1 Answer 1

4

You have to add an extra pair of brackets after your function reference. The first pair contains the function signature (describing the parameter types). Now you're not executing the function but instead passing the actual function to $entry()

So change this

val=$entry(@whateverpackage.thisclass::getValue());

to this

var val = $entry(@whateverpackage.thisclass::getValue()());

Btw I added the var keyword to prevent any potential scope conflicts.

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

2 Comments

$entry runs scheduleEntry and scheduleFinally schedulled commands, and more importantly catches exception and routes them through your GWT.UncaughtExceptionHandler if any. $entry takes a function as input and returns a function, so use it like var val = $entry(@wateverpackage.thisclass::getValue())(); (note: ())(), not ()()), as it would call the function and then only wrap its return value: useless)
That's a nice remark, specially the bracket part, that looks pretty tricky.

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.