pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/6627d62d038fed8a9a7e927f85eb4ddee3c90ff1

mer-b48faa60c69660fa.css" /> [ji] dispatch equals/hashCode/toString to Ruby impl if defined (usePr… · jruby/jruby@6627d62 · GitHub
Skip to content

Commit 6627d62

Browse files
committed
[ji] dispatch equals/hashCode/toString to Ruby impl if defined (useProxy=true version)
1 parent c0c0e86 commit 6627d62

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

core/src/main/java/org/jruby/javasupport/Java.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,13 +1271,17 @@ public Object invoke(Object proxy, Method method, Object[] nargs) throws Throwab
12711271

12721272
switch ( methodName ) {
12731273
case "toString" :
1274-
if ( length == 0 ) return proxyToString(proxy);
1274+
if ( length == 0 && ! wrapper.respondsTo("toString") ) {
1275+
return proxyToString(proxy);
1276+
}
12751277
break;
12761278
case "hashCode" :
1277-
if ( length == 0 ) return proxyHashCode(proxy);
1279+
if ( length == 0 && ! wrapper.respondsTo("hashCode") ) {
1280+
return proxyHashCode(proxy);
1281+
}
12781282
break;
12791283
case "equals" :
1280-
if ( length == 1 ) {
1284+
if ( length == 1 && ! wrapper.respondsTo("equals") ) {
12811285
Class[] parameterTypes = getParameterTypes(method);
12821286
if ( parameterTypes[0] == Object.class ) return proxyEquals(proxy, nargs[0]);
12831287
}

spec/java_integration/interfaces/implementation_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,39 @@ def call_it; 8 end
134134
expect( str ).to match(/\:0x[0-9a-f]+>$/)
135135
end
136136

137+
it "should use Ruby defined equals/hashCode/toString impls" do
138+
klass = Class.new do
139+
include java.util.Map
140+
141+
attr_reader :val
142+
def initialize(val); @val = val end
143+
144+
def equals(obj); obj.respond_to?(:val) ? val == obj.val : false end
145+
def hashCode; val == 'a' ? 42 : val.hash end
146+
147+
def toString; val == 'a' ? raise(NotImplementedError.new('a')) : val end
148+
end
149+
val_a = klass.new 'a'
150+
val_b = klass.new 'b'
151+
152+
eq = UsesSingleMethodInterface.equals(val_a, val_a)
153+
expect(eq).to be true
154+
eq = UsesSingleMethodInterface.equals(val_a, nil)
155+
expect(eq).to be false
156+
eq = UsesSingleMethodInterface.equals(val_a, val_b)
157+
expect(eq).to be false
158+
eq = UsesSingleMethodInterface.equals(val_a, klass.new(:'a'.to_s))
159+
expect(eq).to be true
160+
161+
hash = UsesSingleMethodInterface.hashCode(val_a)
162+
expect( hash ).to eql 42
163+
164+
str = UsesSingleMethodInterface.toString(val_b)
165+
expect( str ).to eql 'b'
166+
167+
expect { UsesSingleMethodInterface.toString(val_a) }.to raise_error(NotImplementedError)
168+
end
169+
137170
it "should allow including the same interface twice" do
138171
c = Class.new do
139172
include SingleMethodInterface

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy