Community
Participate
Working Groups
It is common to provide a getXXX() API to make xxx accessible, but to bypass that API internally by direct access to xxx. A derived class can override getXXX() but fail to override as might be expected. The above scenario could be diagnosed with a warning message, which disappears once the getXXX method is declared final thereby clarifying that the API is solely for read-access.
"A derived class can override getXXX() but fail to override as might be expected." Sorry, I don't understand that sentence. What should "override as expected" mean?
A class such as public class AmiguousGet { private String name; ... public String getName() { return name; } ... private void doSomething() { .... = name ... } } appears to allow getName() be overridden by perhaps @Override public String getName() { return super.getName().toUpperCase(); } but this override fails to affect the bypass usage in doSomething(), so the override does not work as might be expected. I am suggesting that the use of name in doSomething() provoke a warning for which quick fixes could be 'make getName() final' or 'replace direct field access by getName()' or 'add @nooverride to getName()'.
(In reply to comment #2) >[..] I don't think it is the compiler's business to oversee how a getter method is used. What if the user wanted the getName() method to get the field in a particular format, and at some place he just wants to get the field in the original format? We end up with an irritating, and not useful warning in such a case. Also, this fails if I don't name my getter method as getXXX() and use some other naming convention, such as returnXXX()
I agree with Ayush. There is no notion of getter/setter in the spec and JDT Core also has no knowledge about this except for suggesting the getter/setter names.
This was triggered by Bug 329980. I agree that an over-nannyish policing of get/set could be very irritating. On the other hand there is a reasonable requirement that the associated API be clear to the users, and checked on behalf of the developers.
Verified for 3.7M4