Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate getters/setters compliant with JavaBean Spec and common IDEs #1095

Open
hohwille opened this issue Jan 10, 2020 · 1 comment
Open

Comments

@hohwille
Copy link
Member

We have detected a very strange edge-case with getter/setter generation if the second character of a field name is upper-case.
Example:

public class MyBean {
  private String aBc;
}

If you generate getters and setters with CobiGen you will get getABc and setABc what makes sense to me. However, IDEs like Eclipse or IntelliJ will instead generate getaBc and setaBc in this case. What seems like a bug in these IDEs has actually been implemented on purpose as a feature.

The actual problem is derived from the fact that Java Beans specification wanted to support UPPERCASE field names for abbreviations such as XML so that getXML() would make the java.beans.Introspector to derrive the field name XML instead of xML in decapitalize method (see links below). However, the implementation in java.beans.Introspector was messed and only considers the second character (see links below). Therefore getABc() would derrive to the field name ABc but the field name was actually aBc in our example.

Of course you could also argue that the field name of our example could have also initially been ABc. However, Java conventions suggests to start field names with a lower case letter. Then you could also argue that a field should never be named XML and this excuse in the Java Bean Specification does not make any sense at all. To be honest I have to admit that you are perfectly right.
However, java.beans.Introspector has been released decades ago and is unlikely to ever change. From that mistake common IDEs have implemented this bug as a feature into their products. So even though it is completely sick and nonsense we can and should not fight against JDK, Eclipse and IntelliJ if they all belive in the same thing.

Anyhow, what might be considered is that most people are not aware of this nonsense and most open-source bean introspection frameworks not based on java.beans.Introspector will therefore not contain this "bug" as a "feature". In general we should avoid naming fields such that the second character is capitalized to entirely get around this problem. Java is messed up in this regard.

Sources:

@jdiazgon
Copy link
Member

So even though it is completely sick and nonsense we can and should not fight against JDK, Eclipse and IntelliJ if they all belive in the same thing.

I agree with your point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants