Community
Participate
Working Groups
Build Identifier: 20100218-1602 The property for "Local variable declaration hides another field or variable" has an option that allows constructors and setter methods to be not included. We are also now using a lot of fluent setters having the withXXX() signature as they are different than the bean setXXX() signature. It would be good to have these also considered and ignored based on this option selection. Reproducible: Always Steps to Reproduce: Just need a object with a fluent withXXX() version of a setter and a variable to the method named same as the class attribute.
I think it'll be a better approach to just let the user customize what kind of prefixes he wants to be recognized for a method which is to be excluded from the "variable hiding" warning, since in the future someone might start using some other prefixes such as 'put'. Olivier, what do you think?
Yes, we could investigate making these prefixes configurable.
I'll investigate
Since the withXXX() convention is not (yet) widely used (is there even a spec for this anywhere?), we shouldn't hard-code this into the compiler. The existing option is about constructors and setters, but if I understood the "fluent setters" correctly, they are not really setters but they return a different object than 'this'. So it would have to be a new option. But also for that, we're too early at this time.
The convention we are using is that withXXX is simply a setter that returns "this", which allows the user of the POJO to to do method chaining within the construction. There is no 'spec' for this, but it is a pretty common pattern that people are stating to apply (just google for java fluent setters) A separate option that allowed the definition of method prefix(es) to ignore for the field hiding would be a fine solution. As is, we cannot use this warning at all because of these fluent with setters are all flagged. Such an option would allow for project conventions to be accounted for.
Sorry, we can't add support or a compiler option for every possible internal convention. The only thing I could agree on would be an implicit notion of "setter" that is not based on the JavaBeans naming convention but on the structure of the method. In addition to the current notion of "setter", the compiler would not generate a problem for the local variable fieldName in a method that matches this template: any_modifiers ThisType any_method_name (FieldType fieldName) { this.fieldName = fieldName; return this; }
I didn't know how this was done internally, but basing this on the concept of setter and method structure as you suggest would be perfect. I didn't realize that would be doable in that manner. Thanks for your time and input.