From: <cg...@us...> - 2008-11-29 00:48:42
|
Revision: 5659 http://jython.svn.sourceforge.net/jython/?rev=5659&view=rev Author: cgroves Date: 2008-11-29 00:48:32 +0000 (Sat, 29 Nov 2008) Log Message: ----------- Assign the type on PyBeanProperty to the type returned by its getter and only use a setter if its parameter's type matches that. Push accesses to PyObject.javaProxy that query information from the proxy through getJavaProxy. This calls proxyInit if the proxy doesn't already exist which keeps things from blowing up if something accesses the javaProxy in an __init__. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/IdImpl.java branches/newstyle-java-types/src/org/python/core/PyBeanProperty.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/IdImpl.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -66,7 +66,7 @@ public long id(PyObject o) { if (o.getType() instanceof PyJavaType) { - return java_obj_id(o.javaProxy); + return java_obj_id(o.getJavaProxy()); } else { return java_obj_id(o); } Modified: branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -62,7 +62,7 @@ Object jvalue = Py.tojava(value, myType); try { - setMethod.invoke(iself, new Object[] {jvalue}); + setMethod.invoke(iself, jvalue); } catch (Exception e) { throw Py.JavaError(e); } Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -70,11 +70,12 @@ name = normalize(StringUtil.decapitalize(name)); PyBeanProperty prop = props.get(name); if (prop == null) { - prop = new PyBeanProperty(name, underlying_class, null, null); + prop = new PyBeanProperty(name, null, null, null); props.put(name, prop); } if (get) { prop.getMethod = meth; + prop.myType = meth.getReturnType(); } else { prop.setMethod = meth; } @@ -134,7 +135,7 @@ // If the return types on the set and get methods for a property don't agree, the get // get method takes precedence if (prop.getMethod != null && prop.setMethod != null - && prop.getMethod.getReturnType() != prop.setMethod.getReturnType()) { + && prop.myType != prop.setMethod.getParameterTypes()[0]) { prop.setMethod = null; } dict.__setitem__(prop.__name__, prop); @@ -225,7 +226,7 @@ new PyBuiltinMethodNarrow("__len__", 0, 0) { @Override public PyObject __call__() { - return Py.newInteger(((Collection<?>)self.javaProxy).size()); + return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); } }; @@ -233,7 +234,7 @@ new PyBuiltinMethodNarrow("__getitem__", 1, 1) { @Override public PyObject __call__(PyObject key) { - return Py.java2py(((Map<?, ?>)self.javaProxy).get(Py.tojava(key, Object.class))); + return Py.java2py(((Map<?, ?>)self.getJavaProxy()).get(Py.tojava(key, Object.class))); } }; @@ -241,7 +242,7 @@ new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @Override public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<Object, Object>)self.javaProxy).put(Py.tojava(key, Object.class), + return Py.java2py(((Map<Object, Object>)self.getJavaProxy()).put(Py.tojava(key, Object.class), Py.tojava(value, Object.class))); } @@ -251,7 +252,7 @@ new PyBuiltinMethodNarrow("__delitem__", 1, 1) { @Override public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<?, ?>)self.javaProxy).remove(Py.tojava(key, Object.class))); + return Py.java2py(((Map<?, ?>)self.getJavaProxy()).remove(Py.tojava(key, Object.class))); } }; @@ -260,7 +261,7 @@ @Override public PyObject __call__(PyObject key) { if (key instanceof PyInteger) { - return Py.java2py(((List<?>)self.javaProxy).get(((PyInteger)key).getValue())); + return Py.java2py(((List<?>)self.getJavaProxy()).get(((PyInteger)key).getValue())); } else { throw Py.TypeError("only integer keys accepted"); } @@ -272,7 +273,7 @@ @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - ((List<Object>)self.javaProxy).set(((PyInteger)key).getValue(), + ((List<Object>)self.getJavaProxy()).set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); } else { throw Py.TypeError("only integer keys accepted"); @@ -286,7 +287,7 @@ @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); + return Py.java2py(((List<Object>)self.getJavaProxy()).remove(((PyInteger)key).getValue())); } else { throw Py.TypeError("only integer keys accepted"); } @@ -322,7 +323,7 @@ private static final PyBuiltinMethodNarrow ITERABLE_PROXY = new PyBuiltinMethodNarrow("__iter__", 0, 0) { public PyObject __call__() { - return new IteratorIter(((Iterable)self.javaProxy).iterator()); + return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); } }; @@ -330,8 +331,8 @@ new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override public PyObject __call__(PyObject o) { - return self.javaProxy.equals(o.__tojava__(self.javaProxy.getClass())) ? Py.True - : Py.False; + return self.getJavaProxy().equals(o.__tojava__(self.getJavaProxy().getClass())) ? + Py.True : Py.False; } }; Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -37,9 +37,12 @@ } - // This field is only filled on Python wrappers for Java objects and for Python subclasses of - // Java classes. - Object javaProxy; + /** + * An underlying Java instance that this object is wrapping or is a subclass of. Anything + * attempting to use the proxy should go through {@link #getJavaProxy()} which ensures that it's + * initialized. + */ + protected Object javaProxy; private PyType objtype; @@ -243,7 +246,7 @@ * @param c the Class to convert this <code>PyObject</code> to. **/ public Object __tojava__(Class<?> c) { - if ((c == Object.class || c == Serializable.class) && javaProxy != null) { + if ((c == Object.class || c == Serializable.class) && getJavaProxy() != null) { return javaProxy; } if (c.isInstance(this)) { @@ -255,12 +258,19 @@ c = tmp; } } - if (c.isInstance(javaProxy)) { + if (c.isInstance(getJavaProxy())) { return javaProxy; } return Py.NoConversion; } + protected Object getJavaProxy() { + if (javaProxy == null) { + proxyInit(); + } + return javaProxy; + } + private static final Map<Class<?>, Class<?>> primitiveMap = Generic.map(); static { primitiveMap.put(Character.TYPE, Character.class); @@ -1573,6 +1583,8 @@ * @return the result of the comparison **/ public PyObject _is(PyObject o) { + // Access javaProxy directly here as is is for object identity, and at best getJavaProxy + // will initialize a new object with a different identity return this == o || (javaProxy != null && javaProxy == o.javaProxy) ? Py.True : Py.False; } @@ -1583,6 +1595,8 @@ * @return the result of the comparison **/ public PyObject _isnot(PyObject o) { + // Access javaProxy directly here as is is for object identity, and at best getJavaProxy + // will initialize a new object with a different identity return this != o || javaProxy != o.javaProxy ? Py.True : Py.False; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |