From f13b4dbbfccfe5a6bb72906c84c2e640e05d7788 Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sat, 7 Jan 2023 10:10:33 +0100 Subject: [fixes #3306] Convert short array initializers to long version --- src/core/lombok/eclipse/handlers/HandleBuilder.java | 16 +++++++++++++++- src/core/lombok/javac/handlers/HandleBuilder.java | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index a2dd5057..d5ecfeaf 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 { 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 { JCBlock body = maker.Block(0, List.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.nil(), List.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 -- cgit