mkarneim / pojobuilder

A Java Code Generator for Pojo Builders
Other
334 stars 44 forks source link

Support for nested classes? #117

Closed atomgomba closed 7 years ago

atomgomba commented 8 years ago

Is there a plan to add support for nested classes? Sometimes a POJO is used to inject arguments into an object instead of having a constructor/factory method with a long list of arguments. So for example being able to organize code like this could be handy:

public class ItemSelectDialog {
    @GeneratePojoBuilder
    public class Options {
        public String titleText;
        public String positiveButtonText;
        public String negativeButtonText;
        public String neutralButtonText;
        public boolean isModal;
        // ...and so on
    }

    public ItemSelectDialog(Options options) {
        // use the built POJO to initialize the dialog
    }

    // rest of the code...
}

At the moment I have to keep the settings POJO in a separate file with a name like ItemSelectDialogOptions because I cannot use the external class as namespace.

mkarneim commented 8 years ago

PB already supports inner classes. Just make it static.

atomgomba commented 8 years ago

Yes, it's strange. The builder class gets created for the inner class at the right place, like I can open and examine the generated .java file in my IDE. However I keep getting a gradle error upon build using a configuration like this:

@Parcel
public class ContactListModel {
    @Parcel
    @GeneratePojoBuilder
    public static class ContactItem {
        protected String name;
        protected String email;
    }

    protected List<ContactItem> mItems = new ArrayList<>();

    public ContactListModel(List<Response> items) {
        for (Response response : items) {
            ContactItem newItem = new ContactItemBuilder()
                    .withName(response.getFullName())
                    .withEmail(response.getCredentials().getEmail())
                    .build();
            mItems.add(newItem);
        }
    }
}

Compiling this results in a

Error:(x, y) error: cannot find symbol class ContactItemBuilder

It seems odd, since the generated file is there and the error doesn't occur when I put the inner class into it's own file. Seemingly this must be related to my environment, not PojoBuilder.