playframework / play-enhancer

14 stars 9 forks source link

PropertiesEnhancer doesn't work when multiple setter exists #2

Closed jroper closed 10 years ago

jroper commented 10 years ago

From https://github.com/playframework/playframework/issues/824

A simple Java class in a Play project fails to compile (DuplicateMemberException) due to the way sbt-link's PropertiesEnhancer is implemented:

class Test { public int ByteBuffer value; // ... public byte [] getValue() { return value.array(); } public Test setValue(byte [] value) { return setValue(ByteBuffer.wrap(value)); } public Test setValue(ByteBuffer buffer) { this.value = value; return this; } } This kind of Java code gets automatically generated by Apache Thrift for instance. It's not an option to rewrite the class in question and it's a valid Java code anyway.

After investigating the method PropertiesEnhancer.generateAccessors() I found a way to solve the issue by explicitly asking for a setter with one parameter of the same type as the property being "enhanced":

line 97: CtMethod ctMethod = ctClass.getDeclaredMethod(setter, new CtClass [] { ctField.getType() });

instead of (currently):

line 97: CtMethod ctMethod = ctClass.getDeclaredMethod(setter);

It's quite wrong to assume that only one "setXyz" method exist in a given class. It solves the compilation error and I haven't noticed any drawbacks so far. Though I am not 100% sure what is the purpose of the property enhancer in the Play framework so I can't guarantee that this fix won't break other use cases.

Cheers.