From: <cg...@us...> - 2008-11-10 05:15:14
|
Revision: 5567 http://jython.svn.sourceforge.net/jython/?rev=5567&view=rev Author: cgroves Date: 2008-11-10 05:15:10 +0000 (Mon, 10 Nov 2008) Log Message: ----------- Implement the collection proxies as methods on PyJavaTypes Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/CollectionIter.java branches/newstyle-java-types/src/org/python/core/CollectionProxy.java branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyObject.java Modified: branches/newstyle-java-types/src/org/python/core/CollectionIter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -2,13 +2,10 @@ package org.python.core; -import java.lang.reflect.Method; -import java.util.Collection; import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; -import java.util.Vector; class CollectionIter{ PyObject findCollection(Object object) { @@ -30,7 +27,7 @@ return null; } - + } class EnumerationIter extends PyIterator { Modified: branches/newstyle-java-types/src/org/python/core/CollectionProxy.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionProxy.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/CollectionProxy.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -2,7 +2,6 @@ package org.python.core; -import java.util.Collection; import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; @@ -16,15 +15,15 @@ public static CollectionProxy findCollection(Object object) { if (object == null) return NoProxy; - + if (object instanceof List) { return new ListProxy(((List) object)); } if (object instanceof Map) { return new MapProxy(((Map) object)); } - if (object instanceof Collection) { - return new IteratorProxy(((Collection) object).iterator()); + if (object instanceof Iterable) { + return new IteratorProxy(((Iterable) object).iterator()); } if (object instanceof Iterator) { return new IteratorProxy(((Iterator) object)); @@ -44,7 +43,7 @@ return NoProxy; } - /** The basic functions to implement a mapping* */ + /** The basic functions to implement a mapping */ public int __len__() { throw Py.AttributeError("__len__"); } @@ -92,8 +91,7 @@ public PyObject __finditem__(int key) { if (key != this.counter) { - throw Py - .ValueError("enumeration indices must be consecutive ints starting at 0"); + throw Py.ValueError("enumeration indices must be consecutive ints starting at 0"); } this.counter++; if (this.proxy.hasMoreElements()) { @@ -141,8 +139,7 @@ public void __setitem__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - this.proxy.setElementAt(Py.tojava(value, Object.class), - ((PyInteger) key).getValue()); + this.proxy.setElementAt(Py.tojava(value, Object.class), ((PyInteger)key).getValue()); } else { throw Py.TypeError("only integer keys accepted"); } @@ -278,8 +275,7 @@ public PyObject __finditem__(int key) { if (key != this.counter) { - throw Py - .ValueError("iterator indices must be consecutive ints starting at 0"); + throw Py.ValueError("iterator indices must be consecutive ints starting at 0"); } this.counter++; if (this.proxy.hasNext()) { Modified: branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -21,7 +21,7 @@ } /** - * @return a new instance of this type of PyBuiltinFunction bound to self + * Returns a new instance of this type of PyBuiltinFunction bound to self */ abstract public PyBuiltinCallable bind(PyObject self); Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -4,18 +4,21 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; +import org.python.util.Generic; public class PyJavaType extends PyType implements ExposeAsSuperclass { private final static Class<?>[] OO = {PyObject.class, PyObject.class}; public static PyObject wrapJavaObject(Object o) { - PyObject obj = new PyObject(PyType.fromClass(o.getClass())); + PyObject obj = new PyObjectDerived(PyType.fromClass(o.getClass())); obj.javaProxy = o; return obj; } @@ -154,6 +157,13 @@ for (Class<?> inner : underlying_class.getClasses()) { dict.__setitem__(inner.getSimpleName(), PyType.fromClass(inner)); } + for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : _collectionProxies.entrySet()) { + if (entry.getKey().isAssignableFrom(underlying_class)) { + for (PyBuiltinMethod meth : entry.getValue()) { + dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); + } + } + } if (ClassDictInit.class.isAssignableFrom(underlying_class) && underlying_class != ClassDictInit.class) { try { @@ -211,4 +221,194 @@ private static PyException error(Exception e) { return Py.JavaError(e); } + + protected static class LenProxy extends PyBuiltinMethodNarrow { + public LenProxy() { + super("__len__", 0, 0); + } + + protected LenProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new LenProxy(getType(), self, info); + } + + @Override + public PyObject __call__() { + return Py.newInteger(((Collection<?>)self.javaProxy).size()); + } + } + + protected static class MapGetProxy extends PyBuiltinMethodNarrow { + public MapGetProxy() { + super("__getitem__", 1, 1); + } + + protected MapGetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapGetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key) { + return Py.java2py(((Map<?, ?>)self.javaProxy).get(Py.tojava(key, Object.class))); + } + } + + protected static class MapPutProxy extends PyBuiltinMethodNarrow { + public MapPutProxy() { + super("__setitem__", 2, 2); + } + + protected MapPutProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapPutProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(((Map<Object, Object>)self.javaProxy).put(Py.tojava(key, Object.class), + Py.tojava(value, + Object.class))); + } + } + + protected static class MapRemoveProxy extends PyBuiltinMethodNarrow { + + public MapRemoveProxy() { + super("__delitem__", 1, 1); + } + + protected MapRemoveProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapRemoveProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(((Map<?, ?>)self.javaProxy).remove(Py.tojava(key, Object.class))); + } + } + + protected static class ListGetProxy extends PyBuiltinMethodNarrow { + public ListGetProxy() { + super("__getitem__", 1, 1); + } + + protected ListGetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListGetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key) { + if (key instanceof PyInteger) { + return Py.java2py(((List<?>)self.javaProxy).get(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + } + + protected static class ListSetProxy extends PyBuiltinMethodNarrow { + public ListSetProxy() { + super("__setitem__", 2, 2); + } + + protected ListSetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListSetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + ((List<Object>)self.javaProxy).set(((PyInteger)key).getValue(), + Py.tojava(value, Object.class)); + } else { + throw Py.TypeError("only integer keys accepted"); + } + return Py.None; + } + } + + protected static class ListRemoveProxy extends PyBuiltinMethodNarrow { + public ListRemoveProxy() { + super("__delitem__", 1, 1); + } + + protected ListRemoveProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListRemoveProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + } + + public static class IterableProxy extends PyBuiltinMethodNarrow { + + public IterableProxy() { + super("__iter__", 0, 0); + } + + protected IterableProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new IterableProxy(getType(), self, info); + } + + @Override + public PyObject __call__() { + return new IteratorIter(((Iterable)self.javaProxy).iterator()); + } + } + + static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies = Generic.map(); + static { + _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {new IterableProxy()}); + _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {new LenProxy()}); + _collectionProxies.put(Map.class, new PyBuiltinMethod[] {new MapGetProxy(), + new MapPutProxy(), + new MapRemoveProxy()}); + _collectionProxies.put(List.class, new PyBuiltinMethod[] {new ListGetProxy(), + new ListSetProxy(), + new ListRemoveProxy()}); + } } Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -547,6 +547,7 @@ * @param value the value to set this key to **/ public void __setitem__(PyObject key, PyObject value) { + Thread.dumpStack(); throw Py.TypeError(String.format("'%.200s' object does not support item assignment", getType().fastGetName())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |