I have to port a lot of code from java6 to java7. Java6 has no problem with setters, returning values. So this example code:
public class Main {
public static class MyDummy {
private String payload;
public String getPayload() {
return payload;
}
public boolean setPayload(String payload) {
this.payload = payload;
return true;
}
}
public static void main(String[] args) {
System.out.println("JAVA: " + System.getProperty("java.version") + " (" + System.getProperty("java.vm.name") + ")");
MyDummy d = new MyDummy();
d.setPayload("init");
System.out.println("Value before: " + d.getPayload());
try {
PropertyDescriptor pd = new PropertyDescriptor("payload", MyDummy.class);
pd.getWriteMethod().invoke(d, "hello introspection");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
System.out.println("Value after: " + d.getPayload());
}
}
results awaited output at java 6:
JAVA: 1.6.0_24 (OpenJDK 64-Bit Server VM)
Value before: init
Value after: hello introspection
Unfortunately (for me) at java 7 output is:
JAVA: 1.7.0_06 (Java HotSpot(TM) 64-Bit Server VM)
Value before: init
Exception: java.beans.IntrospectionException: Method not found: setPayload
Value after: init
So introspection does not work anymore for setters, that return anything but void. My questions is what shall i do? The firs idea is to wrap setter in void-setter. Like this:
public void setPayload(String payload) {
setPayloadImpl(payload); // ignoring return value
}
public boolean setPayloadImpl(String payload) {
this.payload = payload;
return true;
}
But this solutioin forces to change the setter-name(overload methods by retrun type is not possible) and bloat the source code whith such setter-wrappers. Could you suggest a better solution?
P.S. Strange that it does not mentioned in java7 compatibility list.