Skip to content

Commit

Permalink
Allow JavaBeans properties to be accessed like ruby attributes.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@1452 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
enebo committed Dec 5, 2004
1 parent 14d1255 commit 5f1f814
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 21 deletions.
54 changes: 33 additions & 21 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 Thomas E Enebo <enebo@acm.org>
# Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
#
# JRuby - http://jruby.sourceforge.net
Expand Down Expand Up @@ -278,41 +279,52 @@ def each()

def setup_instance_methods(java_class, proxy_class)
def proxy_class.create_instance_methods(java_class)
public_methods =
java_class.java_instance_methods.select {|m| m.public? }
grouped_methods = public_methods.group_by {|m| m.name }
grouped_methods.each do |name, methods|
# Only one method by this name
if methods.length == 1
m = methods.first
return_type = m.return_type
define_method(m.name) do |*args|
args = JavaProxy.convert_arguments(args)
if m.arity != args.length
raise ArgumentError.new("wrong # of arguments(#{args.length} for #{m.arity})")
end
java_class.java_instance_methods.select { |m|
m.public?
}.group_by { |m|
m.name
}.each { |name, methods|
# Define literal java method (e.g. getFoo())
define_method(name) do |*args|
args = convert_arguments(args)
m = JavaUtilities.matching_method(methods.find_all {|m|
m.arity == args.length}, args)
result = m.invoke(self.java_object, *args)
result = Java.java_to_primitive(result)
if result.kind_of?(JavaObject)
result = JavaUtilities.wrap(result, m.return_type)
end
result
end

# Lets treat javabean properties like ruby attributes
case name[0,4]
when /get./
define_method(name[3..-1].downcase!) do |*args|
args = convert_arguments(args)
m = JavaUtilities.matching_method(methods.find_all {|m|
m.arity == args.length}, args)
result = m.invoke(self.java_object, *args)
result = Java.java_to_primitive(result)
if result.kind_of?(JavaObject)
result = JavaUtilities.wrap(result, m.return_type)
end
result
end
else
define_method(name) do |*args|
end
when /set./
define_method(name[3..-1].downcase!.concat('=')) do |*args|
args = convert_arguments(args)
m = JavaUtilities.matching_method(methods.find_all {|m|
m.arity == args.length
}, args)
m.arity == args.length}, args)
result = m.invoke(self.java_object, *args)
result = Java.java_to_primitive(result)
if result.kind_of?(JavaObject)
result = JavaUtilities.wrap(result, m.return_type)
end
result
end
end
end
end
end
}
end
proxy_class.create_instance_methods(java_class)
end
Expand Down
17 changes: 17 additions & 0 deletions test/org/jruby/javasupport/test/Color.java
@@ -0,0 +1,17 @@
package org.jruby.javasupport.test;

public class Color {
private String color;

public Color(String color) {
this.color = color;
}

public void setColor(String color) {
this.color = color;
}

public String getColor() {
return color;
}
}
29 changes: 29 additions & 0 deletions test/org/jruby/javasupport/test/ConstantHolder.java
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
*
* JRuby - http://jruby.sourceforge.net
*
* This file is part of JRuby
*
* JRuby is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* JRuby is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with JRuby; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/

package org.jruby.javasupport.test;

public class ConstantHolder {

public static final String _LEADING_UNDERSCORE = "Foo";
}
25 changes: 25 additions & 0 deletions test/org/jruby/javasupport/test/Room.java
@@ -0,0 +1,25 @@
package org.jruby.javasupport.test;

public class Room {
private final String name;
private String owner = null;

public Room(String name) {
this.name = name;
}

public boolean equals(Object obj) {
if (! (obj instanceof Room))
return false;
Room that = (Room) obj;
return name.equals(that.name);
}

public String toString() {
return name;
}

public int hashCode() {
return name.hashCode();
}
}
13 changes: 13 additions & 0 deletions test/testHigherJavaSupport.rb
Expand Up @@ -65,9 +65,22 @@ module TestJavaSupport
test_equal(true, Boolean.valueOf("true"))
test_equal(false, Boolean.valueOf(false))

include_package 'org.jruby.javasupport.test'

# Java bean convention properties as attributes
color = Color.new("green")

test_ok(color.color == "green")

color.color = "blue"

test_ok(color.color == "blue")


# Constants
test_equal(9223372036854775807, Long::MAX_VALUE)
test_ok(! defined? Character::Y_DATA) # Known private field in Character
ConstantHolder # class definition with "_" constant causes error

# Using arrays
list = ArrayList.new
Expand Down
1 change: 1 addition & 0 deletions test/test_index
Expand Up @@ -41,6 +41,7 @@ testLoad.rb
testKernel.rb
testCase.rb
testLine.rb
testJavaProxy.rb

# MRI Ruby tests (from sample/test.rb in Matz's Ruby Interpreter):

Expand Down

0 comments on commit 5f1f814

Please sign in to comment.