Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.1.3
-
Fix Version/s: JRuby 1.2
-
Component/s: Core Classes/Modules
-
Labels:None
-
Environment:Demonstrated on Windows, with jruby 1.1.3 (ruby 1.8.6 patchlevel 114) (2008-07-20 rev 7243) [x86-java]
Description
A subclass method that overrides a superclass method is inconsistently called. In the code sample below, JRuby does not call the subclass's meth1, but does call the subclass's meth2. The difference is whether or not the superclass's meth1 was previously called by another method (calling_meth1). MRI Ruby always calls the subclass's method.
A consequence of this bug is that Mocha 0.9.0 works inconsistently with JRuby. Mocha 0.5.6, unlike 0.9.0, undefines the superclass's method, so it works OK. But 0.9.0 mocked methods will not be called if the unmocked method has already been called, even if that was in a different test case. This means the order in which test cases run changes the results of the tests.
def calling_meth1
MySub.meth1
end
def calling_meth2
MySub.meth2
end
class MySuper
def self.meth1
puts 'MySuper::meth1'
end
def self.meth2
puts 'MySuper::meth2'
end
end
class MySub < MySuper
end
puts "# of meth1s = #{MySub.methods.select {|m| m == 'meth1'}.size }"
puts "# of meth2s = #{MySub.methods.select {|m| m == 'meth2'}.size }"
puts 'The following lines should call MySuper methods'
MySub::meth1
calling_meth1
MySub::meth2
puts 'Note: calling_meth2 is not called here'
class MySub
def self.meth1
puts 'MySub::meth1'
end
def self.meth2
puts 'MySub::meth2'
end
end
puts "# of meth1s = #{MySub.methods.select {|m| m == 'meth1'}.size }" # JRuby prints 2, but MRI Ruby prints 1
puts "# of meth2s = #{MySub.methods.select {|m| m == 'meth2'}.size }"
puts 'The following lines should call MySub methods'
MySub::meth1
calling_meth1 # JRuby prints MySuper::meth1 here, but MRI Ruby prints MySub::meth1
MySub::meth2
calling_meth2
We fixed this some time ago by reworking our call site caching to invalidate based on changes anywhere in the hierarchy. But I also fixed the duplicate methods issue and formed your script into a test case. All committed in r9068.