All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.python.core.PyReflectedFunction Maven / Gradle / Ivy

// Copyright (c) Corporation for National Research Initiatives
package org.python.core;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Enumeration;
import java.util.Hashtable;


public class PyReflectedFunction extends PyObject
{
    public String __name__;
    public PyObject __doc__ = Py.None;
    public ReflectedArgs[] argslist;
    public int nargs;

    public PyReflectedFunction(String name) {
        __name__ = name;
        argslist = new ReflectedArgs[1];
        nargs = 0;
    }

    public PyReflectedFunction(Method method) {
        this(method.getName());
        addMethod(method);
    }

    public PyObject _doget(PyObject container) {
        return _doget(container, null);
    }

    public PyObject _doget(PyObject container, PyObject wherefound) {
        if (container == null)
            return this;
        return new PyMethod(container, this, wherefound);
    }

    public boolean _doset(PyObject container) {
        throw Py.TypeError("java function not settable: "+__name__);
    }

    private ReflectedArgs makeArgs(Method m) {
        return new ReflectedArgs(m, m.getParameterTypes(),
                                 m.getDeclaringClass(),
                                 Modifier.isStatic(m.getModifiers()));
    }

    public PyReflectedFunction copy() {
        PyReflectedFunction func = new PyReflectedFunction(__name__);
        func.__doc__ = __doc__;
        func.nargs = nargs;
        func.argslist = new ReflectedArgs[nargs];
        System.arraycopy(argslist, 0, func.argslist, 0, nargs);
        return func;
    }

    public boolean handles(Method method) {
        return handles(makeArgs(method));
    }

    protected boolean handles(ReflectedArgs args) {
        ReflectedArgs[] argsl = argslist;
        int n = nargs;
        for(int i=0; i argsl.length) {
            argsl = new ReflectedArgs[nn+2];
            System.arraycopy(argslist, 0, argsl, 0, n);
            argslist = argsl;
        }

        for(int j=n; j>i; j--) {
            argsl[j] = argsl[j-1];
        }

        argsl[i] = args;
        nargs = nn;
    }

    public PyObject __call__(PyObject self, PyObject[] args,
                             String[] keywords)
    {
        ReflectedCallData callData = new ReflectedCallData();
        Object method = null;

        ReflectedArgs[] argsl = argslist;
        int n = nargs;
        for (int i=0; i 0) {
            buf.append(sep);
        }
        if (min < max) {
            buf.append(Integer.toString(min)+"-"+max);
        } else {
            buf.append(min);
        }
    }


    protected void throwArgCountError(int nArgs, boolean self) {
        // Assume no argument lengths greater than 40...
        boolean[] legalArgs = new boolean[40];
        int maxArgs = -1;
        int minArgs = 40;

        ReflectedArgs[] argsl = argslist;
        int n = nargs;
        for (int i=0; i maxArgs)
                maxArgs = l;
            if (l < minArgs)
                minArgs = l;
        }

        StringBuffer buf = new StringBuffer();

        int startRange = minArgs;
        int a = minArgs+1;
        while (a < maxArgs) {
            if (legalArgs[a]) {
                a++;
                continue;
            } else {
                addRange(buf, startRange, a-1, ", ");
                a++;
                while (a <= maxArgs) {
                    if (legalArgs[a]) {
                        startRange = a;
                        break;
                    }
                    a++;
                }
            }
        }
        addRange(buf, startRange, maxArgs, " or ");
        throwError("expected "+buf+" args; got "+nArgs);
    }

    private static String ordinal(int n) {
        switch(n+1) {
        case 0:
            return "self";
        case 1:
            return "1st";
        case 2:
            return "2nd";
        case 3:
            return "3rd";
        default:
            return Integer.toString(n+1)+"th";
        }
    }

    private static String niceName(Class arg) {
        if (arg == String.class || arg == PyString.class) {
            return "String";
        }
        if (arg.isArray()) {
            return niceName(arg.getComponentType())+"[]";
        }
        return arg.getName();
    }

    protected void throwBadArgError(int errArg, int nArgs, boolean self) {
        Hashtable table = new Hashtable();
        ReflectedArgs[] argsl = argslist;
        int n = nargs;
        for(int i=0; i 2) {
                    buf.setLength(buf.length()-2);
                    buf.append(" or ");
                }
                buf.append(name);
            }
        }

        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) {
            throwArgCountError(nArgs, self);
        }

        /*if (errArg == -1) {
          throwBadArgError(-1);
          throwError("bad self argument");
          // Bad declared class
          }*/

        throwBadArgError(errArg, nArgs, self);
    }

    // Included only for debugging purposes...
    public void printArgs() {
        System.err.println("nargs: "+nargs);
        for(int i=0; i";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy