Issue1789561

classification
Title: Functions named 'set' 'get' or 'is' do not work
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: ayeshaiqbal, cgroves, derbylittle
Priority: normal Keywords:

Created on 2007-09-06.18:54:30 by derbylittle, last changed 2008-01-19.23:17:33 by cgroves.

Messages
msg1897 (view) Author: David Little (derbylittle) Date: 2007-09-06.18:54:30
I'm not sure how to join the project and make a this change to the source myself so I thought I would post this here:

I discovered an error that causes Jython to be unable to make calls to functions with the names "set", "get", or "is". This is because the code which looks for getters and setters (which assumes these sorts of functions begin with these three prefixes) does not check to see if these values are prefixes (it simply assumes that they always will be prefixes). The following code (found in PyType.java under  branches/Release_2_2maint) is currently:

if(added && !Modifier.isStatic(meth.getModifiers())) {
  // check for xxxX.*
  int n = meth.getParameterTypes().length;
  if(methname.startsWith("get") && n == 0) {
     propnames.put(methname.substring(3), "getter");
  } else if(methname.startsWith("is") && n == 0
            && meth.getReturnType() == Boolean.TYPE) {
    propnames.put(methname.substring(2), "getter");
  } else if(methname.startsWith("set") && n == 1) {
    propnames.put(methname.substring(3), meth);
  }
}

but should really read:

if(added && !Modifier.isStatic(meth.getModifiers())) {
   // check for xxxX.*
   int n = meth.getParameterTypes().length;
   if(methname.startsWith("get") && n == 0 && 
      methname.length() > 3) {
      propnames.put(methname.substring(3), "getter");
   } else if(methname.startsWith("is") && n == 0
             && meth.getReturnType() == Boolean.TYPE &&
             methname.length() > 2) {
     propnames.put(methname.substring(2), "getter");
   } else if(methname.startsWith("set") && n == 1 &&
             methname.length() > 3) {
     propnames.put(methname.substring(3), meth);
   }
}
msg1898 (view) Author: ayesha (ayeshaiqbal) Date: 2007-10-01.04:04:41
Hi David ,
   I have attached your patch in the patches list . There are really no formalities required to contribute to jython . In the future you can attach your patches in the patch list . See http://wiki.python.org/jython/PatchGuidelines for guidelines on submitting patches. Make sure you use spaces instead of tabs in your patch.Also you can subscribe to the jython-dev mailing list and participate in our discussions . You can also join the #jython channel at irc.freenode.net and chat with the other jython developers.

Cheers,
Ayesha
msg1899 (view) Author: Charlie Groves (cgroves) Date: 2007-10-06.04:30:42
I can't actually get this to fail as you describe.  I've created several variations on classes with get, set and is methods, and they've always been callable.  We incorrectly add the empty string to __dict__ which is a problem I'm going to fix, but I'd like to be able to reproduce what you noticed.  Could you attach some Java code that produces the problem?
msg1900 (view) Author: David Little (derbylittle) Date: 2007-10-06.13:59:41
The error occurs when you try to call the java method from jython.

So for instance, in java:

public class MyClass{
    private int myValue;
    public int set(int value){myValue=value;}
}

and in jython:

myObject = MyClass()
myObject.set(5)

The call to set will not work, because the method was never added to jython wrapper of the java class.
msg1901 (view) Author: Charlie Groves (cgroves) Date: 2007-10-06.17:27:28
I changed that code to the following so it would compile:

public class MyClass {

    private int myValue;

    public int set(int value) {
        myValue = value;
        return myValue;
    }
}

Here's the results in Jython:

Jython 2.2.1rc2 on java1.5.0_07
Type "copyright", "credits" or "license" for more information.
>>> import MyClass
>>> m = MyClass()
>>> m.set(7)
7
>>> 

If I change the return type to void, I can call it equally well.  I tried code like that and several other variations, and it's always callable.  Is there some difference in the actual code where you saw this fail?
msg1902 (view) Author: Charlie Groves (cgroves) Date: 2008-01-19.23:17:33
No response in 3 months.  Closing.
History
Date User Action Args
2007-09-06 18:54:30derbylittlecreate