diff options
5 files changed, 171 insertions, 1 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index bb9b6dda..57c3afde 100755 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -36,6 +36,7 @@ import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; +import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression; import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.Assignment; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; @@ -918,7 +919,20 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; FieldDeclaration fd = (FieldDeclaration) fieldNode.get(); out.returnType = copyType(fd.type, source); - out.statements = new Statement[] {new ReturnStatement(fd.initialization, pS, pE)}; + + // Convert short array initializers from `{1,2}` to `new int[]{1,2}` + Expression initialization; + if (fd.initialization instanceof ArrayInitializer) { + ArrayAllocationExpression arrayAllocationExpression = new ArrayAllocationExpression(); + arrayAllocationExpression.initializer = (ArrayInitializer) fd.initialization; + arrayAllocationExpression.type = generateQualifiedTypeRef(fd, fd.type.getTypeName()); + arrayAllocationExpression.dimensions = new Expression[fd.type.dimensions()]; + initialization = arrayAllocationExpression; + } else { + initialization = fd.initialization; + } + + out.statements = new Statement[] {new ReturnStatement(initialization, pS, pE)}; fd.initialization = null; out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) fieldNode.up().get()).scope); diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 854c8524..ed7d2844 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -45,6 +45,7 @@ import com.sun.tools.javac.tree.JCTree.JCLiteral; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; import com.sun.tools.javac.tree.JCTree.JCModifiers; +import com.sun.tools.javac.tree.JCTree.JCNewArray; import com.sun.tools.javac.tree.JCTree.JCNewClass; import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree; import com.sun.tools.javac.tree.JCTree.JCReturn; @@ -817,6 +818,14 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { JCBlock body = maker.Block(0, List.<JCStatement>of(statement)); int modifiers = Flags.PRIVATE | Flags.STATIC; JCMethodDecl defaultProvider = maker.MethodDef(maker.Modifiers(modifiers), methodName, cloneType(maker, field.vartype, fieldNode), copyTypeParams(fieldNode, params), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); + // ... then we convert short array initializers from `{1,2}` to `new int[]{1,2}` ... + if (init instanceof JCNewArray && field.vartype instanceof JCArrayTypeTree) { + JCNewArray arrayInitializer = (JCNewArray) init; + JCArrayTypeTree fieldType = (JCArrayTypeTree) field.vartype; + if (arrayInitializer.elemtype == null) { + arrayInitializer.elemtype = cloneType(maker, fieldType.elemtype, fieldNode); + } + } // ... then we set positions for everything else ... recursiveSetGeneratedBy(defaultProvider, job.sourceNode); // ... and finally add back the original expression diff --git a/test/transform/resource/after-delombok/BuilderDefaultsArray.java b/test/transform/resource/after-delombok/BuilderDefaultsArray.java new file mode 100644 index 00000000..2dd6b304 --- /dev/null +++ b/test/transform/resource/after-delombok/BuilderDefaultsArray.java @@ -0,0 +1,79 @@ +import lombok.Value; + +public class BuilderDefaultsArray { + int[] x; + java.lang.String[][] y; + + @java.lang.SuppressWarnings("all") + private static int[] $default$x() { + return new int[] {1, 2}; + } + + @java.lang.SuppressWarnings("all") + private static java.lang.String[][] $default$y() { + return new java.lang.String[][] {}; + } + + @java.lang.SuppressWarnings("all") + BuilderDefaultsArray(final int[] x, final java.lang.String[][] y) { + this.x = x; + this.y = y; + } + + + @java.lang.SuppressWarnings("all") + public static class BuilderDefaultsArrayBuilder { + @java.lang.SuppressWarnings("all") + private boolean x$set; + @java.lang.SuppressWarnings("all") + private int[] x$value; + @java.lang.SuppressWarnings("all") + private boolean y$set; + @java.lang.SuppressWarnings("all") + private java.lang.String[][] y$value; + + @java.lang.SuppressWarnings("all") + BuilderDefaultsArrayBuilder() { + } + + /** + * @return {@code this}. + */ + @java.lang.SuppressWarnings("all") + public BuilderDefaultsArray.BuilderDefaultsArrayBuilder x(final int[] x) { + this.x$value = x; + x$set = true; + return this; + } + + /** + * @return {@code this}. + */ + @java.lang.SuppressWarnings("all") + public BuilderDefaultsArray.BuilderDefaultsArrayBuilder y(final java.lang.String[][] y) { + this.y$value = y; + y$set = true; + return this; + } + + @java.lang.SuppressWarnings("all") + public BuilderDefaultsArray build() { + int[] x$value = this.x$value; + if (!this.x$set) x$value = BuilderDefaultsArray.$default$x(); + java.lang.String[][] y$value = this.y$value; + if (!this.y$set) y$value = BuilderDefaultsArray.$default$y(); + return new BuilderDefaultsArray(x$value, y$value); + } + + @java.lang.Override + @java.lang.SuppressWarnings("all") + public java.lang.String toString() { + return "BuilderDefaultsArray.BuilderDefaultsArrayBuilder(x$value=" + java.util.Arrays.toString(this.x$value) + ", y$value=" + java.util.Arrays.deepToString(this.y$value) + ")"; + } + } + + @java.lang.SuppressWarnings("all") + public static BuilderDefaultsArray.BuilderDefaultsArrayBuilder builder() { + return new BuilderDefaultsArray.BuilderDefaultsArrayBuilder(); + } +} diff --git a/test/transform/resource/after-ecj/BuilderDefaultsArray.java b/test/transform/resource/after-ecj/BuilderDefaultsArray.java new file mode 100644 index 00000000..d25e0371 --- /dev/null +++ b/test/transform/resource/after-ecj/BuilderDefaultsArray.java @@ -0,0 +1,57 @@ +import lombok.Builder; +import lombok.Value; +public @Builder class BuilderDefaultsArray { + public static @java.lang.SuppressWarnings("all") class BuilderDefaultsArrayBuilder { + private @java.lang.SuppressWarnings("all") int[] x$value; + private @java.lang.SuppressWarnings("all") boolean x$set; + private @java.lang.SuppressWarnings("all") java.lang.String[][] y$value; + private @java.lang.SuppressWarnings("all") boolean y$set; + @java.lang.SuppressWarnings("all") BuilderDefaultsArrayBuilder() { + super(); + } + /** + * @return {@code this}. + */ + public @java.lang.SuppressWarnings("all") BuilderDefaultsArray.BuilderDefaultsArrayBuilder x(final int[] x) { + this.x$value = x; + x$set = true; + return this; + } + /** + * @return {@code this}. + */ + public @java.lang.SuppressWarnings("all") BuilderDefaultsArray.BuilderDefaultsArrayBuilder y(final java.lang.String[][] y) { + this.y$value = y; + y$set = true; + return this; + } + public @java.lang.SuppressWarnings("all") BuilderDefaultsArray build() { + int[] x$value = this.x$value; + if ((! this.x$set)) + x$value = BuilderDefaultsArray.$default$x(); + java.lang.String[][] y$value = this.y$value; + if ((! this.y$set)) + y$value = BuilderDefaultsArray.$default$y(); + return new BuilderDefaultsArray(x$value, y$value); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return (((("BuilderDefaultsArray.BuilderDefaultsArrayBuilder(x$value=" + java.util.Arrays.toString(this.x$value)) + ", y$value=") + java.util.Arrays.deepToString(this.y$value)) + ")"); + } + } + @Builder.Default int[] x; + @Builder.Default java.lang.String[][] y; + private static @java.lang.SuppressWarnings("all") int[] $default$x() { + return new int[]{1, 2}; + } + private static @java.lang.SuppressWarnings("all") java.lang.String[][] $default$y() { + return new java.lang.String[][]{}; + } + @java.lang.SuppressWarnings("all") BuilderDefaultsArray(final int[] x, final java.lang.String[][] y) { + super(); + this.x = x; + this.y = y; + } + public static @java.lang.SuppressWarnings("all") BuilderDefaultsArray.BuilderDefaultsArrayBuilder builder() { + return new BuilderDefaultsArray.BuilderDefaultsArrayBuilder(); + } +}
\ No newline at end of file diff --git a/test/transform/resource/before/BuilderDefaultsArray.java b/test/transform/resource/before/BuilderDefaultsArray.java new file mode 100644 index 00000000..11fabc6a --- /dev/null +++ b/test/transform/resource/before/BuilderDefaultsArray.java @@ -0,0 +1,11 @@ +import lombok.Builder; +import lombok.Value; + +@Builder +public class BuilderDefaultsArray { + @Builder.Default + int[] x = {1,2}; + + @Builder.Default + java.lang.String[][] y = {}; +}
\ No newline at end of file |