The reason for these problems is a chicken-and-egg problem wrt to finding the right converter for incoming data. DWR only uses type information from the method signatures to know what converter to use on the data (unless class-mapping is used). As converters don't register what type they expect as input, we have the following assumptions in the code to allow method overloading without resorting to having to try running several converters for a single piece of incoming data:
- JS basic types (numbers, string etc) hardwired to corresponding type in Java
- JS object types may have custom converters subclassed from BasicObjectConverter
This is what you see in lines 204-220 ("Added to increase our ability to call overloaded methods accurately"), checked in 2010 by David, where we call isJavaScriptTypeAssignableTo() to allow us to use overloaded methods with the same number of parameters but different basic JS types, f ex:
js:myMethod("") -> java:myMethod(String)
js:myMethod({}) -> java:myMethod(MyClass)
This is the part which is conflicting with your code that relies on the DWR2 behaviour.
You are also mentioning line 164-169 (Remove non-varargs methods which declare less params than were passed), checked in 2008 by Joe. The reason more parameters are allowed, is to offer JavaScript semantics on unspecified parameters with nullable types, f ex:
js:myMethod("") -> java:myMethod(String, String /*null*/, String /*null*/)
This is involved more by fluke in your case, and would not be significant in a somewhat different setup.
To sum up, my view is that we should support your use case where one can register a converter to deal with any JS type. Unfortunately, this conflicts with type-based method overloading which is also something that has been requested by other DWR users. There are two fixes:
- refactor converter registration to allow specifying input type
- add a DWR configuration parameter ("strictMethodOverloading=false") that provides possibility to use DWR2-like behaviour (disabling the isJavaScriptTypeAssignableTo() check)
My opinion is that it is too late for refactoring converters in DWR3, and it should be done in the next release instead. This leaves us with the configuration parameter solution.
What are your thoughts?
Dealing with overloaded methods is tricky and has always been difficult. I agree with you about the code on 165 but I need to check with Mike first. It should probably just be looking for a match on the args length if vararg methods aren't involved (remove if exact match not found). I am hesitant to modify it without more thought as it was added by Joe quite awhile back, I am assuming for a good reason.
In the meantime, have you tried using class mappings? If DWR knows what type your Object is than it should find the correct method in LocalUtil.isJavaScriptTypeAssignableTo. Without class mapping DWR knows it has an Object but it doesn't know what type that Object is. Why the complexity? Think about the following overloaded case:
public void overloaded(TypeA a);
public void overloaded(TypeB b);
Try class mapping and let me know.
http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/converters/bean.html#mappingJavaToJavaScript