From d6d018f0b643f80173cab56b4f2bfbd1c9fd4dbb Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Fri, 18 Feb 2005 23:06:39 +0000 Subject: [PATCH] Resolving bug 282595: patch from Juerg lehni to allow for several setters for bean properties. --- .../org/mozilla/javascript/JavaMembers.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/JavaMembers.java b/js/rhino/src/org/mozilla/javascript/JavaMembers.java index a4ee812c7ff..098e36ea238 100644 --- a/js/rhino/src/org/mozilla/javascript/JavaMembers.java +++ b/js/rhino/src/org/mozilla/javascript/JavaMembers.java @@ -132,12 +132,22 @@ class JavaMembers if (bp.setter == null) { throw reportMemberNotFound(name); } - Class setType = bp.setter.argTypes[0]; - Object[] args = { Context.jsToJava(value, setType) }; - try { - bp.setter.invoke(javaObject, args); - } catch (Exception ex) { - throw Context.throwAsScriptRuntimeEx(ex); + // If there's only one setter or if the value is null, use the + // main setter. Otherwise, let the NativeJavaMethod decide which + // setter to use: + if (bp.setters == null || value == null) { + Class setType = bp.setter.argTypes[0]; + Object[] args = { Context.jsToJava(value, setType) }; + try { + bp.setter.invoke(javaObject, args); + } catch (Exception ex) { + throw Context.throwAsScriptRuntimeEx(ex); + } + } else { + Object[] args = { value }; + bp.setters.call(Context.getContext(), + ScriptableObject.getTopLevelScope(scope), + scope, args); } } else { @@ -460,6 +470,7 @@ class JavaMembers // We have a getter. Now, do we have a setter? NativeJavaMethod njmSet = null; MemberBox setter = null; + NativeJavaMethod setters = null; String setterName = "set".concat(nameComponent); if (ht.containsKey(setterName)) { // Is this value a method? @@ -469,10 +480,14 @@ class JavaMembers Class type = getter.method().getReturnType(); setter = extractSetMethod(type, njmSet.methods, isStatic); + if (njmSet.methods.length > 1) { + setters = njmSet; + } } } // Make the property. - BeanProperty bp = new BeanProperty(getter, setter); + BeanProperty bp = new BeanProperty(getter, setter, + setters); toAdd.put(beanPropertyName, bp); } } @@ -631,14 +646,16 @@ class JavaMembers class BeanProperty { - BeanProperty(MemberBox getter, MemberBox setter) + BeanProperty(MemberBox getter, MemberBox setter, NativeJavaMethod setters) { this.getter = getter; this.setter = setter; + this.setters = setters; } MemberBox getter; MemberBox setter; + NativeJavaMethod setters; } class FieldAndMethods extends NativeJavaMethod