From a17d1fc13db231bd84996d7f8fd68d5514f5aab8 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 17 Jul 2015 00:09:27 +0200 Subject: eclipse impl for @Builder(toBuilder=true) --- .../resource/before/BuilderWithToBuilder.java | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/transform/resource/before/BuilderWithToBuilder.java (limited to 'test') diff --git a/test/transform/resource/before/BuilderWithToBuilder.java b/test/transform/resource/before/BuilderWithToBuilder.java new file mode 100644 index 00000000..98b7cebc --- /dev/null +++ b/test/transform/resource/before/BuilderWithToBuilder.java @@ -0,0 +1,36 @@ +import java.util.List; +import lombok.Builder; + +@Builder(toBuilder = true) @lombok.experimental.Accessors(prefix = "m") +class BuilderWithToBuilder { + private String mOne, mTwo; + @Builder.ObtainVia(method = "rrr", isStatic = true) private T foo; + @lombok.Singular private List bars; + + public static K rrr(BuilderWithToBuilder x) { + return x.foo; + } +} + +@lombok.experimental.Accessors(prefix = "m") +class ConstructorWithToBuilder { + private String mOne, mTwo; + private T foo; + @lombok.Singular private List bars; + + @Builder(toBuilder = true) + public ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T bar) { + } +} + +@lombok.experimental.Accessors(prefix = "m") +class StaticWithToBuilder { + private String mOne, mTwo; + private T foo; + @lombok.Singular private List bars; + + @Builder(toBuilder = true) + public static StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { + return new StaticWithToBuilder(); + } +} -- cgit From e2289ef2007981ff285bc7221e89b99211598e27 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 17 Jul 2015 10:48:10 +0200 Subject: added testcase for ecj version of @Builder(toBuilder=true) --- .../resource/after-ecj/BuilderWithToBuilder.java | 150 +++++++++++++++++++++ .../resource/before/BuilderWithToBuilder.java | 13 +- 2 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 test/transform/resource/after-ecj/BuilderWithToBuilder.java (limited to 'test') diff --git a/test/transform/resource/after-ecj/BuilderWithToBuilder.java b/test/transform/resource/after-ecj/BuilderWithToBuilder.java new file mode 100644 index 00000000..423865ff --- /dev/null +++ b/test/transform/resource/after-ecj/BuilderWithToBuilder.java @@ -0,0 +1,150 @@ +import java.util.List; +import lombok.Builder; +@Builder(toBuilder = true) @lombok.experimental.Accessors(prefix = "m") class BuilderWithToBuilder { + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") class BuilderWithToBuilderBuilder { + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") String one; + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") String two; + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") T foo; + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.util.ArrayList bars; + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder() { + super(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder one(final String one) { + this.one = one; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder two(final String two) { + this.two = two; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder foo(final T foo) { + this.foo = foo; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder bar(T bar) { + if ((this.bars == null)) + this.bars = new java.util.ArrayList(); + this.bars.add(bar); + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder bars(java.util.Collection bars) { + if ((this.bars == null)) + this.bars = new java.util.ArrayList(); + this.bars.addAll(bars); + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilder build() { + java.util.List bars; + switch (((this.bars == null) ? 0 : this.bars.size())) { + case 0 : + bars = java.util.Collections.emptyList(); + break; + case 1 : + bars = java.util.Collections.singletonList(this.bars.get(0)); + break; + default : + bars = java.util.Collections.unmodifiableList(new java.util.ArrayList(this.bars)); + } + return new BuilderWithToBuilder(one, two, foo, bars); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() { + return (((((((("BuilderWithToBuilder.BuilderWithToBuilderBuilder(one=" + this.one) + ", two=") + this.two) + ", foo=") + this.foo) + ", bars=") + this.bars) + ")"); + } + } + private String mOne; + private String mTwo; + private @Builder.ObtainVia(method = "rrr",isStatic = true) T foo; + private @lombok.Singular List bars; + public static K rrr(BuilderWithToBuilder x) { + return x.foo; + } + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilder(final String one, final String two, final T foo, final List bars) { + super(); + this.mOne = one; + this.mTwo = two; + this.foo = foo; + this.bars = bars; + } + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder builder() { + return new BuilderWithToBuilderBuilder(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithToBuilderBuilder toBuilder() { + return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(this.bars); + } +} +@lombok.experimental.Accessors(prefix = "m") class ConstructorWithToBuilder { + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") class ConstructorWithToBuilderBuilder { + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") String mOne; + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") T bar; + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilderBuilder() { + super(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilderBuilder mOne(final String mOne) { + this.mOne = mOne; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilderBuilder bar(final T bar) { + this.bar = bar; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilder build() { + return new ConstructorWithToBuilder(mOne, bar); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() { + return (((("ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne) + ", bar=") + this.bar) + ")"); + } + } + private String mOne; + private String mTwo; + private T foo; + private @lombok.Singular List bars; + public @Builder(toBuilder = true) ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T bar) { + super(); + } + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilderBuilder builder() { + return new ConstructorWithToBuilderBuilder(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") ConstructorWithToBuilderBuilder toBuilder() { + return new ConstructorWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + } +} +@lombok.experimental.Accessors(prefix = "m") class StaticWithToBuilder { + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") class StaticWithToBuilderBuilder { + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") String mOne; + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") Z bar; + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilderBuilder() { + super(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilderBuilder mOne(final String mOne) { + this.mOne = mOne; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilderBuilder bar(final Z bar) { + this.bar = bar; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilder build() { + return StaticWithToBuilder.test(mOne, bar); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() { + return (((("StaticWithToBuilder.StaticWithToBuilderBuilder(mOne=" + this.mOne) + ", bar=") + this.bar) + ")"); + } + } + private String mOne; + private String mTwo; + private T foo; + private K bar; + private @lombok.Singular List bars; + StaticWithToBuilder() { + super(); + } + public static @Builder(toBuilder = true) StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { + return new StaticWithToBuilder(); + } + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilderBuilder builder() { + return new StaticWithToBuilderBuilder(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") StaticWithToBuilderBuilder toBuilder() { + return new StaticWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + } +} diff --git a/test/transform/resource/before/BuilderWithToBuilder.java b/test/transform/resource/before/BuilderWithToBuilder.java index 98b7cebc..63e16ae8 100644 --- a/test/transform/resource/before/BuilderWithToBuilder.java +++ b/test/transform/resource/before/BuilderWithToBuilder.java @@ -1,36 +1,31 @@ import java.util.List; import lombok.Builder; - @Builder(toBuilder = true) @lombok.experimental.Accessors(prefix = "m") class BuilderWithToBuilder { private String mOne, mTwo; @Builder.ObtainVia(method = "rrr", isStatic = true) private T foo; @lombok.Singular private List bars; - public static K rrr(BuilderWithToBuilder x) { return x.foo; } } - @lombok.experimental.Accessors(prefix = "m") class ConstructorWithToBuilder { private String mOne, mTwo; private T foo; @lombok.Singular private List bars; - @Builder(toBuilder = true) public ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T bar) { } } - @lombok.experimental.Accessors(prefix = "m") -class StaticWithToBuilder { +class StaticWithToBuilder { private String mOne, mTwo; private T foo; + private K bar; @lombok.Singular private List bars; - @Builder(toBuilder = true) - public static StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { - return new StaticWithToBuilder(); + public static StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { + return new StaticWithToBuilder(); } } -- cgit From f38811779b9280bff7adebde64ed23b554a42dd9 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 20 Jul 2015 22:39:35 +0200 Subject: added javac impl of toBuilder along with test file. --- src/core/lombok/core/TypeLibrary.java | 2 +- .../lombok/eclipse/handlers/HandleBuilder.java | 16 +- src/core/lombok/javac/handlers/HandleBuilder.java | 261 ++++++++++++++++++--- .../after-delombok/BuilderWithToBuilder.java | 216 +++++++++++++++++ 4 files changed, 444 insertions(+), 51 deletions(-) create mode 100644 test/transform/resource/after-delombok/BuilderWithToBuilder.java (limited to 'test') diff --git a/src/core/lombok/core/TypeLibrary.java b/src/core/lombok/core/TypeLibrary.java index d4f5f2b0..cdaf7a70 100644 --- a/src/core/lombok/core/TypeLibrary.java +++ b/src/core/lombok/core/TypeLibrary.java @@ -84,7 +84,7 @@ public class TypeLibrary { int idx = fullyQualifiedTypeName.lastIndexOf('.'); if (idx == -1) throw new IllegalArgumentException( "Only fully qualified types are allowed (and stuff in the default package is not palatable to us either!)"); - String unqualified = dotBased.substring(idx + 1); + String unqualified = fullyQualifiedTypeName.substring(idx + 1); if (unqualifiedToQualifiedMap == null) throw new IllegalStateException("SingleType library"); unqualifiedToQualifiedMap.put(unqualified.replace("$", "."), dotBased); diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index c7ff714b..ee6dfd70 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -223,6 +223,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { char[][] pkg = null; if (md.returnType.dimensions() > 0) { annotationNode.addError(TO_BUILDER_NOT_SUPPORTED); + return; } if (md.returnType instanceof SingleTypeReference) { @@ -243,9 +244,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { return; } - EclipseNode selfType = parent; - while (selfType != null && selfType.getKind() != Kind.TYPE) selfType = selfType.up(); - if (selfType == null || !equals(selfType.getName(), token)) { + if (tdParent == null || !equals(tdParent.getName(), token)) { annotationNode.addError(TO_BUILDER_NOT_SUPPORTED); return; } @@ -274,12 +273,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { if (!Arrays.equals(((SingleTypeReference) tpOnRet[i]).token, onMethod.name)) continue; pos = i; } - if (pos == -1) { - annotationNode.addError("@Builder(toBuilder=true) requires that each type parameter on the static method is part of the typeargs of the return value. Type parameter " + new String(onMethod.name) + " is not part of the return type."); - return; - } - - if (tpOnType == null || tpOnType.length <= pos) { + if (pos == -1 || tpOnType == null || tpOnType.length <= pos) { annotationNode.addError("@Builder(toBuilder=true) requires that each type parameter on the static method is part of the typeargs of the return value. Type parameter " + new String(onMethod.name) + " is not part of the return type."); return; } @@ -388,8 +382,8 @@ public class HandleBuilder extends EclipseAnnotationHandler { if (constructorExists(builderType) == MemberExistsResult.NOT_EXISTS) { ConstructorDeclaration cd = HandleConstructor.createConstructor( - AccessLevel.PACKAGE, builderType, Collections.emptyList(), null, - annotationNode, Collections.emptyList()); + AccessLevel.PACKAGE, builderType, Collections.emptyList(), null, + annotationNode, Collections.emptyList()); if (cd != null) injectMethod(builderType, cd); } diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 7ef7b859..484a0b25 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -29,6 +29,7 @@ import org.mangosdk.spi.ProviderFor; 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.JCArrayTypeTree; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCExpression; @@ -49,6 +50,7 @@ import com.sun.tools.javac.util.Name; import lombok.AccessLevel; import lombok.Builder; +import lombok.Builder.ObtainVia; import lombok.ConfigurationKeys; import lombok.Singular; import lombok.core.AST.Kind; @@ -79,8 +81,11 @@ public class HandleBuilder extends JavacAnnotationHandler { private static class BuilderFieldData { JCExpression type; + Name rawName; Name name; SingularData singularData; + ObtainVia obtainVia; + JavacNode obtainViaNode; java.util.List createdFields = new ArrayList(); } @@ -95,6 +100,9 @@ public class HandleBuilder extends JavacAnnotationHandler { String builderMethodName = builderInstance.builderMethodName(); String buildMethodName = builderInstance.buildMethodName(); String builderClassName = builderInstance.builderClassName(); + String toBuilderMethodName = "toBuilder"; + boolean toBuilder = builderInstance.toBuilder(); + java.util.List typeArgsForToBuilder = null; if (builderMethodName == null) builderMethodName = "builder"; if (buildMethodName == null) buildMethodName = "build"; @@ -135,9 +143,11 @@ public class HandleBuilder extends JavacAnnotationHandler { // Value will only skip making a field final if it has an explicit @NonFinal annotation, so we check for that. if (fd.init != null && valuePresent && !hasAnnotation(NonFinal.class, fieldNode)) continue; BuilderFieldData bfd = new BuilderFieldData(); + bfd.rawName = fd.name; bfd.name = removePrefixFromField(fieldNode); bfd.type = fd.vartype; bfd.singularData = getSingularData(fieldNode); + addObtainVia(bfd, fieldNode); builderFields.add(bfd); allFields.append(fieldNode); } @@ -171,7 +181,8 @@ public class HandleBuilder extends JavacAnnotationHandler { annotationNode.addError("@Builder is only supported on types, constructors, and static methods."); return; } - returnType = jmd.restype; + JCExpression fullReturnType = jmd.restype; + returnType = fullReturnType; typeParams = jmd.typarams; thrownExceptions = jmd.thrown; nameOfStaticBuilderMethod = jmd.name; @@ -202,6 +213,70 @@ public class HandleBuilder extends JavacAnnotationHandler { builderClassName = td.name.toString() + "Builder"; } } + if (toBuilder) { + final String TO_BUILDER_NOT_SUPPORTED = "@Builder(toBuilder=true) is only supported if you return your own type."; + if (returnType instanceof JCArrayTypeTree) { + annotationNode.addError(TO_BUILDER_NOT_SUPPORTED); + return; + } + + Name simpleName; + String pkg; + List tpOnRet = List.nil(); + + if (fullReturnType instanceof JCTypeApply) { + tpOnRet = ((JCTypeApply) fullReturnType).arguments; + } + + if (returnType instanceof JCIdent) { + simpleName = ((JCIdent) returnType).name; + pkg = null; + } else if (returnType instanceof JCFieldAccess) { + JCFieldAccess jcfa = (JCFieldAccess) returnType; + simpleName = jcfa.name; + pkg = unpack(jcfa.selected); + if (pkg.startsWith("ERR:")) { + String err = pkg.substring(4, pkg.indexOf("__ERR__")); + annotationNode.addError(err); + return; + } + } else { + annotationNode.addError("Expected a (parameterized) type here instead of a " + returnType.getClass().getName()); + return; + } + + if (pkg != null && !parent.getPackageDeclaration().equals(pkg)) { + annotationNode.addError(TO_BUILDER_NOT_SUPPORTED); + return; + } + + if (!tdParent.getName().contentEquals(simpleName)) { + annotationNode.addError(TO_BUILDER_NOT_SUPPORTED); + return; + } + + List tpOnMethod = jmd.typarams; + List tpOnType = ((JCClassDecl) tdParent.get()).typarams; + typeArgsForToBuilder = new ArrayList(); + + for (JCTypeParameter tp : tpOnMethod) { + int pos = -1; + int idx = -1; + for (JCExpression tOnRet : tpOnRet) { + idx++; + if (!(tOnRet instanceof JCIdent)) continue; + if (((JCIdent) tOnRet).name != tp.name) continue; + pos = idx; + } + + if (pos == -1 || tpOnType.size() <= pos) { + annotationNode.addError("**" + returnType.getClass().toString()); +// annotationNode.addError("@Builder(toBuilder=true) requires that each type parameter on the static method is part of the typeargs of the return value. Type parameter " + tp.name + " is not part of the return type."); + return; + } + typeArgsForToBuilder.add(tpOnType.get(pos).name); + } + } } else { annotationNode.addError("@Builder is only supported on types, constructors, and static methods."); return; @@ -213,8 +288,10 @@ public class HandleBuilder extends JavacAnnotationHandler { BuilderFieldData bfd = new BuilderFieldData(); JCVariableDecl raw = (JCVariableDecl) param.get(); bfd.name = raw.name; + bfd.rawName = raw.name; bfd.type = raw.vartype; bfd.singularData = getSingularData(param); + addObtainVia(bfd, param); builderFields.add(bfd); } } @@ -245,6 +322,16 @@ public class HandleBuilder extends JavacAnnotationHandler { break; } } + if (bfd.obtainVia != null) { + if (bfd.obtainVia.field().isEmpty() == bfd.obtainVia.method().isEmpty()) { + bfd.obtainViaNode.addError("The syntax is either @ObtainVia(field = \"fieldName\") or @ObtainVia(method = \"methodName\")."); + return; + } + if (bfd.obtainVia.method().isEmpty() && bfd.obtainVia.isStatic()) { + bfd.obtainViaNode.addError("@ObtainVia(isStatic = true) is not valid unless 'method' has been set."); + return; + } + } } generateBuilderFields(builderType, builderFields, ast); @@ -285,9 +372,95 @@ public class HandleBuilder extends JavacAnnotationHandler { if (md != null) injectMethod(tdParent, md); } + if (toBuilder) { + switch (methodExists(toBuilderMethodName, tdParent, 0)) { + case EXISTS_BY_USER: + annotationNode.addWarning("Not generating toBuilder() as it already exists."); + return; + case NOT_EXISTS: + List tps = typeParams; + if (typeArgsForToBuilder != null) { + ListBuffer lb = new ListBuffer(); + JavacTreeMaker maker = tdParent.getTreeMaker(); + for (Name n : typeArgsForToBuilder) { + lb.append(maker.TypeParameter(n, List.nil())); + } + tps = lb.toList(); + } + JCMethodDecl md = generateToBuilderMethod(toBuilderMethodName, builderClassName, tdParent, tps, builderFields, fluent, ast); + if (md != null) injectMethod(tdParent, md); + } + } + recursiveSetGeneratedBy(builderType.get(), ast, annotationNode.getContext()); } + private static String unpack(JCExpression expr) { + StringBuilder sb = new StringBuilder(); + unpack(sb, expr); + return sb.toString(); + } + + private static void unpack(StringBuilder sb, JCExpression expr) { + if (expr instanceof JCIdent) { + sb.append(((JCIdent) expr).name.toString()); + return; + } + + if (expr instanceof JCFieldAccess) { + JCFieldAccess jcfa = (JCFieldAccess) expr; + unpack(sb, jcfa.selected); + sb.append(".").append(jcfa.name.toString()); + return; + } + + if (expr instanceof JCTypeApply) { + sb.setLength(0); + sb.append("ERR:"); + sb.append("@Builder(toBuilder=true) is not supported if returning a type with generics applied to an intermediate."); + sb.append("__ERR__"); + return; + } + + sb.setLength(0); + sb.append("ERR:"); + sb.append("Expected a type of some sort, not a " + expr.getClass().getName()); + sb.append("__ERR__"); + } + + private JCMethodDecl generateToBuilderMethod(String toBuilderMethodName, String builderClassName, JavacNode type, List typeParams, java.util.List builderFields, boolean fluent, JCAnnotation ast) { + // return new ThingieBuilder().setA(this.a).setB(this.b); + JavacTreeMaker maker = type.getTreeMaker(); + + ListBuffer typeArgs = new ListBuffer(); + for (JCTypeParameter typeParam : typeParams) { + typeArgs.append(maker.Ident(typeParam.name)); + } + + JCExpression call = maker.NewClass(null, List.nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.nil(), null); + JCExpression invoke = call; + for (BuilderFieldData bfd : builderFields) { + Name setterName = fluent ? bfd.name : type.toName(HandlerUtil.buildAccessorName("set", bfd.name.toString())); + JCExpression arg; + if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) { + arg = maker.Select(maker.Ident(type.toName("this")), bfd.obtainVia == null ? bfd.rawName : type.toName(bfd.obtainVia.field())); + } else { + if (bfd.obtainVia.isStatic()) { + JCExpression c = maker.Select(maker.Ident(type.toName(type.getName())), type.toName(bfd.obtainVia.method())); + arg = maker.Apply(List.nil(), c, List.of(maker.Ident(type.toName("this")))); + } else { + JCExpression c = maker.Select(maker.Ident(type.toName("this")), type.toName(bfd.obtainVia.method())); + arg = maker.Apply(List.nil(), c, List.nil()); + } + } + invoke = maker.Apply(List.nil(), maker.Select(invoke, setterName), List.of(arg)); + } + JCStatement statement = maker.Return(invoke); + + JCBlock body = maker.Block(0, List.of(statement)); + return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName(toBuilderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.nil(), List.nil(), List.nil(), body, null); + } + private JCMethodDecl generateCleanMethod(java.util.List builderFields, JavacNode type, JCTree source) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer statements = new ListBuffer(); @@ -449,6 +622,17 @@ public class HandleBuilder extends JavacAnnotationHandler { return injectType(tdParent, builder); } + private void addObtainVia(BuilderFieldData bfd, JavacNode node) { + for (JavacNode child : node.down()) { + if (!annotationTypeMatches(ObtainVia.class, child)) continue; + AnnotationValues ann = createAnnotation(ObtainVia.class, child); + bfd.obtainVia = ann.getInstance(); + bfd.obtainViaNode = child; + deleteAnnotationIfNeccessary(child, ObtainVia.class); + return; + } + } + /** * Returns the explicitly requested singular annotation on this node (field * or parameter), or null if there's no {@code @Singular} annotation on it. @@ -457,48 +641,47 @@ public class HandleBuilder extends JavacAnnotationHandler { */ private SingularData getSingularData(JavacNode node) { for (JavacNode child : node.down()) { - if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Singular.class, child)) { - Name pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((JCVariableDecl) node.get()).name; - AnnotationValues ann = createAnnotation(Singular.class, child); - deleteAnnotationIfNeccessary(child, Singular.class); - String explicitSingular = ann.getInstance().value(); - if (explicitSingular.isEmpty()) { - if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) { - node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled."); + if (!annotationTypeMatches(Singular.class, child)) continue; + Name pluralName = node.getKind() == Kind.FIELD ? removePrefixFromField(node) : ((JCVariableDecl) node.get()).name; + AnnotationValues ann = createAnnotation(Singular.class, child); + deleteAnnotationIfNeccessary(child, Singular.class); + String explicitSingular = ann.getInstance().value(); + if (explicitSingular.isEmpty()) { + if (Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.SINGULAR_AUTO))) { + node.addError("The singular must be specified explicitly (e.g. @Singular(\"task\")) because auto singularization is disabled."); + explicitSingular = pluralName.toString(); + } else { + explicitSingular = autoSingularize(node.getName()); + if (explicitSingular == null) { + node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))"); explicitSingular = pluralName.toString(); - } else { - explicitSingular = autoSingularize(node.getName()); - if (explicitSingular == null) { - node.addError("Can't singularize this name; please specify the singular explicitly (i.e. @Singular(\"sheep\"))"); - explicitSingular = pluralName.toString(); - } } } - Name singularName = node.toName(explicitSingular); - - JCExpression type = null; - if (node.get() instanceof JCVariableDecl) { - type = ((JCVariableDecl) node.get()).vartype; - } - - String name = null; - List typeArgs = List.nil(); - if (type instanceof JCTypeApply) { - typeArgs = ((JCTypeApply) type).arguments; - type = ((JCTypeApply) type).clazz; - } - - name = type.toString(); - - String targetFqn = JavacSingularsRecipes.get().toQualified(name); - JavacSingularizer singularizer = JavacSingularsRecipes.get().getSingularizer(targetFqn); - if (singularizer == null) { - node.addError("Lombok does not know how to create the singular-form builder methods for type '" + name + "'; they won't be generated."); - return null; - } - - return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer); } + Name singularName = node.toName(explicitSingular); + + JCExpression type = null; + if (node.get() instanceof JCVariableDecl) { + type = ((JCVariableDecl) node.get()).vartype; + } + + String name = null; + List typeArgs = List.nil(); + if (type instanceof JCTypeApply) { + typeArgs = ((JCTypeApply) type).arguments; + type = ((JCTypeApply) type).clazz; + } + + name = type.toString(); + + String targetFqn = JavacSingularsRecipes.get().toQualified(name); + JavacSingularizer singularizer = JavacSingularsRecipes.get().getSingularizer(targetFqn); + if (singularizer == null) { + node.addError("Lombok does not know how to create the singular-form builder methods for type '" + name + "'; they won't be generated."); + return null; + } + + return new SingularData(child, singularName, pluralName, typeArgs, targetFqn, singularizer); } return null; diff --git a/test/transform/resource/after-delombok/BuilderWithToBuilder.java b/test/transform/resource/after-delombok/BuilderWithToBuilder.java new file mode 100644 index 00000000..eb61a6db --- /dev/null +++ b/test/transform/resource/after-delombok/BuilderWithToBuilder.java @@ -0,0 +1,216 @@ +import java.util.List; +class BuilderWithToBuilder { + private String mOne; + private String mTwo; + private T foo; + private List bars; + public static K rrr(BuilderWithToBuilder x) { + return x.foo; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + BuilderWithToBuilder(final String one, final String two, final T foo, final List bars) { + this.mOne = one; + this.mTwo = two; + this.foo = foo; + this.bars = bars; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static class BuilderWithToBuilderBuilder { + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private String one; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private String two; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private T foo; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private java.util.ArrayList bars; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + BuilderWithToBuilderBuilder() { + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder one(final String one) { + this.one = one; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder two(final String two) { + this.two = two; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder foo(final T foo) { + this.foo = foo; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder bar(final T bar) { + if (this.bars == null) this.bars = new java.util.ArrayList(); + this.bars.add(bar); + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder bars(final java.util.Collection bars) { + if (this.bars == null) this.bars = new java.util.ArrayList(); + this.bars.addAll(bars); + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilder build() { + java.util.List bars; + switch (this.bars == null ? 0 : this.bars.size()) { + case 0: + bars = java.util.Collections.emptyList(); + break; + case 1: + bars = java.util.Collections.singletonList(this.bars.get(0)); + break; + default: + bars = java.util.Collections.unmodifiableList(new java.util.ArrayList(this.bars)); + } + return new BuilderWithToBuilder(one, two, foo, bars); + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public java.lang.String toString() { + return "BuilderWithToBuilder.BuilderWithToBuilderBuilder(one=" + this.one + ", two=" + this.two + ", foo=" + this.foo + ", bars=" + this.bars + ")"; + } + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static BuilderWithToBuilderBuilder builder() { + return new BuilderWithToBuilderBuilder(); + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithToBuilderBuilder toBuilder() { + return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(this.bars); + } +} +class ConstructorWithToBuilder { + private String mOne; + private String mTwo; + private T foo; + @lombok.Singular + private List bars; + public ConstructorWithToBuilder(String mOne, T bar) { + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static class ConstructorWithToBuilderBuilder { + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private String mOne; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private T bar; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + ConstructorWithToBuilderBuilder() { + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public ConstructorWithToBuilderBuilder mOne(final String mOne) { + this.mOne = mOne; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public ConstructorWithToBuilderBuilder bar(final T bar) { + this.bar = bar; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public ConstructorWithToBuilder build() { + return new ConstructorWithToBuilder(mOne, bar); + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public java.lang.String toString() { + return "ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne + ", bar=" + this.bar + ")"; + } + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static ConstructorWithToBuilderBuilder builder() { + return new ConstructorWithToBuilderBuilder(); + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public ConstructorWithToBuilderBuilder toBuilder() { + return new ConstructorWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + } +} +class StaticWithToBuilder { + private String mOne; + private String mTwo; + private T foo; + private K bar; + @lombok.Singular + private List bars; + public static StaticWithToBuilder test(String mOne, Z bar) { + return new StaticWithToBuilder(); + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static class StaticWithToBuilderBuilder { + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private String mOne; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private Z bar; + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + StaticWithToBuilderBuilder() { + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public StaticWithToBuilderBuilder mOne(final String mOne) { + this.mOne = mOne; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public StaticWithToBuilderBuilder bar(final Z bar) { + this.bar = bar; + return this; + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public StaticWithToBuilder build() { + return StaticWithToBuilder.test(mOne, bar); + } + @java.lang.Override + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public java.lang.String toString() { + return "StaticWithToBuilder.StaticWithToBuilderBuilder(mOne=" + this.mOne + ", bar=" + this.bar + ")"; + } + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static StaticWithToBuilderBuilder builder() { + return new StaticWithToBuilderBuilder(); + } + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public StaticWithToBuilderBuilder toBuilder() { + return new StaticWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + } +} -- cgit