public static Method[] findMethods(Class clazz, String name)

is replaced by

static Method findSingleMethod(Method[] methods, String name)
This commit is contained in:
igor%mir2.org 2003-04-24 12:08:23 +00:00
parent 7cd59bb5f1
commit d8fc5c127a
5 changed files with 44 additions and 96 deletions

View File

@ -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.
*
* <p>Searches <code>clazz</code> for methods with name
* <code>name</code>. 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;

View File

@ -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 {

View File

@ -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.

View File

@ -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);
}
}

View File

@ -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.