History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEA-18354
Type: New Feature New Feature
Status: Open Open
Assignee: Maxim Shafirov
Reporter: Gibson
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
IDEA: Feedback

Code->Generate->Builder would be nice

Created: 06 Jun 08 20:18   Updated: 07 Jun 08 12:58
Component/s: Editor. Editing Text

Build: 7,860
Severity: Medium


 Description  « Hide
As described in Design Patterns & Effective Java, it would be great to have some help from Idea.
E.g. for a class like this
public class ImmutableObject {
    @NotNull
    private final String param1;
    @NotNull
    private final String param2;
    @Nullable
    private final String param3;
    @Nullable
    private final String param1;
}

Instead of doing Code->Generate->Constructor, I would like to be able to do Code->Generate->Builder and get the following class definition

@SuppressWarnings ({"InnerClassFieldHidesOuterClassField"})
public class ImmutableObject {
    @NotNull
    private final String param1;
    @NotNull
    private final String param2;
    @Nullable
    private final String param3;
    @Nullable
    private final String param4;

    public class Builder {
        @NotNull
        private final String param1;
        @NotNull
        private final String param2;
        @Nullable
        private String param3;
        @Nullable
        private String param4;
        public Builder (String param1, String param2) {
            this.param1 = param1;
            this.param2 = param2;
        }
        public Builder param3  (String param3) {
            this.param3 = param3;
            return this;
        }
        public Builder param4 (String param4) {
            this.param4 = param4;
            return this;
        }

        public ImmutableObject build () {
            return new ImmutableObject (this);
        }
    }

    private ImmutableObject (Builder builder) {
        this.param1 = builder.param1;
        this.param2 = builder.param2;
        this.param3 = builder.param3;
        this.param4 = builder.param4;
    }
}

Note that the parameters' optionality (or not) is assumed in this case from the @NotNull annotations, but if the class isn't annotated the user should be prompted for which ones are optional (like the equals/hashcode wizard).



 All   Comments   Work Log   Change History      Sort Order:
There are no comments yet on this issue.