ravij2gm / jannocessor

Automatically exported from code.google.com/p/jannocessor
Other
0 stars 0 forks source link

Methods.getter() makes incorrect getter name for booleans #23

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
For boolean fields Methods.getter() returns method with name that starts with 
"get" instead of "is". 

For example, for

private boolean disabled;

getter and setter should be:

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

Original issue reported on code.google.com by ora...@gmail.com on 29 Mar 2013 at 10:27

GoogleCodeExporter commented 8 years ago
I can suggest to use something like the following to create getter and setter 
method names in Methods.getter(field) and Methods.setter(field). It matches to 
getter / setter names that are generated by Intellij Idea. 

    public static String getterName(JavaField field) {
        if (field.getType().getKind() != JavaTypeKind.BOOLEAN) {
            return field.getName().copy().insertPart(0, "get").toString();
        }
        final String firstPart = field.getName().getParts()[0];
        if (firstPart.equals("is")) {
            return field.getName().toString();
        }
        return field.getName().copy().insertPart(0, "is").toString();
    }

    public static String setterName(JavaField field) {
        if (field.getType().getKind() == JavaTypeKind.BOOLEAN && field.getName().getParts()[0].equals("is")) {
            return field.getName().copy().replacePart(0, "set").toString();
        }
        return field.getName().copy().insertPart(0, "set").toString();
    }

Original comment by ora...@gmail.com on 1 Apr 2013 at 11:32