Skip to content

Commit

Permalink
Check for parent beans of the same name to fill in missing getters or…
Browse files Browse the repository at this point in the history
… setters when adding beans for

a type.  Fixes bug #1132.  Thanks to garyh for tracking it down and supplying a test case.
  • Loading branch information
groves committed Jan 16, 2009
1 parent bc2d54b commit 9bab462
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Lib/test/test_java_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ def test_array_coercion(self):
self.assertEquals("OtherSubVisible[]", c.takeArray([OtherSubVisible()]))
self.assertEquals("SubVisible[]", c.takeArray([SubVisible()]))

def test_iterable_coercion(self):
def simple_gen():
yield 1
yield 2
yield 3
self.assertEquals(6, Coercions.takeIterable(simple_gen()))
self.assertEquals(True, Coercions.takeBoolIterable(simple_gen()))


def test_class_coercion(self):
c = Coercions()
from java.util import Hashtable, HashMap
Expand Down
25 changes: 24 additions & 1 deletion src/org/python/core/PyJavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,31 @@ && lookup(nmethname) == null) {
prop.field = ((PyReflectedField)prev).field;
}
}

// If one of our superclasses has something defined for this name, check if its a bean
// property, and if so, try to fill in any gaps in our property from there
PyObject superForName = lookup(prop.__name__);
if (superForName instanceof PyBeanProperty) {
PyBeanProperty superProp = ((PyBeanProperty)superForName);
// If it has a set method and we don't, take it regardless. If the types don't line
// up, it'll be rejected below
if (prop.setMethod == null) {
prop.setMethod = superProp.setMethod;
} else if (superProp.myType == prop.setMethod.getParameterTypes()[0]) {
// Otherwise, we must not have a get method. Only take a get method if the type
// on it agrees with the set method we already have. The bean on this type
// overrides a conflicting one o the parent
prop.getMethod = superProp.getMethod;
prop.myType = superProp.myType;
}

if (prop.field == null) {
// If the parent bean is hiding a static field, we need it as well.
prop.field = superProp.field;
}
}
// If the return types on the set and get methods for a property don't agree, the get
// get method takes precedence
// method takes precedence
if (prop.getMethod != null && prop.setMethod != null
&& prop.myType != prop.setMethod.getParameterTypes()[0]) {
prop.setMethod = null;
Expand Down
12 changes: 12 additions & 0 deletions tests/java/org/python/tests/Child.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.python.tests;

public class Child extends Parent {

public void setId(int newId) {
this.id = newId;
}

public String getValue() {
return value;
}
}
16 changes: 16 additions & 0 deletions tests/java/org/python/tests/Parent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.python.tests;

public class Parent {

protected String value = "blah";

protected int id = 7;

public int getId() {
return id;
}

public void setValue(String value) {
this.value = value;
}
}

0 comments on commit 9bab462

Please sign in to comment.