diff options
author | Jan Rieke <rieke@subshell.com> | 2018-08-16 14:38:44 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2018-08-27 21:23:11 +0200 |
commit | f072827630bfbdc548bc739fa979308cc2c4d3fb (patch) | |
tree | 2726270fb01887e3a2cd2e0b0d93130b5f5f192b /src/core/lombok | |
parent | 3f4f9e65520c91df59abbb03cdd91574d75ea197 (diff) | |
download | lombok-f072827630bfbdc548bc739fa979308cc2c4d3fb.tar.gz lombok-f072827630bfbdc548bc739fa979308cc2c4d3fb.tar.bz2 lombok-f072827630bfbdc548bc739fa979308cc2c4d3fb.zip |
@SuperBuilder adapts @Builder.Default behavior from @Builder as #1347 is
fixed now (javac)
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleBuilder.java | 2 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSuperBuilder.java | 31 |
2 files changed, 15 insertions, 18 deletions
diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index edf6f2ae..63697691 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -611,7 +611,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName(buildName), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), thrownExceptions, body, null); } - public JCMethodDecl generateDefaultProvider(Name methodName, JavacNode fieldNode, List<JCTypeParameter> params) { + public static JCMethodDecl generateDefaultProvider(Name methodName, JavacNode fieldNode, List<JCTypeParameter> params) { JavacTreeMaker maker = fieldNode.getTreeMaker(); JCVariableDecl field = (JCVariableDecl) fieldNode.get(); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index 38fec2f6..c00bd66d 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -33,12 +33,12 @@ import com.sun.tools.javac.code.BoundKind; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; +import com.sun.tools.javac.tree.JCTree.JCAssign; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCIdent; -import com.sun.tools.javac.tree.JCTree.JCIf; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.JCTree.JCModifiers; @@ -83,6 +83,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { JCExpression type; Name rawName; Name name; + Name nameOfDefaultProvider; Name nameOfSetFlag; SingularData singularData; ObtainVia obtainVia; @@ -154,16 +155,12 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { } if (isDefault != null) { + bfd.nameOfDefaultProvider = tdParent.toName("$default$" + bfd.name); bfd.nameOfSetFlag = tdParent.toName(bfd.name + "$set"); - // The @Builder annotation removes the initializing expression on the field and moves - // it to a method called "$default$FIELDNAME". This method is then called upon building. - // We do NOT do this, because this is unexpected and may lead to bugs when using other - // constructors (see, e.g., issue #1347). - // Instead, we keep the init expression and only set a new value in the builder-based - // constructor if it was set in the builder. Drawback is that the init expression is - // always executed, even if it was unnecessary because its value is overwritten by the - // builder. - // TODO: Once the issue is resolved in @Builder, we can adapt the solution here. + bfd.nameOfSetFlag = tdParent.toName(bfd.name + "$set"); + JCMethodDecl md = HandleBuilder.generateDefaultProvider(bfd.nameOfDefaultProvider, fieldNode, td.typarams); + recursiveSetGeneratedBy(md, ast, annotationNode.getContext()); + if (md != null) injectMethod(tdParent, md); } addObtainVia(bfd, fieldNode); builderFields.add(bfd); @@ -427,17 +424,17 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { } else { rhs = maker.Select(maker.Ident(builderVariableName), bfd.rawName); } - JCFieldAccess thisX = maker.Select(maker.Ident(typeNode.toName("this")), bfd.rawName); + JCFieldAccess fieldInThis = maker.Select(maker.Ident(typeNode.toName("this")), bfd.rawName); - JCStatement assign = maker.Exec(maker.Assign(thisX, rhs)); + JCStatement assign = maker.Exec(maker.Assign(fieldInThis, rhs)); + statements.append(assign); - // In case of @Builder.Default, only set the value if it really was set in the builder. + // In case of @Builder.Default, set the value to the default if it was NOT set in the builder. if (bfd.nameOfSetFlag != null) { JCFieldAccess setField = maker.Select(maker.Ident(builderVariableName), bfd.nameOfSetFlag); - JCIf ifSet = maker.If(setField, assign, null); - statements.append(ifSet); - } else { - statements.append(assign); + fieldInThis = maker.Select(maker.Ident(typeNode.toName("this")), bfd.rawName); + JCAssign assignDefault = maker.Assign(fieldInThis, maker.Apply(typeParameterNames(maker, ((JCClassDecl) typeNode.get()).typarams), maker.Select(maker.Ident(((JCClassDecl) typeNode.get()).name), bfd.nameOfDefaultProvider), List.<JCExpression>nil())); + statements.append(maker.If(maker.Unary(CTC_NOT, setField), maker.Exec(assignDefault), null)); } } |