An external javascript gives a number that should be handed over to Java method named mycallback.
I have defined:
Java:
class MyClass {
public static void mycallback(JavaScriptObject number) {
// do something with the number
}
}
Javascript:
$wnd.callback = $entry(#com.package.MyClass::mycallback(Lcom/google/gwt/core/client/JavaScriptObject));
And the Javascript call is:
$wnd.callback(number_from_external_javascript);
But I get error:
JS value of type number, expected com.google.gwt.core.client.JavaScriptObject
And my ultimate goal is to have a java method with parameter type of Integer, not of JavascriptObject. I just thought GWT should wrap javascript objects in JavascriptObject, but it seems it won't.
GWT version is 2.4.
GWT will automatically cast a JS Number value to any Java number primitive type (int, double, etc.), JS String to Java String, and JS Boolean to Java boolean. It'll never pass them as JavaScriptObjects.
If the number cannot be null, then just declare your callback with an int argument. If it can be null, then you'll have to explicitly create an Integer instance, something like:
$wnd.callback = $entry(function(n) {
if (number != null) {
// box into java.lang.Integer
number = #java.lang.Integer::valueOf(I)(n);
}
#com.packge.MyClass::mycallback(Ljava/lang/Integer;)(number);
});
Alternatively, I think you can pass a JS number as a JavaScriptObject if it's a Number object rather than a Number value, so this might work:
$wnd.callback = $entry(function(n) {
n = new Number(n); // "box" as a Number object
#com.packge.MyClass::mycallback(Lcom/google/gwt/core/client/JavaScriptObject;)(n);
});
What about using the gwt-exporter generator to expose your gwt code to js, so you dont have to deal with jsni and you could benefit of the nice features it has (complex objects, arrays, closures, overlays, doclet, etc)
Using gwt-exporter your class just have to implement Exportable and use an annotation to expose your method.
public static class MyClass implements Exportable {
#Export("$wnd.mycallback")
public static void mycallback(long number) {
Window.alert("" + number);
}
}
Add this line to your onmoduleload and leave the compiler to do the work
public void onModuleLoad() {
ExporterUtil.exportAll();
}
Then you can use the method as you said
<script>
window.mycallback(1234)
</script>
Related
We are using Kryo to communicate between a Scala application and a Java application. Since the class definitions have to be used from Java (and we don't want to include the Scala library as a dependency in the Java applicaton) we are using JavaBeans to define the transfer objects.
However, using JavaBeans directly in Scala is a bit of a hassle. No pattern matching, having to use new, etc. What we're doing right now is defining extractors and apply methods in separate objects on the Scala side to make it nicer to work with these classes.
Since most of what we need is boilerplate, we are wondering if there would be a way of doing this automatically. For example, we have this JavaBean (there are about 20+ different message types):
public class HandshakeRequest extends Request {
private String gatewayId;
private String requestId;
private Date timestamp = new Date();
public HandshakeRequest(String gatewayId, String requestId) {
this.gatewayId = gatewayId;
this.requestId = requestId;
}
public String getGatewayId() { return gatewayId; }
public String getRequestId() { return requestId; }
public Date getTimestamp() { return timestamp; }
private HandshakeRequest() { /* For Kryo */ }
}
This is an example of the object we use to bridge to Scala:
object Handshake {
def unapply(msg: HandshakeRequest): Option[ (DateTime, String, String) ] = {
(
new DateTime(msg.getTimestamp.getTime),
msg.getRequestId,
msg.getGatewayId
).some
}
def apply(gatewayId: String, requestId: String) = new HandshakeRequest(gatewayId, requestId)
}
Since all of our object have the Timestamp, it is also part of the boilerplate. We'd like some way (perhaps a macro?) to automatically generate the unapply and apply methods (and ideally, the whole object itself).
Does anyone know of an easy way to accomplish this?
Since there were no answers, I came up with this: https://github.com/yetu/scala-beanutils.
I've started a project https://github.com/limansky/beanpuree which allows convertions from beans to case classes. I'm going to add some more features, like automatic type convertion between Java number classes and Scala classes.
I have a pojo in my class containing some methods to manipulate Maps and Arrays in java. This object is used in RPC calls to carry my configurations. I have a mechanism in which before making any RPC call I execute a javascript function. Now what I really want is to pass my configuration object to this javascript function and this javascript function can manipulate this configuration object and finally this manipulated object will be passed in my RPC call.
So how can I pass my java object to javascript and allow manipulating it?
First, you cannot manipulate Java objects from javascript directly. But what you can do, is to export a set of static methods to javascript and use them to manipulate your objects. This is done in this way:
public void onModuleLoad() {
exportHelloMethod(this);
}
public String exportedMethod(String name) {
// Manipulate your java classes here
// return something to JS
}
// Create a reference in the browser to the static java method
private native void exportHelloMethod(HelloClass instance) /*-{
$wnd.hello = instance#[...]HelloClass::exportedMethod(Ljava/lang/String;);
}-*/;
Fortunately there is a library which allows exporting java methods and classes in a simpler way. It is gwt-exporter, and you have just to implement Exportable in your class and use a set of annotations so as the exporter generator does all the work.
#ExportPackage("jsc")
#Export
public class MyClass implements Exportable {
public void show(String s){
}
}
public void onModuleLoad() {
ExporterUtil.exportAll();
}
Then in javascript you can instanciate and manipulate the class:
var myclass = new jsc.MyClass();
myclass.show('whatever');
I know that GWT doesn't emulate this method, but I need smth that provide its functionality.
I have tried next approach:
private static <T extends Widget> T createWidget(Class<T> widgetClass) {
return GWT.create(widgetClass);
}
But when I try to compile it I get an error:
Only class literals may be used as arguments to GWT.create()
So, how can I write a foresaid method that will emulate Class#newInstance?
GWT.create() always needs the class literal as argument, which means that you has to pass this: GWT.create(MyClass.class) and no other thing.
This is so because the gwt compiler has to decide which class to pick up in compile time, note that in your code the class is passed in runtime.
If you are planing to use GWT.create for a reduced and well known set of classes you can do something like that:
private static <T extends Widget> T createWidget(Class<T> widgetClass) {
if (ClassA.class.equals(widgetClass)) {
return GWT.create(ClassA.class);
} else if (ClassA.class.equals(widgetClass)) {
return GWT.create(ClassB.class);
}
return null;
}
I am going to file this as a bug report, but I wanted to check if someone here can see something wrong with what I am doing.
When you expose an instance method from a GWT class through JSNI, this works as expected in JavaScript. Since we are cross compiling Java, I would instead expect this to be bound to the instance automatically. For example:
package com.test;
class Foo {
public void instanceFunction() {
this.otherFunction() // will cause an error when called from JSNI!
}
public void otherFunction() {
// does some stuff
}
public native JavaScriptObject getInstanceFunction() /*-{
return this.#com.test.Foo::instanceFunction();
}-*/;
}
Currently the workaround is to bind the function yourself (not very portable):
public native JavaScriptObject getInstanceFunction() /*-{
return this.#com.test.Foo::instanceFunction().bind(this);
}-*/;
This can also be seen as preference, some may prefer that the functions remain unbound. I would say the current functionality is unintuitave and unnecessary. I cannot imagine a use case for having an unbound this directly in Java code. Also, some browsers do not implement bind(1), so my workaround is not robust.
If you want a portable bind, it's as easy as:
var that = this;
return $entry(function() {
return that.#com.test.Foo::instanceFunction()();
});
I need to call a Java method from Javascript. So I defined the method:
private native void registerMethod() /*-{
var self = this;
$wnd.test = function(longParam) {
self.#mypackage.HomeView::test(Ljava/lang/Long;)(longParam);
};
}-*/;
The Java method:
private void test(Long longParam) {
GWT.log("Call to test with longParam = " + longParam);
}
The JS call:
public static native void paypalClose() /*-{
$wnd.alert(top.test);
top.test(10);
top.dgFlow.closeFlow();
top.close();
}-*/;
The alert shows the Javascript function definition. If I call top.test(), it works but no param is passed. But if I call top.test(10) I get a null alert window.
Your method excepts a java.lang.Long, not a long, so you have to create an instance of Long. You could use #java.lang.Long::new or #java.lang.Long::valueOf, except you cannot use longs either in JSNI: https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#important
Because JavaScript numbers are strictly equivalent to Java doubles, you should use a double or java.lang.Double as an argument. Once in the Java world, you can cast the double to a long if you like, but not from/in JSNI.