From: <cg...@us...> - 2008-10-26 17:35:15
|
Revision: 5511 http://jython.svn.sourceforge.net/jython/?rev=5511&view=rev Author: cgroves Date: 2008-10-26 17:35:12 +0000 (Sun, 26 Oct 2008) Log Message: ----------- Document magic values n ReflectedCallData Modified Paths: -------------- trunk/jython/src/org/python/core/PyReflectedConstructor.java trunk/jython/src/org/python/core/PyReflectedFunction.java trunk/jython/src/org/python/core/ReflectedArgs.java trunk/jython/src/org/python/core/ReflectedCallData.java Modified: trunk/jython/src/org/python/core/PyReflectedConstructor.java =================================================================== --- trunk/jython/src/org/python/core/PyReflectedConstructor.java 2008-10-26 14:05:35 UTC (rev 5510) +++ trunk/jython/src/org/python/core/PyReflectedConstructor.java 2008-10-26 17:35:12 UTC (rev 5511) @@ -9,7 +9,7 @@ public class PyReflectedConstructor extends PyReflectedFunction { - + public PyReflectedConstructor(String name) { super(name); __name__ = name; @@ -38,16 +38,15 @@ // xxx temporary solution, type ctr will go through __new__ ... PyObject make(PyObject[] args,String[] keywords) { ReflectedArgs[] argsl = argslist; - + ReflectedCallData callData = new ReflectedCallData(); Object method=null; boolean consumes_keywords = false; - int n = nargs; int nkeywords = keywords.length; PyObject[] allArgs = null; - + // Check for a matching constructor to call - if (n > 0) { // PyArgsKeywordsCall signature, if present, is the first + if (nargs > 0) { // PyArgsKeywordsCall signature, if present, is the first if (argsl[0].matches(null, args, keywords, callData)) { method = argsl[0].data; consumes_keywords = argsl[0].flags == ReflectedArgs.PyArgsKeywordsCall; @@ -55,15 +54,16 @@ allArgs = args; int i = 1; if (nkeywords > 0) { - args = new PyObject[allArgs.length-nkeywords]; + args = new PyObject[allArgs.length - nkeywords]; System.arraycopy(allArgs, 0, args, 0, args.length); i = 0; } - for (; i < n; i++) { + for (; i < nargs; i++) { ReflectedArgs rargs = argsl[i]; - if (rargs - .matches(null, args, Py.NoKeywords, callData)) { - method = rargs.data; break; } + if (rargs.matches(null, args, Py.NoKeywords, callData)) { + method = rargs.data; + break; + } } } } @@ -82,14 +82,14 @@ catch (Throwable t) { throw Py.JavaError(t); } - + if (!consumes_keywords) { int offset = args.length; - for (int i=0; i<nkeywords; i++) { - obj.__setattr__(keywords[i], allArgs[i+offset]); - } + for (int i = 0; i < nkeywords; i++) { + obj.__setattr__(keywords[i], allArgs[i + offset]); + } } - + return obj; } Modified: trunk/jython/src/org/python/core/PyReflectedFunction.java =================================================================== --- trunk/jython/src/org/python/core/PyReflectedFunction.java 2008-10-26 14:05:35 UTC (rev 5510) +++ trunk/jython/src/org/python/core/PyReflectedFunction.java 2008-10-26 17:35:12 UTC (rev 5511) @@ -3,10 +3,11 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.Enumeration; -import java.util.Hashtable; +import java.util.Set; +import org.python.util.Generic; + public class PyReflectedFunction extends PyObject { public String __name__; @@ -136,11 +137,11 @@ } Object cself = callData.self; - Method m = (Method)method; + Method m = (Method)method; // Check to see if we should be using a super__ method instead // This is probably a bit inefficient... if (self == null && cself != null && cself instanceof PyProxy && - !__name__.startsWith("super__")) { + !__name__.startsWith("super__")) { PyInstance iself = ((PyProxy)cself)._getPyInstance(); if (argslist[0].declaringClass != iself.instclass.proxyClass) { String mname = ("super__"+__name__); @@ -148,9 +149,9 @@ Method[] super__methods = (Method[])iself.instclass.super__methods.get(mname); if (super__methods != null) { Class[] msig = m.getParameterTypes(); - for (int i=0; i<super__methods.length;i++) { - if (java.util.Arrays.equals(msig,super__methods[i].getParameterTypes())) { - m = super__methods[i]; + for (Method super__method : super__methods) { + if (java.util.Arrays.equals(msig,super__method.getParameterTypes())) { + m = super__method; break; } } @@ -264,64 +265,40 @@ } protected void throwBadArgError(int errArg, int nArgs, boolean self) { - Hashtable table = new Hashtable(); - ReflectedArgs[] argsl = argslist; - int n = nargs; - for(int i=0; i<n; i++) { - ReflectedArgs rargs = argsl[i]; - Class[] args = rargs.args; - int len = args.length; - /*if (!args.isStatic && !self) { - len = len-1; - }*/ + Set<Class<?>> argTypes = Generic.set(); + for (int i = 0; i < nargs; i++) { + // if (!args.isStatic && !self) { len = len-1; } // This check works almost all the time. // I'm still a little worried about non-static methods // called with an explict self... - if (len == nArgs) { - if (errArg == -1) { - table.put(rargs.declaringClass, rargs.declaringClass); + if (argslist[i].args.length == nArgs) { + if (errArg == ReflectedCallData.UNABLE_TO_CONVERT_SELF) { + argTypes.add(argslist[i].declaringClass); } else { - table.put(args[errArg], args[errArg]); + argTypes.add(argslist[i].args[errArg]); } } } - - StringBuffer buf = new StringBuffer(); - Enumeration keys = table.keys(); - while (keys.hasMoreElements()) { - Class arg = (Class)keys.nextElement(); - String name = niceName(arg); - if (keys.hasMoreElements()) { - buf.append(name); - buf.append(", "); - } else { - if (buf.length() > 2) { - buf.setLength(buf.length()-2); - buf.append(" or "); - } - buf.append(name); - } + StringBuilder buf = new StringBuilder(); + for (Class<?> arg : argTypes) { + buf.append(niceName(arg)); + buf.append(", "); } + if(buf.length() > 2) { + buf.setLength(buf.length() - 2); + } - throwError(ordinal(errArg)+" arg can't be coerced to "+buf); + throwError(ordinal(errArg) + " arg can't be coerced to " + buf); } - protected void throwError(int errArg, int nArgs, boolean self, - boolean keywords) - { - if (keywords) throwError("takes no keyword arguments"); - - if (errArg == -2) { + protected void throwError(int errArg, int nArgs, boolean self, boolean keywords) { + if (keywords) { + throwError("takes no keyword arguments"); + } else if (errArg == ReflectedCallData.BAD_ARG_COUNT) { throwArgCountError(nArgs, self); + } else { + throwBadArgError(errArg, nArgs, self); } - - /*if (errArg == -1) { - throwBadArgError(-1); - throwError("bad self argument"); - // Bad declared class - }*/ - - throwBadArgError(errArg, nArgs, self); } // Included only for debugging purposes... Modified: trunk/jython/src/org/python/core/ReflectedArgs.java =================================================================== --- trunk/jython/src/org/python/core/ReflectedArgs.java 2008-10-26 14:05:35 UTC (rev 5510) +++ trunk/jython/src/org/python/core/ReflectedArgs.java 2008-10-26 17:35:12 UTC (rev 5511) @@ -101,7 +101,7 @@ } // Make error messages clearer - callData.errArg = -1; + callData.errArg = ReflectedCallData.UNABLE_TO_CONVERT_SELF; if (self != null) { Object tmp = self.__tojava__(this.declaringClass); @@ -199,7 +199,7 @@ return cmp > 0 ? +1 : -1; } } - } + } return p1 > p2 ? +2 : (p1 == p2 ? 0 : -2); } @@ -261,8 +261,8 @@ String s = "" + this.declaringClass + ", " + this.isStatic + ", " + this.flags + ", " + this.data + "\n"; s = s + "\t("; - for (int j = 0; j < this.args.length; j++) { - s += this.args[j].getName() + ", "; + for (Class arg : this.args) { + s += arg.getName() + ", "; } s += ")"; return s; Modified: trunk/jython/src/org/python/core/ReflectedCallData.java =================================================================== --- trunk/jython/src/org/python/core/ReflectedCallData.java 2008-10-26 14:05:35 UTC (rev 5510) +++ trunk/jython/src/org/python/core/ReflectedCallData.java 2008-10-26 17:35:12 UTC (rev 5511) @@ -2,21 +2,25 @@ package org.python.core; class ReflectedCallData { - public Object[] args; + /** errArg value if the self passed to a call wasn't compatible with the type of the method. */ + public static final int UNABLE_TO_CONVERT_SELF = -1; + + /** errArg value if the method has no forms that take the number of arguments given. */ + public static final int BAD_ARG_COUNT = -2; + + public Object[] args = Py.EmptyObjects; + public int length; public Object self; - public int errArg; + /** + * Either {@link #BAD_ARG_COUNT}, {@link #UNABLE_TO_CONVERT_SELF}, or the index of the + * unconvertible argument in args. + */ + public int errArg = BAD_ARG_COUNT; - public ReflectedCallData() { - this.args = Py.EmptyObjects; - this.length = 0; - this.self = null; - this.errArg = -2; - } - public void setLength(int newLength) { this.length = newLength; if (newLength <= this.args.length) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |