GitHub user feshbach opened a pull request:
https://github.com/apache/cayenne/pull/87
Fix BeanAccessors for Classes with Complex Inheritance
In certain cases, simply adding an interface to a class with no other
changes can break `PropertyUtils.setProperty`.
For example, consider a HasName interface and a Person entity with a name
attribute:
```java
public interface HasName {
CharSequence getName();
}
```
```java
public class _Person extends CayenneDataObject {
public void setName(String name) {
writeProperty(NAME_KEY, name);
}
public String getName() {
return (String) readProperty(NAME_KEY);
}
}
```
```java
public class Person extends _Person implements HasName {
}
```
My only change from the generated classes is to make `Person` implement
`HasName`. Now, when I call `PropertyUtils.setProperty(person, Person.NAME_KEY,
newName)`, I get a `PropertyException: Property "name" is not writable`.
What's happening is that the compiler is generating a synthetic, no-arg
method in Person.class named "getName" that returns a CharSequence. Since
that's the only no-arg method named "getName" in the class, that's the method
returned by `Class.getMethod`. (It finds a method in the class and so never
checks the super class, per the documentation.) When BeanAccessor looks for the
write method, it looks for `setName(CharSequence)` instead of `setName(String)`
and comes up empty.
My change is to iterate through the public methods manually and return the
read method with the most specific return type, then find a write method that's
compatible with it.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/feshbach/cayenne beanAccessorFix
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/cayenne/pull/87.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #87
----
commit bb0c8c777404bd177c9601a5fae2341ff4ffbde6
Author: David Feshbach <[email protected]>
Date: 2016-02-25T22:26:24Z
Add failing unit test for BeanAccessors for classes with complex inheritance
commit 7ff0b873d791e0e71c2187bac6758b7137bdbb42
Author: David Feshbach <[email protected]>
Date: 2016-02-25T22:28:03Z
Fix some issues in BeanAccessor/PropertyUtils for classes with complex
inheritance
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---