--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nice to see other people here who had the same idea like me. :) I'd love to have a hook in the generated build method like @Builder(preBuild="myFunctionName") to have a chance to avoid illegal states.
Stringly typed refs to methods are extremely ugly, it's a last resort, and this use case is not nearly important enough to warrant stooping _that_ low. If anything, there'd be a @Builder.Prebuild kind of method.More to the point, what does this actually mean? Please show me what lombok would desugar it to, because I don't really get it. You are entirely free to make your own builder class and leave it blank (letting lombok fill in all the fields and methods), except for the build() method, which does all your checks and then manually calls the 'target' (static method or constructor). This is some manual busywork, but it's not possible for a colleague to add a method to the constructor/static method you are putting @Builder on without immediately running into a compile-time error.
@Builder
public class MyClass {
@Mandatory private long longField;
@Mandatory private String stringField;
private int intField;
private char charField;
}
public class MyClass {
private final long longField;
private final String stringField;
private final int intField;
private final char charField;
private MyClass(long longField, String stringField, int intField, char charField) {
this.longField = longField;
this.stringField = stringField;
this.intField = intField;
this.charField = charField;
}
public long getLongField() {
return longField;
}
public String getStringField() {
return stringField;
}
public int getIntField() {
return intField;
}
public int getCharField() {
return charField;
}
public interface LongFieldBuilder {
StringFieldBuilder longField(long longField);
}
public interface StringFieldBuilder {
OptionalsBuilder stringField(String stringField);
}
public interface OptionalsBuilder {
Builder intField(int intField);
Builder charField(char charField);
}
public static LongFieldBuilder myClass() {
return new Builder();
}
private static class Builder implements LongFieldBuilder, StringFieldBuilder, OptionalsBuilder {
private long longField;
private String stringField;
private int intField;
private char charField;
@Override
public StringFieldBuilder longField(long longField) {
this.longField = longField;
return this;
}
@Override
public OptionalsBuilder stringField(String stringField) {
this.stringField = stringField;
return this;
}
@Override
public Builder intField(int intField) {
this.intField = intField;
return this;
}
@Override
public Builder charField(char charField) {
this.charField = charField;
return this;
}
public MyClass build() {
return new MyClass(longField, stringField, intField, charField);
}
}
}
I see Project Lombok as ideal to generate builders which force the setting of fields at compilation time, instead of throwing exceptions. Throwing exceptions means that boilerplate code is still required to handle them.
I would expect to write something like the following:
@Builder
public class MyClass {
@Mandatory private long longField;
@Mandatory private String stringField;
private int intField;
private final char charField;
}
Which Lombok would convert in something similar to the code below, and would be callable with e.g. myClass().longField(1L).stringField("blah").charField('c').build(). Failing to call e.g. stringField() would make compilation fail, but e.g. not calling intField() is permitted.
(Implementation-wise, if you get rid of the final modifier on MyClass fields and make the builder class non-static, it is further possible for the builder to directly assign values to them instead of declaring fields inside the builder itself (and the all-args constructor could be deleted).)
Le mardi 19 novembre 2013 21:16:01 UTC+2, Reinier Zwitserloot a écrit :
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Project Lombok" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/project-lombok/TK0Ud8ojcmc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to project-lombo...@googlegroups.com.