From e9d879febe05cc3ca9ca563dd164b916c05906d8 Mon Sep 17 00:00:00 2001 From: enebo Date: Fri, 18 Jul 2008 02:03:19 +0000 Subject: [PATCH] JRUBY-1613: Tweak Java method aliasing in JavaClass to apply ? suffix to any method with boolean return type, loosen restriction on non-void setters git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@7221 961051c9-f516-0410-bf72-c9f7e237a7b7 --- spec/java_integration/fixtures/MethodNames.java | 3 ++ spec/java_integration/methods/naming_spec.rb | 51 ++++++++++++++----- src/org/jruby/javasupport/JavaClass.java | 68 ++++++++++++------------- 3 files changed, 73 insertions(+), 49 deletions(-) diff --git a/spec/java_integration/fixtures/MethodNames.java b/spec/java_integration/fixtures/MethodNames.java index f10be059c..5e3b8b065 100644 --- a/spec/java_integration/fixtures/MethodNames.java +++ b/spec/java_integration/fixtures/MethodNames.java @@ -28,6 +28,9 @@ public class MethodNames { public void setValue2(Object value) {} public void setValues2(Object value, Object otherValue) {} + public Object getJConsecutiveCaps() {return null;} + public void setJConsecutiveCaps(Object value) {} + public boolean isFirst2() {return false;} public boolean isSecond2(Object something) {return false;} public boolean hasThird2() {return false;} diff --git a/spec/java_integration/methods/naming_spec.rb b/spec/java_integration/methods/naming_spec.rb index 965d8decb..eb008e2a1 100644 --- a/spec/java_integration/methods/naming_spec.rb +++ b/spec/java_integration/methods/naming_spec.rb @@ -39,23 +39,30 @@ describe "Java static method names" do methods.should include("set_values1") methods.should_not include("values1=") end - + it "should present boolean javabean property accessors as '?' method" do methods = MethodNames.methods - pending("missing") do - methods.should include("isFirst2") - end - methods.should include("isSecond2") - methods.should include("first2?") - methods.should_not include("second2?") + methods.should include("isFirst1") + methods.should include("first1") + methods.should include("first1?") + + methods.should include("isSecond1") + methods.should include("second1") + methods.should include("second1?") - methods.should include("hasThird2") - methods.should include("hasFourth2") + methods.should include("hasThird1") + methods.should include("has_third1") + methods.should include("has_third1?") + + methods.should include("hasFourth1") + methods.should include("has_fourth1"); + methods.should include("has_fourth1?"); + pending("not implemented") do - methods.should include("third2?") + methods.should include("third1?") end - methods.should_not include("fourth2?") + methods.should_not include("fourth1?") end it "should not overwrite critical core Ruby methods" do @@ -101,16 +108,32 @@ describe "Java instance method names" do methods.should_not include("values2=") end + it "should treat consecutive caps as part of one property name" do + methods = MethodNames.instance_methods + + methods.should include("jconsecutive_caps") + methods.should include("jconsecutive_caps=") + end + it "should present boolean javabean property accessors as '?' method" do methods = MethodNames.instance_methods methods.should include("isFirst2") - methods.should include("isSecond2") + methods.should include("first2") methods.should include("first2?") - methods.should_not include("second2?") + + methods.should include("isSecond2") + methods.should include("second2") + methods.should include("second2?") methods.should include("hasThird2") + methods.should include("has_third2") + methods.should include("has_third2?") + methods.should include("hasFourth2") + methods.should include("has_fourth2") + methods.should include("has_fourth2?") + pending("not implemented") do methods.should include("third2?") end @@ -125,4 +148,4 @@ describe "Java instance method names" do obj.__id__.should_not == "foo" lambda {obj.__send__}.should raise_error(ArgumentError) end -end \ No newline at end of file +end diff --git a/src/org/jruby/javasupport/JavaClass.java b/src/org/jruby/javasupport/JavaClass.java index 7e0762770..1308f0873 100644 --- a/src/org/jruby/javasupport/JavaClass.java +++ b/src/org/jruby/javasupport/JavaClass.java @@ -736,49 +736,51 @@ public class JavaClass extends JavaObject { private static void assignAliases(MethodCallback callback, Map assignedNames) { String name = callback.name; - addUnassignedAlias(getRubyCasedName(name),assignedNames,callback); - // logic adapted from java.beans.Introspector - if (!(name.length() > 3 || name.startsWith("is"))) - return; + String rubyCasedName = getRubyCasedName(name); + addUnassignedAlias(rubyCasedName,assignedNames,callback); String javaPropertyName = getJavaPropertyName(name); - if (javaPropertyName == null) - return; // not a Java property name, done with this method + String rubyPropertyName = null; for (Method method: callback.methods) { Class[] argTypes = method.getParameterTypes(); Class resultType = method.getReturnType(); int argCount = argTypes.length; - if (argCount == 0) { - if (name.startsWith("get")) { - addUnassignedAlias(getRubyCasedName(name).substring(4),assignedNames,callback); - addUnassignedAlias(javaPropertyName,assignedNames,callback); - } else if (resultType == boolean.class && name.startsWith("is")) { - String rubyName = getRubyCasedName(name).substring(3); - if (rubyName != null) { - addUnassignedAlias(rubyName,assignedNames,callback); - addUnassignedAlias(rubyName+'?',assignedNames,callback); - } - if (!javaPropertyName.equals(rubyName)) { + + // Add property name aliases + if (javaPropertyName != null) { + if (rubyCasedName.startsWith("get_")) { + rubyPropertyName = rubyCasedName.substring(4); + if (argCount == 0 || // getFoo => foo + argCount == 1 && argTypes[0] == int.class) { // getFoo(int) => foo(int) + addUnassignedAlias(javaPropertyName,assignedNames,callback); - addUnassignedAlias(javaPropertyName+'?',assignedNames,callback); + addUnassignedAlias(rubyPropertyName,assignedNames,callback); } - } - } else if (argCount == 1) { - // indexed get - if (argTypes[0] == int.class && name.startsWith("get")) { - addUnassignedAlias(getRubyCasedName(name).substring(4),assignedNames,callback); - addUnassignedAlias(javaPropertyName,assignedNames,callback); - } else if (resultType == void.class && name.startsWith("set")) { - String rubyName = getRubyCasedName(name).substring(4); - if (rubyName != null) { - addUnassignedAlias(rubyName + '=',assignedNames,callback); + } else if (rubyCasedName.startsWith("set_")) { + rubyPropertyName = rubyCasedName.substring(4); + if (argCount == 1 && resultType == void.class) { // setFoo(Foo) => foo=(Foo) + addUnassignedAlias(javaPropertyName+'=',assignedNames,callback); + addUnassignedAlias(rubyPropertyName+'=',assignedNames,callback); } - if (!javaPropertyName.equals(rubyName)) { - addUnassignedAlias(javaPropertyName + '=',assignedNames,callback); + } else if (rubyCasedName.startsWith("is_")) { + rubyPropertyName = rubyCasedName.substring(3); + if (resultType == boolean.class) { // isFoo() => foo, isFoo(*) => foo(*) + addUnassignedAlias(javaPropertyName,assignedNames,callback); + addUnassignedAlias(rubyPropertyName,assignedNames,callback); } } } + + // Additionally add ?-postfixed aliases to any boolean methods and properties. + if (resultType == boolean.class) { + // is_something?, contains_thing? + addUnassignedAlias(rubyCasedName+'?',assignedNames,callback); + if (rubyPropertyName != null) { + // something? + addUnassignedAlias(rubyPropertyName+'?',assignedNames,callback); + } + } } } @@ -812,11 +814,7 @@ public class JavaClass extends JavaObject { private static final Pattern CAMEL_CASE_SPLITTER = Pattern.compile("([a-z][0-9]*)([A-Z])"); public static String getRubyCasedName(String javaCasedName) { Matcher m = CAMEL_CASE_SPLITTER.matcher(javaCasedName); - String rubyCasedName = m.replaceAll("$1_$2").toLowerCase(); - if (rubyCasedName.equals(javaCasedName)) { - return null; - } - return rubyCasedName; + return m.replaceAll("$1_$2").toLowerCase(); } -- 2.11.4.GIT