|
From: Jose M. R. <jm...@in...> - 2003-10-07 11:50:29
|
My first was attempt was to define a bean property function, only a
getter and no
setter for the read only property. Unfortunately my java class simulates
a Python
module and the static getter for the module property is not recognized
as a property,
I think that getters / setters only work for instance properties. Is
this inteded? I
expected that a function like "public static String getFoo()" defined a
class property.
I followed your suggestion and derived a new PyObject class based on
PyBeanProperties that is suited for class properties, it worked fine.
The code is:
<cutHere-->
public class PyStaticBeanProperty extends PyObject {
public Method getMethod;
public Method setMethod;
public Class myType;
String name;
public PyStaticBeanProperty(String name, Class myType, Method
getMethod,
Method setMethod) {
this.name = name;
this.getMethod = getMethod;
this.setMethod = setMethod;
this.myType = myType;
}
public PyObject _doget(PyObject self) {
if (self != null) {
throw Py.AttributeError("attribute: "+ name +
" is not an instance attribute");
}
if (getMethod == null) {
throw Py.AttributeError("write-only attr: " + name);
}
try {
Object value = getMethod.invoke(null, Py.EmptyObjects);
return Py.java2py(value);
} catch (Exception e) {
throw Py.JavaError(e);
}
}
public boolean _doset(PyObject self, PyObject value) {
if (self != null) {
throw Py.AttributeError("attribute: " + name +
" is not an instance attributre");
}
if (setMethod == null) {
throw Py.AttributeError("read-only attr: " + name);
}
Object jvalue = null;
// Special handling of tuples
// try to call a class constructor
if (value instanceof PyTuple) {
try {
PyTuple vtup = (PyTuple) value;
value = PyJavaClass.lookup(myType).__call__(vtup.list);
} catch (Throwable t) {
// If something goes wrong ignore it?
}
}
if (jvalue == null) {
jvalue = Py.tojava(value, myType);
}
try {
setMethod.invoke(null, new Object[] { jvalue });
} catch (Exception e) {
throw Py.JavaError(e);
}
return true;
}
public PyStaticBeanProperty copy() {
return new PyStaticBeanProperty(name, myType, getMethod, setMethod);
}
public String toString() {
String typeName = "unknown";
if (myType != null) {
typeName = myType.getName();
}
return "<staticBeanProperty "+ name +" type: "+ typeName +" at " +
Py.id(this) + ">";
}
}
</cutHere>
Now I can write something like:
dict.__setitem__("foo", new PyStaticBeanProperty("foo",
String.class, getter, null));
and obtain a read only property.
I think that a Wiki for Jython would be great, so users can contribute
in the
documentation, explain tips, help other users, etc.
Regards,
Jose . Rus
Jeff Emanuel wrote:
>Jose,
>
>This is simple if you rely on Jython bean
>property access. If your Java class has
>a getFoo method but no setFoo, then foo
>would be read only.
>
>Since you are adding your own custom attribute
>objects to the dictionary, you can make the
>_doset method of the PyObjects you put in the
>dictionary throw a Py.AttributeError. That's
>how PyBeanProperty implements read-only
>attributes.
>
>Jeff
>
>
>-----Original Message-----
>From: Jose M. Rus [mailto:jm...@in...]
>Sent: Monday, October 06, 2003 10:35 AM
>To: jyt...@li...
>Subject: [Jython-users] read only attributes
>
>
>Hi list,
>
>I have a java class that emulates a Python module. It implements
>ClassDictInit and
>it defines some attributes in his own dictionary. Now I want to make one
>of them
>read only.
>
>What is the recommended way of doing this?
>
>Thanks.
>
>
>
>
>-------------------------------------------------------
>This sf.net email is sponsored by:ThinkGeek
>Welcome to geek heaven.
>http://thinkgeek.com/sf
>_______________________________________________
>Jython-users mailing list
>Jyt...@li...
>https://lists.sourceforge.net/lists/listinfo/jython-users
>
>
>
>
|