|
| 1 | +# An opinionated Java Inner Builder Generator |
| 2 | + |
| 3 | +This is a simple Java Inner Builder Generator IntelliJ plugin that generates a |
| 4 | +builder for a given class. The builder is an inner class of the class it is building. |
| 5 | + |
| 6 | +Automatically detects the target class access modifier |
| 7 | + |
| 8 | +```java |
| 9 | +public class Person { |
| 10 | + private static final Logger logger = Logger.getLogger("test"); |
| 11 | + |
| 12 | + private final String name = "1"; |
| 13 | + private final int age; |
| 14 | + private String lastName; |
| 15 | + |
| 16 | + private Person(Builder builder) { |
| 17 | + age = builder.age; |
| 18 | + lastName = builder.lastName; |
| 19 | + } |
| 20 | + |
| 21 | + public static Builder builder() { |
| 22 | + return new Builder(); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + public static final class Builder { |
| 27 | + private int age; |
| 28 | + private String lastName; |
| 29 | + |
| 30 | + public Builder age(int age) { |
| 31 | + this.age = age; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + public Builder lastName(String lastName) { |
| 36 | + this.lastName = lastName; |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + public Person build() { |
| 41 | + return new Person(this); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +Supports Java record classes |
| 49 | + |
| 50 | +```java |
| 51 | +record Address(String street, String city, String state, String country) { |
| 52 | + static String m = "ME"; |
| 53 | + |
| 54 | + public static Builder builder() { |
| 55 | + return new Builder(); |
| 56 | + } |
| 57 | + |
| 58 | + //optional toBuilder method |
| 59 | + public Builder toBuilder() { |
| 60 | + Builder builder = new Builder(); |
| 61 | + builder.street = this.street; |
| 62 | + builder.city = this.city; |
| 63 | + builder.state = this.state; |
| 64 | + builder.country = this.country; |
| 65 | + return builder; |
| 66 | + } |
| 67 | + |
| 68 | + public static final class Builder { |
| 69 | + private String street; |
| 70 | + private String city; |
| 71 | + private String state; |
| 72 | + private String country; |
| 73 | + |
| 74 | + public Builder street(String street) { |
| 75 | + this.street = street; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + public Builder city(String city) { |
| 80 | + this.city = city; |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + public Builder state(String state) { |
| 85 | + this.state = state; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + public Builder country(String country) { |
| 90 | + this.country = country; |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + //follows the containing class visibility |
| 95 | + Address build() { |
| 96 | + return new Address(street, city, state, country); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments