From: <cg...@us...> - 2008-11-28 08:56:41
|
Revision: 5651 http://jython.svn.sourceforge.net/jython/?rev=5651&view=rev Author: cgroves Date: 2008-11-28 08:56:38 +0000 (Fri, 28 Nov 2008) Log Message: ----------- Add an EQ_PROXY to PyJavaType to pass equals through to wrapped Java instances like PyJavaInstance did. Make PyBuiltinMethod Cloneable and use clone to implement a default bind implementation. This eliminates 75% of the crud that came with creating a builtin method in Java. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/CollectionIter.java branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java Modified: branches/newstyle-java-types/src/org/python/core/CollectionIter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -1,59 +1,30 @@ -// Copyright (c) Finn Bock - package org.python.core; -import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; -import java.util.Map; -class CollectionIter{ - PyObject findCollection(Object object) { - if (object instanceof Map) { - return new IteratorIter(((Map) object).keySet().iterator()); - } - if (object instanceof Iterable) { - return new IteratorIter(((Iterable)object).iterator()); - } - if (object instanceof Iterator) { - return new IteratorIter(((Iterator) object)); - } - if (object instanceof Enumeration) { - return new EnumerationIter(((Enumeration) object)); - } - if (object instanceof Dictionary) { - return new EnumerationIter(((Dictionary) object).keys()); - } - - return null; - } - -} - class EnumerationIter extends PyIterator { - private Enumeration proxy; - public EnumerationIter(Enumeration proxy) { + private Enumeration<Object> proxy; + + public EnumerationIter(Enumeration<Object> proxy) { this.proxy = proxy; } public PyObject __iternext__() { - if (!this.proxy.hasMoreElements()) - return null; - return Py.java2py(this.proxy.nextElement()); + return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; } } class IteratorIter extends PyIterator { - private Iterator proxy; - public IteratorIter(Iterator proxy) { + private Iterator<Object> proxy; + + public IteratorIter(Iterator<Object> proxy) { this.proxy = proxy; } public PyObject __iternext__() { - if (!this.proxy.hasNext()) - return null; - return Py.java2py(this.proxy.next()); + return proxy.hasNext() ? Py.java2py(proxy.next()) : null; } } Modified: branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -2,7 +2,8 @@ import org.python.expose.ExposeAsSuperclass; -public abstract class PyBuiltinMethod extends PyBuiltinCallable implements ExposeAsSuperclass { +public abstract class PyBuiltinMethod extends PyBuiltinCallable implements ExposeAsSuperclass, + Cloneable { protected PyObject self; @@ -15,15 +16,31 @@ super(info); this.self = self; } - + protected PyBuiltinMethod(String name) { this(null, new DefaultInfo(name)); } - + + @Override + public PyBuiltinCallable bind(PyObject bindTo) { + if(self == null) { + PyBuiltinMethod bindable; + try { + bindable = (PyBuiltinMethod)clone(); + } catch(CloneNotSupportedException e) { + throw new RuntimeException("Didn't expect PyBuiltinMethodto throw " + + "CloneNotSupported since it implements Cloneable", e); + } + bindable.self = bindTo; + return bindable; + } + return this; + } + public PyObject getSelf(){ return self; } - + public PyMethodDescr makeDescriptor(PyType t) { return new PyMethodDescr(t, this); } Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -45,15 +45,12 @@ String methname = meth.getName(); String nmethname = normalize_name(methname); PyReflectedFunction reflfunc = (PyReflectedFunction)dict.__finditem__(nmethname); - boolean added = false; if (reflfunc == null) { dict.__setitem__(nmethname, new PyReflectedFunction(meth)); - added = true; } else { reflfunc.addMethod(meth); - added = true; } - if (added && !Modifier.isStatic(meth.getModifiers())) { + if (!Modifier.isStatic(meth.getModifiers())) { // check for xxxX.* int n = meth.getParameterTypes().length; if (methname.startsWith("get") && n == 0) { @@ -164,6 +161,7 @@ } } } + dict.__setitem__("__eq__", new PyMethodDescr(this, EQUALS_PROXY)); if (ClassDictInit.class.isAssignableFrom(underlying_class) && underlying_class != ClassDictInit.class) { try { @@ -222,104 +220,43 @@ 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); - } - + protected static final PyBuiltinMethodNarrow LEN_PROXY = + new PyBuiltinMethodNarrow("__len__", 0, 0) { @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); - } - + protected static final PyBuiltinMethodNarrow MAP_GET_PROXY = + new PyBuiltinMethodNarrow("__getitem__", 1, 1) { @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); - } - + protected static final PyBuiltinMethodNarrow MAP_PUT_PROXY = + new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @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); - } - + protected static final PyBuiltinMethodNarrow MAP_REMOVE_PROXY = + new PyBuiltinMethodNarrow("__delitem__", 1, 1) { @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); - } - + protected static final PyBuiltinMethodNarrow LIST_GET_PROXY = + new PyBuiltinMethodNarrow("__getitem__", 1, 1){ @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())); @@ -327,23 +264,11 @@ 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); - } - + protected static final PyBuiltinMethodNarrow LIST_SET_PROXY = + new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @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(), @@ -353,23 +278,11 @@ } 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 + protected static final PyBuiltinMethodNarrow LIST_REMOVE_PROXY = + new PyBuiltinMethodNarrow("__delitem__", 1, 1) { + @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); @@ -377,38 +290,33 @@ throw Py.TypeError("only integer keys accepted"); } } - } + }; - public static class IterableProxy extends PyBuiltinMethodNarrow { - - public IterableProxy() { - super("__iter__", 0, 0); + public static final PyBuiltinMethodNarrow ITERABLE_PROXY = + new PyBuiltinMethodNarrow("__iter__", 0, 0) { + public PyObject __call__() { + return new IteratorIter(((Iterable)self.javaProxy).iterator()); } + }; - protected IterableProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + public static final PyBuiltinMethodNarrow EQUALS_PROXY = + new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new IterableProxy(getType(), self, info); + public PyObject __call__(PyObject o) { + return self.javaProxy.equals(o.__tojava__(self.javaProxy.getClass())) ? Py.True + : Py.False; } + }; - @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()}); + _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {ITERABLE_PROXY}); + _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {LEN_PROXY}); + _collectionProxies.put(Map.class, new PyBuiltinMethod[] {MAP_GET_PROXY, + MAP_PUT_PROXY, + MAP_REMOVE_PROXY}); + _collectionProxies.put(List.class, new PyBuiltinMethod[] {LIST_GET_PROXY, + LIST_SET_PROXY, + LIST_REMOVE_PROXY}); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |