From d8fc5c127a0f95540affd7bed7956a8113c768f6 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Thu, 24 Apr 2003 12:08:23 +0000 Subject: [PATCH] Resolving http://bugzilla.mozilla.org/show_bug.cgi?id=202872 : public static Method[] findMethods(Class clazz, String name) is replaced by static Method findSingleMethod(Method[] methods, String name) --- .../mozilla/javascript/FunctionObject.java | 64 ++++--------------- .../mozilla/javascript/LazilyLoadedCtor.java | 6 +- .../mozilla/javascript/NativeJavaPackage.java | 8 ++- .../mozilla/javascript/ScriptableObject.java | 59 +++++++---------- .../javascript/resources/Messages.properties | 3 - 5 files changed, 44 insertions(+), 96 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/FunctionObject.java b/js/rhino/src/org/mozilla/javascript/FunctionObject.java index 1c392062dbe1..95d8526b76f3 100644 --- a/js/rhino/src/org/mozilla/javascript/FunctionObject.java +++ b/js/rhino/src/org/mozilla/javascript/FunctionObject.java @@ -219,59 +219,24 @@ public class FunctionObject extends BaseFunction { return getArity(); } - // TODO: Make not public - /** - * Finds methods of a given name in a given class. - * - *

Searches clazz for methods with name - * name. Maintains a cache so that multiple - * lookups on the same class are cheap. - * - * @param clazz the class to search - * @param name the name of the methods to find - * @return an array of the found methods, or null if no methods - * by that name were found. - * @see java.lang.Class#getMethods - */ - public static Method[] findMethods(Class clazz, String name) { - return findMethods(getMethodList(clazz), name); - } - - static Method[] findMethods(Method[] methods, String name) { - // Usually we're just looking for a single method, so optimize - // for that case. - ObjArray v = null; - Method first = null; - for (int i=0; i < methods.length; i++) { - if (methods[i] == null) - continue; - if (methods[i].getName().equals(name)) { - if (first == null) { - first = methods[i]; - } else { - if (v == null) { - v = new ObjArray(5); - v.add(first); - } - v.add(methods[i]); + static Method findSingleMethod(Method[] methods, String name) + { + Method found = null; + for (int i = 0, N = methods.length; i != N; ++i) { + Method method = methods[i]; + if (method != null && name.equals(method.getName())) { + if (found != null) { + throw Context.reportRuntimeError2( + "msg.no.overload", name, + method.getDeclaringClass().getName()); } + found = method; } } - if (v == null) { - if (first == null) - return null; - Method[] single = { first }; - return single; - } - Method[] result = new Method[v.size()]; - v.toArray(result); - return result; + return found; } static Method[] getMethodList(Class clazz) { - Method[] cached = methodsCache; // get once to avoid synchronization - if (cached != null && cached[0].getDeclaringClass() == clazz) - return cached; Method[] methods = null; try { // getDeclaredMethods may be rejected by the security manager @@ -302,8 +267,6 @@ public class FunctionObject extends BaseFunction { if (methods[i] != null) result[j++] = methods[i]; } - if (result.length > 0 && Context.isCachingEnabled) - methodsCache = result; return result; } @@ -560,7 +523,6 @@ public class FunctionObject extends BaseFunction { static void setCachingEnabled(boolean enabled) { if (!enabled) { - methodsCache = null; invokerMaster = null; } else if (invokerMaster == null) { invokerMaster = newInvokerMaster(); @@ -717,8 +679,6 @@ public class FunctionObject extends BaseFunction { private static boolean sawSecurityException; - static Method[] methodsCache; - transient Method method; transient Constructor ctor; transient Invoker invoker; diff --git a/js/rhino/src/org/mozilla/javascript/LazilyLoadedCtor.java b/js/rhino/src/org/mozilla/javascript/LazilyLoadedCtor.java index a10bd4d08e7b..121c9d411d68 100644 --- a/js/rhino/src/org/mozilla/javascript/LazilyLoadedCtor.java +++ b/js/rhino/src/org/mozilla/javascript/LazilyLoadedCtor.java @@ -53,9 +53,9 @@ public final class LazilyLoadedCtor { this.sealed = sealed; if (getter == null) { - Method[] all = FunctionObject.getMethodList(getClass()); - getter = FunctionObject.findMethods(all, "getProperty")[0]; - setter = FunctionObject.findMethods(all, "setProperty")[0]; + Method[] methods = FunctionObject.getMethodList(getClass()); + getter = FunctionObject.findSingleMethod(methods, "getProperty"); + setter = FunctionObject.findSingleMethod(methods, "setProperty"); } try { diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java index 17fb6b7dcb4b..19bc1047903b 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaPackage.java @@ -131,9 +131,11 @@ public class NativeJavaPackage extends ScriptableObject { nameStart = nameEnd + 1; } - Method[] m = FunctionObject.findMethods(NativeJavaPackage.class, - "jsFunction_getClass"); - FunctionObject f = new FunctionObject("getClass", m[0], scope); + Method[] methods = FunctionObject.getMethodList( + NativeJavaPackage.class); + Method m = FunctionObject.findSingleMethod(methods, + "jsFunction_getClass"); + FunctionObject f = new FunctionObject("getClass", m, scope); // It's safe to downcast here since initStandardObjects takes // a ScriptableObject. diff --git a/js/rhino/src/org/mozilla/javascript/ScriptableObject.java b/js/rhino/src/org/mozilla/javascript/ScriptableObject.java index a88b5295daa9..01336d5bfbce 100644 --- a/js/rhino/src/org/mozilla/javascript/ScriptableObject.java +++ b/js/rhino/src/org/mozilla/javascript/ScriptableObject.java @@ -839,16 +839,7 @@ public abstract class ScriptableObject implements Scriptable, Serializable, final String setterPrefix = "jsSet_"; final String ctorName = "jsConstructor"; - Method[] ctorMeths = FunctionObject.findMethods(clazz, ctorName); - Member ctorMember = null; - if (ctorMeths != null) { - if (ctorMeths.length > 1) { - throw new ClassDefinitionException( - Context.getMessage2("msg.multiple.ctors", - ctorMeths[0], ctorMeths[1])); - } - ctorMember = ctorMeths[0]; - } + Member ctorMember = FunctionObject.findSingleMethod(methods, ctorName); if (ctorMember == null) { if (ctors.length == 1) { @@ -921,20 +912,15 @@ public abstract class ScriptableObject implements Scriptable, Serializable, throw PropertyException.withMessage2 ("msg.extend.scriptable", proto.getClass().toString(), name); } - Method[] setter = FunctionObject.findMethods( - clazz, + Method setter = FunctionObject.findSingleMethod( + methods, setterPrefix + name); - if (setter != null && setter.length != 1) { - throw PropertyException.withMessage2 - ("msg.no.overload", name, clazz.getName()); - } int attr = ScriptableObject.PERMANENT | ScriptableObject.DONTENUM | (setter != null ? 0 : ScriptableObject.READONLY); - Method m = setter == null ? null : setter[0]; ((ScriptableObject) proto).defineProperty(name, null, - methods[i], m, + methods[i], setter, attr); continue; } @@ -1037,19 +1023,25 @@ public abstract class ScriptableObject implements Scriptable, Serializable, int attributes) throws PropertyException { - StringBuffer buf = new StringBuffer(propertyName); - buf.setCharAt(0, Character.toUpperCase(propertyName.charAt(0))); - String s = buf.toString(); - Method[] getter = FunctionObject.findMethods(clazz, "get" + s); - Method[] setter = FunctionObject.findMethods(clazz, "set" + s); + int length = propertyName.length(); + if (length == 0) throw new IllegalArgumentException(); + char[] buf = new char[3 + length]; + propertyName.getChars(0, length, buf, 3); + buf[3] = Character.toUpperCase(buf[3]); + buf[0] = 'g'; + buf[1] = 'e'; + buf[2] = 't'; + String getterName = new String(buf); + buf[0] = 's'; + String setterName = new String(buf); + + Method[] methods = FunctionObject.getMethodList(clazz); + Method getter = FunctionObject.findSingleMethod(methods, getterName); + Method setter = FunctionObject.findSingleMethod(methods, setterName); if (setter == null) attributes |= ScriptableObject.READONLY; - if (getter.length != 1 || (setter != null && setter.length != 1)) { - throw PropertyException.withMessage2 - ("msg.no.overload", propertyName, clazz.getName()); - } - defineProperty(propertyName, null, getter[0], - setter == null ? null : setter[0], attributes); + defineProperty(propertyName, null, getter, + setter == null ? null : setter, attributes); } /** @@ -1176,18 +1168,15 @@ public abstract class ScriptableObject implements Scriptable, Serializable, int attributes) throws PropertyException { + Method[] methods = FunctionObject.getMethodList(clazz); for (int i=0; i < names.length; i++) { String name = names[i]; - Method[] m = FunctionObject.findMethods(clazz, name); + Method m = FunctionObject.findSingleMethod(methods, name); if (m == null) { throw PropertyException.withMessage2 ("msg.method.not.found", name, clazz.getName()); } - if (m.length > 1) { - throw PropertyException.withMessage2 - ("msg.no.overload", name, clazz.getName()); - } - FunctionObject f = new FunctionObject(name, m[0], this); + FunctionObject f = new FunctionObject(name, m, this); defineProperty(name, f, attributes); } } diff --git a/js/rhino/src/org/mozilla/javascript/resources/Messages.properties b/js/rhino/src/org/mozilla/javascript/resources/Messages.properties index 458a3e9e2061..caa3bd5178ba 100644 --- a/js/rhino/src/org/mozilla/javascript/resources/Messages.properties +++ b/js/rhino/src/org/mozilla/javascript/resources/Messages.properties @@ -400,9 +400,6 @@ msg.default.value =\ msg.zero.arg.ctor =\ Cannot load class "{0}" which has no zero-parameter constructor. -msg.multiple.ctors =\ - Cannot have more than one constructor method, but found both {0} and {1}. - msg.ctor.multiple.parms =\ Can''t define constructor or class {0} since more than one \ constructor has multiple parameters.