3

Is this possible? Or is the only solution the builder? Having a class with 10 fields would mean have to duplicate the 10 fields in the builder so that AutoValue works. Or via a manual written create function? Or am I missing something?

I just want to create an object of my AutoValue class outside of the package...

Here's a short example:

@AutoValue
public abstract class Data
{
    // Can something like this be auto generated????
    public static Data create(String field1, String field2, ...)
    {
        return new AutoValue_Data(field1, field2, ...);
    }

    public abstract String field1();
    public abstract String field2();
    ...

    @AutoValue.Builder
    public abstract static class Builder {
        // Or can I tell the builder to create setters for ALL fields 
        // automatically instead of having to declare them one by one?
        public abstract Builder setField1(String field1);
        public abstract Builder setField2(String field2);
        ...
        public abstract Data build();
    }
}
prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

0

The create() method is just a wrapper around the constructor to hide implementation details like the class AutoValue_Data itself. If you just don't want to write everything yourself, you can find a plugin for Android Studio for creating these methods automatically.

tynn
  • 38,113
  • 8
  • 108
  • 143