From: <cg...@us...> - 2008-12-02 07:47:28
|
Revision: 5675 http://jython.svn.sourceforge.net/jython/?rev=5675&view=rev Author: cgroves Date: 2008-12-02 07:47:23 +0000 (Tue, 02 Dec 2008) Log Message: ----------- Hook up container methods for Map separately as it isn't actually a Collection. Modified Paths: -------------- branches/newstyle-java-types/Lib/test/test_java_integration.py branches/newstyle-java-types/src/org/python/core/Py.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/Lib/test/test_java_integration.py =================================================================== --- branches/newstyle-java-types/Lib/test/test_java_integration.py 2008-12-02 07:46:14 UTC (rev 5674) +++ branches/newstyle-java-types/Lib/test/test_java_integration.py 2008-12-02 07:47:23 UTC (rev 5675) @@ -6,7 +6,7 @@ from test import test_support from java.awt import (Dimension, Component, Rectangle, Button, Color, HeadlessException) -from java.util import Vector, Hashtable +from java.util import ArrayList, Vector, HashMap, Hashtable from java.io import FileOutputStream, FileWriter, OutputStreamWriter from java.lang import Runnable, Thread, ThreadGroup, System, Runtime, Math, Byte @@ -394,6 +394,37 @@ x = lang.String('test') self.assertRaises(TypeError, list, x) +class JavaDelegationTest(unittest.TestCase): + def test_list_delegation(self): + for c in ArrayList, Vector: + a = c() + a.add("blah") + self.assertTrue("blah" in a) + self.assertEquals(1, len(a)) + n = 0 + for i in a: + n += 1 + self.assertEquals("blah", i) + self.assertEquals(1, n) + self.assertEquals("blah", a[0]) + a[0] = "bleh" + del a[0] + self.assertEquals(0, len(a)) + + def test_map_delegation(self): + m = HashMap() + m["a"] = "b" + self.assertTrue("a" in m) + self.assertEquals("b", m["a"]) + n = 0 + for k in m: + n += 1 + self.assertEquals("a", k) + self.assertEquals(1, n) + del m["a"] + self.assertEquals(0, len(m)) + + def test_main(): test_support.run_unittest(AbstractOnSyspathTest, InstantiationTest, @@ -414,6 +445,7 @@ MethodInvTest, InterfaceTest, JavaStringTest, + JavaDelegationTest, ) if __name__ == "__main__": Modified: branches/newstyle-java-types/src/org/python/core/Py.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/Py.java 2008-12-02 07:46:14 UTC (rev 5674) +++ branches/newstyle-java-types/src/org/python/core/Py.java 2008-12-02 07:47:23 UTC (rev 5675) @@ -90,7 +90,6 @@ BOOTSTRAP_TYPES.add(PyType.class); BOOTSTRAP_TYPES.add(PyBuiltinCallable.class); BOOTSTRAP_TYPES.add(PyDataDescr.class); - BOOTSTRAP_TYPES.add(PyMethodDescr.class); } /** A unique object to indicate no conversion is possible Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-02 07:46:14 UTC (rev 5674) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-02 07:47:23 UTC (rev 5675) @@ -247,7 +247,7 @@ dict.__setitem__(inner.getSimpleName(), PyType.fromClass(inner)); } for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : getCollectionProxies().entrySet()) { - if (entry.getKey().isAssignableFrom(underlying_class)) { + if (entry.getKey() == underlying_class) { for (PyBuiltinMethod meth : entry.getValue()) { dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); } @@ -268,7 +268,7 @@ has_delete = getDescrMethod(underlying_class, "__delete__", PyObject.class) != null || getDescrMethod(underlying_class, "_dodel", PyObject.class) != null; } else { - // Pass __eq__ through to subclasses of Object + // Pass __eq__ and __repr__ through to subclasses of Object PyBuiltinCallable equals = new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override public PyObject __call__(PyObject o) { @@ -277,6 +277,13 @@ } }; dict.__setitem__("__eq__", new PyMethodDescr(this, equals)); + PyBuiltinCallable repr = new PyBuiltinMethodNarrow("__repr__", 0, 0) { + @Override + public PyObject __call__() { + return Py.newString(self.getJavaProxy().toString()); + } + }; + dict.__setitem__("__repr__", new PyMethodDescr(this, repr)); } } @@ -328,12 +335,12 @@ } } - private static class IteratorIter extends PyIterator { + private static class IterableIter extends PyIterator { private Iterator<Object> proxy; - public IteratorIter(Iterator<Object> proxy) { - this.proxy = proxy; + public IterableIter(Iterable<Object> proxy) { + this.proxy = proxy.iterator(); } public PyObject __iternext__() { @@ -365,6 +372,13 @@ if (collectionProxies == null) { collectionProxies = Generic.map(); + PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + public PyObject __call__() { + return new IterableIter(((Iterable)self.getJavaProxy())); + } + }; + collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); + PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__", 0, 0) { @Override public PyObject __call__() { @@ -372,13 +386,42 @@ } }; + PyBuiltinMethodNarrow containsProxy = new PyBuiltinMethodNarrow("__contains__", 1, 1) { + @Override + public PyObject __call__(PyObject obj) { + Object other = obj.__tojava__(Object.class); + boolean contained = ((Collection<?>)self.getJavaProxy()).contains(other); + return contained ? Py.True : Py.False; + } + }; + collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy, + containsProxy}); + + // Map doesn't extend Collection, so it needs its own version of len, iter and contains + PyBuiltinMethodNarrow mapLenProxy = new MapMethod("__len__", 0, 0) { + @Override + public PyObject __call__() { + return Py.java2py(asMap().size()); + } + }; + PyBuiltinMethodNarrow mapIterProxy = new MapMethod("__iter__", 0, 0) { + @Override + public PyObject __call__() { + return new IterableIter(asMap().keySet()); + } + }; + PyBuiltinMethodNarrow mapContainsProxy = new MapMethod("__contains__", 1, 1) { + public PyObject __call__(PyObject obj) { + Object other = obj.__tojava__(Object.class); + return asMap().containsKey(other) ? Py.True : Py.False; + } + }; PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1, 1) { @Override public PyObject __call__(PyObject key) { return Py.java2py(asMap().get(Py.tojava(key, Object.class))); } }; - PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2, 2) { @Override public PyObject __call__(PyObject key, PyObject value) { @@ -386,13 +429,18 @@ Py.tojava(value, Object.class))); } }; - PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1, 1) { @Override - public PyObject __call__(PyObject key, PyObject value) { + public PyObject __call__(PyObject key) { return Py.java2py(asMap().remove(Py.tojava(key, Object.class))); } }; + collectionProxies.put(Map.class, new PyBuiltinMethod[] {mapLenProxy, + mapIterProxy, + mapContainsProxy, + mapGetProxy, + mapPutProxy, + mapRemoveProxy}); PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { @Override @@ -404,7 +452,6 @@ } } }; - PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { @Override public PyObject __call__(PyObject key, PyObject value) { @@ -416,10 +463,9 @@ return Py.None; } }; - PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { @Override - public PyObject __call__(PyObject key, PyObject value) { + public PyObject __call__(PyObject key) { if (key instanceof PyInteger) { return Py.java2py(asList().remove(((PyInteger)key).getValue())); } else { @@ -427,20 +473,9 @@ } } }; - - PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { - public PyObject __call__() { - return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); - } - }; - collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); - collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy}); - collectionProxies.put(Map.class, new PyBuiltinMethod[] {mapGetProxy, - mapPutProxy, - mapRemoveProxy}); collectionProxies.put(List.class, new PyBuiltinMethod[] {listGetProxy, - listSetProxy, - listRemoveProxy}); + listSetProxy, + listRemoveProxy}); } return collectionProxies; } Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-02 07:46:14 UTC (rev 5674) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-02 07:47:23 UTC (rev 5675) @@ -754,9 +754,9 @@ } /** - * @return an Iterable over the Python iterator returned by __iter__ on this object. If this - * object doesn't support __iter__, a TypeException will be raised when iterator is - * called on the returned Iterable. + * Returns an Iterable over the Python iterator returned by __iter__ on this object. If this + * object doesn't support __iter__, a TypeException will be raised when iterator is called on + * the returned Iterable. */ public Iterable<PyObject> asIterable() { return new Iterable<PyObject>() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |