History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: JRUBY-2867
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Blocker Blocker
Assignee: Charles Oliver Nutter
Reporter: Rod Cope
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
JRuby

Wrong overloaded Java method called when both int and float signatures exist

Created: 25/Jul/08 10:45 AM   Updated: 30/Aug/08 04:16 AM
Component/s: Java Integration
Affects Version/s: JRuby 1.1.4
Fix Version/s: JRuby 1.1.5

Time Tracking:
Not Specified

Environment: Windows XP


 Description  « Hide
When calling an overloaded method on a Java class with both int and float signatures, JRuby trunk (as of 7293) always chooses to call the float version. JRuby 1.1.3 chooses properly.

Test.java:
package com.openlogic;
public class Test {
public static String doIt(int a) { return "integer: " + a; }
public static String doIt(float a) { return "float: " + a; }
}

test.rb:
include Java
require 'Test.jar'
Test = com.openlogic.Test
puts Test.doIt(3)
puts Test.doIt(4.5)

Results with jruby 1.1.3:
integer: 3
float: 4.5

Results with jruby-trunk (revision 7293):
float: 3.0
float: 4.5



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Charles Oliver Nutter - 27/Jul/08 03:03 PM
Thanks for the regression report Rod...should be easy enough to fix, I'll look into it.

Charles Oliver Nutter - 27/Jul/08 04:22 PM
This should be fixed now, and I also added appropriate smarts to select the more specific primitive-typed target method based on the size of the incoming Fixnum or Float. Specs have been enabled in spec/java_integration/coercion_spec for this logic.

Rod Cope - 27/Jul/08 09:26 PM
Thanks for looking into this so quickly, but I'm still having the same problem as of 7308. Any other ideas?

Charles Oliver Nutter - 28/Jul/08 12:55 AM
Reopening...I was a bit hasty in marking it resolved.

I think the problem is that rather than search for a best match in integer types first it's probably picking up the float types and doing an assignable search. Since int is assignable to float, it never continues to search for int typed methods that might be a better match. I'll see if I can't tighten up the logic a bit.

I'm also considering looking into Attila Szegedi's dynalang MOP, which he claims does JLS method selection for Java types. If I can wire that search logic into our Java integration layer, it might solve a lot of issues.


Charles Oliver Nutter - 28/Jul/08 02:09 PM
Ok, I rolled back my "enhancement" and went with code that better mimicked the 1.1.3 method search logic. It does fix your exact case now, but I haven't come up with a test for it. So let me know that it's working while I try to formulate a good test that fits into our suite. Fixed as of 7310.

Rod Cope - 28/Jul/08 03:34 PM
It fixes that test case, but unfortunately not my actual issue. I'm having trouble nailing down another test case, so here's a very short version of the real problem that still occurs. It requires jscience.jar (from jscience.org: http://jscience.org/jscience-4.3.1-src.zip). I hope this helps.

include Java
require 'jscience.jar'
Amount = org.jscience.physics.amount.Amount
Unit = javax.measure.unit.Unit

class Numeric
def method_missing(sym)
Amount.valueOf(self, Unit.valueOf(sym.to_s))
end
end

puts 3.kg
puts 4.5.kg

Results with 1.1.3:
3 kg
(4.5 +/- 4.4E-16) kg

Results with 7310:
3 kg
4 kg


Rod Cope - 28/Jul/08 03:37 PM
Here's the link to the binary download that actually contains jscience.jar: http://jscience.org/jscience-4.3.1-bin.zip

Charles Oliver Nutter - 25/Aug/08 05:55 PM
Marking as blocker. 1.1.4 this week needs this fix.

Charles Oliver Nutter - 26/Aug/08 06:47 PM
I just committed a fix and Tom's working on a spec. Your revised case, with JScience, now works correctly. It's a fairly primitive fix for now, scoring methods both on whether the arguments are assignable AND an "exactness count" but it should solve the majority of cases. Post 1.1.4 we'll look at getting a more complete JLS-standard scoring mechanism in place, either by repurposing code from Dynalang or by examining what Groovy, Rhino, et al do. Please let us know ASAP if you see any additional issues.

Specs coming in a moment.


Rod Cope - 27/Aug/08 04:53 PM
I just updated to 7560 and I still have the issue with my test case above.

Rod Cope - 27/Aug/08 05:00 PM
I just updated to 7560 and my jscience test case still fails the same way it did before.

Charles Oliver Nutter - 30/Aug/08 03:01 AM
Blast it all, I know it was fixed. I wonder if the commit didn't get pushed correctly. I'll investigate.

Charles Oliver Nutter - 30/Aug/08 03:33 AM
Argh. There were two problems, and I'm not sure whether they were introduced after my change or came along with it.

First off, a different overloaded version of the method selection logic is getting called for your test. I think we wandered away from your test into a hand-written specification of our own that didn't follow the same path.

Second, it seems a ! snuck into the exactness scoring logic and somehow flipped the logic so it's picking the LEAST exact method. I have no frigging clue how this happened.

I've fixed the overload (deleted), removed the cursed ! and this is my result running your script now:

~/NetBeansProjects/jruby ➔ cat jscience_test.rb 
include Java
require 'jscience.jar'
Amount = org.jscience.physics.amount.Amount
Unit = javax.measure.unit.Unit

class Numeric
def method_missing(sym)
Amount.valueOf(self, Unit.valueOf(sym.to_s))
end
end

puts 3.kg
puts 4.5.kg
~/NetBeansProjects/jruby ➔ jruby -I jscience-4.3/ jscience_test.rb 
3 kg
(4.5 ± 4.4E-16) kg

I'm going to get this pushed out and I'll talk to Tom tomorrow about how to deal with it. I think this is responsible for one other regression report in JRuby too.


Charles Oliver Nutter - 30/Aug/08 04:16 AM
I have pushed the change along with a substantial expansion of the specs that accompanied it in r7586. I don't think we're going to be able to re-spin 1.1.4 but this is strong motivation to spin a 1.1.5 before month's end.