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

Unable to generate AutoValue for a class with only boolean variables #994

Closed
kndarp opened this issue Feb 26, 2021 · 2 comments
Closed

Unable to generate AutoValue for a class with only boolean variables #994

kndarp opened this issue Feb 26, 2021 · 2 comments

Comments

@kndarp
Copy link

kndarp commented Feb 26, 2021

Using AutoValue 1.7.4

Failed to create an AutoValue class with builder for only boolean variable(s) in class. I observed this behaviour for 2 naming patterns - 'isXxx' and 'getXxx'. It works fine for 'xxx'.

Also, this does't fail when there is another variable of another type present in the class.

Failing code

@AutoValue
public abstract class AutoValueFails {
  
  AutoValueFails() {}

  public static Builder builder() {
    return new AutoValue_AutoValueFails.Builder();
  }

  public abstract boolean isBoolean();


  @AutoValue.Builder
  public abstract static class Builder {
    Builder() {}
        public abstract Builder isBoolean(final boolean value);
    }
}

Error :

Method does not correspond to a property of AutoValueFails
    public abstract Builder isBoolean(final boolean value);

Passing Code

@AutoValue
public abstract class AutoValuePasses {
  
  AutoValuePasses() {}

  public static Builder builder() {
    return new AutoValue_AutoValuePasses.Builder();
  }

  public abstract boolean isBoolean();
  public abstract String aString();


  @AutoValue.Builder
  public abstract static class Builder {
    Builder() {}
        public abstract Builder isBoolean(final boolean value);
    }
   Builder() {}
        public abstract Builder aString(final String value);
    }

}
@kndarp
Copy link
Author

kndarp commented Feb 26, 2021

@ruchirsachdeva

@eamonnmcmanus
Copy link
Member

The documentation says:

Some developers prefer to name their accessors with a get- or is- prefix, but would prefer that only the "bare" property name be used in toString and for the generated constructor's parameter names.

AutoValue will do exactly this, but only if you are using these prefixes consistently. In that case, it infers your intended property name by first stripping the get- or is- prefix, then adjusting the case of what remains as specified by Introspector.decapitalize.

That's what you're seeing here. In the first example, all of the properties (all one of them) look like isFoo, so they are assumed to follow the JavaBeans conventions. The single property method in AutoValueFails is called isBoolean() so the property is called boolean and it must be set by a builder method setBoolean(boolean x) or just boolean(boolean x) (ignoring for now that boolean is a keyword). If you add a second property method aString() then it is no longer the case that all the methods follow the JavaBeans conventions, so the properties are now isBoolean and aString and they are set by methods setIsBoolean(boolean x) and setAString(String x) or isBoolean(boolean x) and aString(String x).

This is admittedly a bit tricky. AutoValue does contain code that attempts to detect when someone runs into this problem but unfortunately you must not have triggered it in your experiments.

If you can think of a documentation change that would have made it easier for you to find this then we would certainly appreciate that.

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

2 participants