Skip to content

Commit

Permalink
Missed tests from last commit.
Browse files Browse the repository at this point in the history
Make java proxies forward some method requests to the JavaObject they wrap (patch by David Corbin dcorbin@machturtle.com)


git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@1451 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
enebo committed Dec 5, 2004
1 parent 80555c9 commit 14d1255
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
32 changes: 21 additions & 11 deletions src/builtin/javasupport.rb
@@ -1,6 +1,7 @@
#
# Copyright (C) 2002 Anders Bengtsson <ndrsbngtssn@yahoo.se>
# Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
# Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
#
# JRuby - http://jruby.sourceforge.net
#
Expand All @@ -27,6 +28,26 @@ module JavaProxy
attr :java_class, true
attr :java_object, true

def ==(rhs)
return @java_object == rhs
end

def to_s
@java_object.to_s
end

def eql?(rhs)
self == rhs
end

def equal?(rhs)
@java_object.equal?(rhs)
end

def hash()
@java_object.hash()
end

def JavaProxy.convert_arguments(arguments)
arguments.collect {|v|
if v.kind_of?(JavaProxy)
Expand Down Expand Up @@ -292,17 +313,6 @@ def proxy_class.create_instance_methods(java_class)
end
end
end

# All ruby objects allow to_s. Make sure our proxy maps to the
# Java objects toString method.
define_method('to_s') do |*args|
method = java_class.java_method('toString')
result = Java.java_to_primitive(method.invoke(java_object))
if result.kind_of?(JavaObject)
result = JavaUtilities.wrap(result, method.return_type)
end
result
end
end
proxy_class.create_instance_methods(java_class)
end
Expand Down
39 changes: 25 additions & 14 deletions src/org/jruby/javasupport/JavaObject.java
Expand Up @@ -111,8 +111,7 @@ public static RubyClass createJavaObjectClass(Ruby runtime) {
}

public RubyFixnum hash() {
return RubyFixnum.newFixnum(runtime,
value == null ? 0 : value.hashCode());
return RubyFixnum.newFixnum(runtime, value == null ? 0 : value.hashCode());
}

public RubyString to_s() {
Expand All @@ -121,23 +120,35 @@ public RubyString to_s() {
}

public IRubyObject equal(IRubyObject other) {
if (!(other instanceof JavaObject)) {
return getRuntime().getFalse();
}
if (!(other instanceof JavaObject)) {
other = other.getInstanceVariable("@java_object");
if (!(other instanceof JavaObject)) {
return getRuntime().getFalse();
}
}

if (getValue() == null && ((JavaObject) other).getValue() == null) {
return getRuntime().getTrue();
}
if (getValue() == null && ((JavaObject) other).getValue() == null) {
return getRuntime().getTrue();
}

return (getValue().equals(((JavaObject) other).getValue())) ? getRuntime().getTrue() : getRuntime().getFalse();
boolean isEqual = getValue().equals(((JavaObject) other).getValue());
return isEqual ? getRuntime().getTrue() : getRuntime().getFalse();
}

public IRubyObject same(IRubyObject other) {
if (other instanceof JavaObject &&
value == ((JavaObject) other).value) {
return getRuntime().getTrue();
}
return getRuntime().getFalse();
if (!(other instanceof JavaObject)) {
other = other.getInstanceVariable("@java_object");
if (!(other instanceof JavaObject)) {
return getRuntime().getFalse();
}
}

if (getValue() == null && ((JavaObject) other).getValue() == null) {
return getRuntime().getTrue();
}

boolean isSame = getValue() == ((JavaObject) other).getValue();
return isSame ? getRuntime().getTrue() : getRuntime().getFalse();
}

public RubyString java_type() {
Expand Down

0 comments on commit 14d1255

Please sign in to comment.