From: Jeff E. <jem...@fr...> - 2010-03-02 19:05:47
|
The root of your problem is that Jython adds syntactic sugar (called bean property shortcuts) to Java classes (and interfaces) so that you can replace calling getFoo/setFoo methods with attribute syntax (.foo). Since you have both a getLength method and a length field in your class, there is ambiguity between whether self.length=0 means accessing the attribute or is a shortcut for calling self.setLength. Apparently, the latter interpretation takes precedence. Since your interface has a getLength method but no setLength method, it complains that length is read-only. The fix, as someone explained before, is not to create fields for classes that conflict with the bean property shortcuts. Rename the length field to _length, myLength, aLength, theLength, or anything else. You've done that for source, destination, version, and MessageID. Sherrell, Dana wrote: > *Hi –* > > * * > > *I am hoping I can get an explanation regarding an issue I have. I have > found a work around, however I do not understand why the issue is > occurring and why the work around even resolves it. Below I have a very > simple interface in Java and a jython object that implements that > interface. If I try something to access the length property from Java > by doing:* > > * * > > * *MessageInterface message = (MessageInterface)factory.createObject(); > > System./out/.println("Message length:" + message.getLength()); > > > > *I get the following error: * > > > > Exception in thread "main" Traceback (most recent call last): > > File "__pyclasspath__/message_interface.py", line 90, in __init__ > > AttributeError: read-only attr: length > > * * > > * * > > *If I add a setter method for length and expose that through the > interface, I get a max recursion depth error. If I change self.length > to instead be self.msg_length the problem goes away. If I put an > underscore in front of length the problem goes away. I have this same > issue with self.source instead of self.msg_source. However, I don’t > have any problems with self.target and other attributes. Why is it an > issue for some attributes and not others? I would like to understand > this problem so if anyone can explain what is going on to me I would > really appreciate it.* > > * * > > *Thank you.* > > * * > > *public* *interface* MessageInterface { > > > > *public* *int* getSource(); > > *public* *int* getDestination(); > > *public* *int* getLength(); > > *public* *int* getVersion(); > > *public* *int* getMessageID(); > > } > > > > > > class *Message*(MessageInterface): > > def *__init__*(/self/): > > /"""_@sig_ Message()"""/ > > > > /self/.length = 0 > > /self/.msg_version = 0 > > /self/.msg_source = 0 > > /self/.target = 0 > > /self/.Message_ID = 0 > > > > > > > > def *getLength*(/self/): > > return /self/.length > > > > > > def *getSource*(/self/): > > return /self/.msg_source > > > > def *getDestination*(/self/): > > return /self/.target > > > > def *getMessageID*(/self/): > > return /self/.Message_ID > > > > def *getVersion*(/self/): > > return /self/.msg_version > > > > > > > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------------ > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev > > > ------------------------------------------------------------------------ > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |