Search code examples
javaintellij-idealombok

Is IntelliJ IDEA generating a wrong getter method for aField?


I'm moving some old code to Lombok, and found some difference in the name of the generated getter methods in IDEA and Lombok.

For example, a field like:

private String aField;

In IDEA, a getter method will be generated like this (using IntelliJ Default template):

public String getaField() {
    return aField;
}

But in Lombok, @Data will add a getter method named

getAField()

Which one is correct? It's a bug or just a different choice?

I'm using IDEA 2023.1.5 community and Lombok 1.18.24.


Solution

  • What Intellij does (you can see it in Generate -> Getter -> ... in top right corner):

    #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
    

    That code is here: https://github.com/JetBrains/intellij-community/blob/2247153ca4c2073591785e1854e9a35f1b513010/platform/util/src/com/intellij/openapi/util/text/StringUtil.java#L981

    So your case is basically the first thing that happens in that method:

    if (s.length() > 1 && Character.isUpperCase(s.charAt(1))) return s;

    Someone did it on purpose, that's for sure, so a different choice, not a bug.