⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pyinstance.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// Copyright (c) Corporation for National Research Initiativespackage org.python.core;import java.util.Hashtable;import java.util.StringTokenizer;import java.io.Serializable;/** * A python class instance. */public class PyInstance extends PyObject{    //This field is only used by Python subclasses of Java classes    Object javaProxy;    /**       The namespace of this instance.  Contains all instance attributes.    **/    public PyObject __dict__;    /* Override serialization behavior */    private void readObject(java.io.ObjectInputStream in)        throws java.io.IOException, ClassNotFoundException    {        in.defaultReadObject();        String module = in.readUTF();        String name = in.readUTF();        /* Check for types and missing members here */        //System.out.println("module: "+module+", "+name);        PyObject mod = imp.importName(module.intern(), false);        PyClass pyc = (PyClass)mod.__getattr__(name.intern());        __class__ = pyc;        if (javaProxy != null)            ((PyProxy) javaProxy)._setPySystemState(Py.getSystemState());    }    private void writeObject(java.io.ObjectOutputStream out)        throws java.io.IOException    {        //System.out.println("writing: "+getClass().getName());        out.defaultWriteObject();        PyObject name = __class__.__findattr__("__module__");        if (!(name instanceof PyString) || name == Py.None || name == null) {            throw Py.ValueError("Can't find module for class: "+                                __class__.__name__);        }        out.writeUTF(name.toString());        name = __class__.__findattr__("__name__");        if (!(name instanceof PyString) || name == Py.None || name == null) {            throw Py.ValueError("Can't find module for class with no name");        }        out.writeUTF(name.toString());    }    /**       Returns a new    **/    public PyInstance(PyClass iclass, PyObject dict) {        super(iclass);        __dict__ = dict;    }    public PyInstance(PyClass iclass) {        this(iclass, new PyStringMap());    }    public PyInstance() {}    private static Hashtable primitiveMap;    protected void makeProxy() {        Class c = __class__.proxyClass;        PyProxy proxy;        ThreadState ts = Py.getThreadState();        try {            ts.pushInitializingProxy(this);            try {                proxy = (PyProxy)c.newInstance();            } catch (java.lang.InstantiationException e) {                Class sup = c.getSuperclass();                String msg = "Default constructor failed for Java superclass";                if (sup != null)                    msg += " " + sup.getName();                throw Py.TypeError(msg);            } catch (NoSuchMethodError nsme) {                throw Py.TypeError("constructor requires arguments");            } catch (Exception exc) {                throw Py.JavaError(exc);            }        } finally {            ts.popInitializingProxy();        }        if (javaProxy != null && javaProxy != proxy) {            // The javaProxy can be initialized in Py.jfindattr()            throw Py.TypeError("Proxy instance already initialized");        }        PyInstance proxyInstance = proxy._getPyInstance();        if (proxyInstance != null && proxyInstance != this) {            // The proxy was initialized to another instance!!            throw Py.TypeError("Proxy initialization conflict");        }        javaProxy = proxy;    }    public Object __tojava__(Class c) {        if ((c == Object.class || c == Serializable.class) &&                                                    javaProxy != null) {            return javaProxy;        }        if (c.isInstance(this))            return this;        if (c.isPrimitive()) {            if (primitiveMap == null) {                primitiveMap = new Hashtable();                primitiveMap.put(Character.TYPE, Character.class);                primitiveMap.put(Boolean.TYPE, Boolean.class);                primitiveMap.put(Byte.TYPE, Byte.class);                primitiveMap.put(Short.TYPE, Short.class);                primitiveMap.put(Integer.TYPE, Integer.class);                primitiveMap.put(Long.TYPE, Long.class);                primitiveMap.put(Float.TYPE, Float.class);                primitiveMap.put(Double.TYPE, Double.class);            }            Class tmp = (Class)primitiveMap.get(c);            if (tmp != null)                c = tmp;        }        if (javaProxy == null && __class__.proxyClass != null) {            makeProxy();        }        if (c.isInstance(javaProxy))            return javaProxy;        if (__class__.__tojava__ != null) {            //try {            PyObject ret =                __class__.__tojava__.__call__(this, PyJavaClass.lookup(c));            if (ret == Py.None)                return Py.NoConversion;            if (ret != this)                return ret.__tojava__(c);            /*} catch (PyException exc) {              System.err.println("Error in __tojava__ method");              Py.printException(exc);              }*/        }        return Py.NoConversion;    }    public void __init__(PyObject[] args, String[] keywords) {        // Invoke our own init function        PyObject init = __class__.lookup("__init__", true);        PyObject ret = null;        if (init != null) {            ret = init.__call__(this, args, keywords);        }        if (ret == null) {            if (args.length != 0) {                init = __class__.lookup("__init__", false);                if (init != null) {                    ret = init.__call__(this, args, keywords);                } else {                    throw Py.TypeError("this constructor takes no arguments");                }            }        }        else if (ret != Py.None) {            throw Py.TypeError("constructor has no return value");        }        // Now init all superclasses that haven't already been initialized        if (javaProxy == null && __class__.proxyClass != null) {            makeProxy();        }    }    public PyObject __jfindattr__(String name) {        //System.err.println("jfinding: "+name);        return __findattr__(name, true);    }    public PyObject __findattr__(String name) {        return __findattr__(name, false);    }    public PyObject __findattr__(String name, boolean stopAtJava) {        PyObject result = ifindlocal(name);        if (result != null)            return result;        // it wasn't found in the instance, try the class        PyObject[] result2 = __class__.lookupGivingClass(name, stopAtJava);        if (result2[0] != null)            return result2[0]._doget(this, result2[1]);        return ifindfunction(name);    }    protected PyObject ifindlocal(String name) {        if (name == "__dict__") return __dict__;        if (name == "__class__") return __class__;        if (__dict__ == null) return null;        return __dict__.__finditem__(name);    }    protected PyObject ifindclass(String name, boolean stopAtJava) {        return __class__.lookup(name, stopAtJava);    }    protected PyObject ifindfunction(String name) {        PyObject getter = __class__.__getattr__;        if (getter == null)            return null;        try {            return getter.__call__(this, new PyString(name));        } catch (PyException exc) {            if (Py.matchException(exc, Py.AttributeError)) return null;            throw exc;        }    }    public PyObject invoke(String name) {        PyObject f = ifindlocal(name);        if (f == null) {            f = ifindclass(name, false);            if (f != null) {                if (f instanceof PyFunction) {                    return f.__call__(this);                } else {                    f = f._doget(this);                }            }        }        if (f == null) f = ifindfunction(name);        if (f == null) throw Py.AttributeError(name);        return f.__call__();    }    public PyObject invoke(String name, PyObject arg1) {        PyObject f = ifindlocal(name);        if (f == null) {            f = ifindclass(name, false);            if (f != null) {                if (f instanceof PyFunction) {                    return f.__call__(this, arg1);                } else {                    f = f._doget(this);                }            }        }        if (f == null) f = ifindfunction(name);        if (f == null) throw Py.AttributeError(name);        return f.__call__(arg1);    }    public PyObject invoke(String name, PyObject arg1, PyObject arg2) {        PyObject f = ifindlocal(name);        if (f == null) {            f = ifindclass(name, false);            if (f != null) {                if (f instanceof PyFunction) {                    return f.__call__(this, arg1, arg2);                } else {                    f = f._doget(this);                }            }        }        if (f == null) f = ifindfunction(name);        if (f == null) throw Py.AttributeError(name);        return f.__call__(arg1, arg2);    }    public void __setattr__(String name, PyObject value) {        if (name == "__class__") {            if (value instanceof PyClass) {                __class__ = (PyClass)value;            } else {                throw Py.TypeError("__class__ must be set to a class");            }            return;        } else if (name == "__dict__") {            __dict__ = value;            return;        }        PyObject setter = __class__.__setattr__;        if (setter != null) {            setter.__call__(this, new PyString(name), value);        } else {            if (__class__.getProxyClass() != null) {                PyObject field = __class__.lookup(name, false);                if (field == null) {                    noField(name, value);                } else if (!field._doset(this, value)) {                    unassignableField(name, value);                }            } else {                __dict__.__setitem__(name, value);            }        }    }    protected void noField(String name, PyObject value) {        __dict__.__setitem__(name, value);    }    protected void unassignableField(String name, PyObject value) {        __dict__.__setitem__(name, value);    }    public void __delattr__(String name) {        // Need code to handle _dodel        PyObject deller = __class__.__delattr__;        if (deller != null) {            deller.__call__(this, new PyString(name));        } else {            try {                __dict__.__delitem__(name);            } catch (PyException exc) {                if (Py.matchException(exc, Py.KeyError))                    throw Py.AttributeError("class " + __class__.__name__ +                                        " has no attribute '" + name + "'");            };        }    }    public PyObject invoke_ex(String name, PyObject[] args, String[] keywords)    {        PyObject meth = __findattr__(name);        if (meth == null)            return null;        return meth.__call__(args, keywords);    }    public PyObject invoke_ex(String name) {        PyObject meth = __findattr__(name);        if (meth == null)            return null;        return meth.__call__();    }    public PyObject invoke_ex(String name, PyObject arg1) {        PyObject meth = __findattr__(name);        if (meth == null)            return null;        return meth.__call__(arg1);    }    public PyObject invoke_ex(String name, PyObject arg1, PyObject arg2) {        PyObject meth = __findattr__(name);        if (meth == null)            return null;        return meth.__call__(arg1, arg2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -