开发者

GWT problem with Long in Javascript JNI overlay types

开发者 https://www.devze.com 2023-03-08 10:18 出处:网络
I\'m having a problem with the conversion of a JSON to a JNI overlay type. The java code has to following method:

I'm having a problem with the conversion of a JSON to a JNI overlay type. The java code has to following method:

long nr = 10l; 
public Long getNr() {
    return nr;
}

The JNI overlay type is:

public final native Long getNr() /*-{
    return this.nr;
}-*/;

I'm avoiding operating with the long primitive in the overlay type, as the compiler doesn't allow it. The official documentation says that this is inefficient but it should work. However, I'm getting:

 java.lang.IllegalArgumentException: Something other than a Java object was returned from JSNI method '@com.开发者_运维知识库avaya.thunder.portal.client.shared.model.Customer::getNr1()': JS value of type int, expected java.lang.Object
at com.google.gwt.dev.shell.JsValueGlue.get(JsValueGlue.java:178)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:271)

I'm using GWT 2.2.0.

Is it something I'm doing wrong? Should this work? Any help is appreciated. Thanks.


You need to put in a Long object if you want to get one out:

public native void setNr(Long val) /*-{
  this.nr = val;
}-*/;

Unfortunately, that means that 'nr' will be an opaque object in JavaScript. Usually, we pass around 'double' primitives if we want to manipulate a number in both JS and Java. That way there are no surprises (Java primitive double maps directly to Js type Number).

0

精彩评论

暂无评论...
验证码 换一张
取 消