Hi Colin!
Sorry about the default package issue. FreeBuilder cannot work in the default package as it needs to make cross-class references, but the error message definitely needs fixing. This exact issue was raised a few days ago: https://github.com/google/FreeBuilder/issues/85
Unfortunately, Builder inheritance is not currently supported in FreeBuilder. It's on the long-term agenda, but that won't be much use to you right now! One option you have is to use composition rather than inheritance: that is, add a getter method returning a BaseModelParameters instance rather than inheriting from BaseModelParameters. Some argue that this is always the correct approach, though I personally disagree.
If you still prefer inheritance, then consider making an interface with a default setB implementation that all your ModelParameters builders can implement:
//@FreeBuilder <-- not used any more
public interface BaseModelParameters {
...
interface BaseBuilder<B extends BaseBuilder<B>> {
B setA(String a);
default B setB(String b) {
return setA("transform from " + b);
}
}
}
@FreeBuilder
public interface ConcreteModelParameters {
String getD();
class Builder extends ConcreteModelParameters_Builder
implements BaseBuilder<Builder> {}
}
If you want to know what FreeBuilder is generating, you should be able to read the generated source code. Try looking in the "target" directory if you're using a command-line build tool like maven. If you're using Eclipse, you'll need to configure the generated source directory (Project > Properties > Java Compiler > Annotation Processing) to not start with a period, as otherwise Eclipse hides it by default.
Hi Chris,Thanks for your tool. I tried it out in my project, here is some feedback.1. There is a bug, the builder interface can not live under default package, compile error.2. In my use case, there is a list of builders.I have a BaseBuilder containing some fields, one field A is getting from another field B.In the code without using FreeBuilder, in the setting method of B, I do some transform of B, and set the value of A.Then there are a list of Builders which are subclasses of BaseBuilder, they have their own fields.Because the override method on BaseBuilder can not be inherited to SubBuilders, I am using the feature of default method in JAVA 8 to achieve it.Here is the code.I don't think move the code in getB() to an override method in Builder is good, since interface extends it can not use it.Maybe you have better thoughts to do it.@FreeBuilder
public interface BaseModelParameters {
String getA();
default String getB() {
return "transform from " + getA();
}
Optional<Boolean> getC();
class Builder extends BaseModelParameters_Builder {}
}@FreeBuilder
public interface ConcreteModelParameters extends BaseModelParameters {
String getD();
class Builder extends ConcreteModelParameters_Builder {}
}Would you be able to let me know how to debug the source code? This project is different, there is not main() method.Thanks,Colin