I wonder if anyone has an approach to avoids (or lessen) the tedious 
instanceof if loops in the following kind of code:

public static void javaMethod(Object value) { 
 if (value instanceof String) {
jsniMethod((String) value);
 } else if (value instanceof Number) {
jsniMethod(((Number) value).doubleValue());
 } else if (value instanceof Boolean) {
jsniMethod((Boolean) value);
 } else { 
       if (value!=null) 
          throw new RuntimeException("not a primitive : " + 
value.getClass().getName());
 }
}

...

public static native void jsniMethod(String arg) /*-{
    jsMethod(arg);
}-*/;

public static native void jsniMethod(double arg) /*-{
    jsMethod(arg);
}-*/;

public static native void jsniMethod(boolean arg) /*-{
    jsMethod(arg);
}-*/;


and in JavaScript - a single method

jsMethod = function(data) { 
       alert('hello ' + data);
};

Part of the annoyance is tedium, but I often wonder if I might miss a case. 
 What I'd love is for a class JavaScriptPrimitive or something like that. 
 JSONValue is pretty close in semantics, but I don't think it works for 
this use case.

so, then I could write a single

public static native void jsniMethod(JavaScriptPrimitive arg) /*-{
    jsMethod(arg);
}-*/;

and perhaps have a single utility cast method toJavaScriptPrimitive

public static void javaMethod(Object value) { 
  jsniMethod(toJavaScriptPrimitive(value));
}

I've written code like this so often now, I'm resigned to continuing along 
those lines  - but I am posting here because I wonder if there is a better 
way?  

Thanks,

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to