From ea0398f5568417916ed1804415da3a4fc30f383f Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Mon, 21 Feb 2022 14:00:29 -0600 Subject: [PATCH] GROOVY-10500 --- .../core/tests/xform/NamedVariantTests.java | 2 +- .../NamedVariantASTTransformation.java | 20 ++++++++++++---- .../NamedVariantASTTransformation.java | 19 +++++++++++---- .../NamedVariantASTTransformation.java | 24 +++++++------------ 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/NamedVariantTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/NamedVariantTests.java index 9aa8f3ae54..2f10928b02 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/NamedVariantTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/NamedVariantTests.java @@ -303,6 +303,6 @@ public void testNamedVariant12() { }; //@formatter:on - runConformTest(sources, /*TODO:"Pogo(42)"*/"", "groovy.lang.MissingPropertyException: No such property: V for class: Script"); + runConformTest(sources, "Pogo(42)"); } } diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java index 01345ba0fc..9284ddf8b8 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java @@ -33,7 +33,6 @@ import org.codehaus.groovy.ast.expr.AnnotationConstantExpression; import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.Expression; -import org.codehaus.groovy.ast.expr.MapEntryExpression; import org.codehaus.groovy.ast.expr.MethodCallExpression; import org.codehaus.groovy.ast.stmt.AssertStatement; import org.codehaus.groovy.ast.stmt.BlockStatement; @@ -58,6 +57,7 @@ import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType; import static org.codehaus.groovy.ast.ClassHelper.make; import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching; +import static org.codehaus.groovy.ast.tools.GeneralUtils.args; import static org.codehaus.groovy.ast.tools.GeneralUtils.boolX; import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX; import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; @@ -65,11 +65,9 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.classX; import static org.codehaus.groovy.ast.tools.GeneralUtils.constX; import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX; import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties; import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args; import static org.codehaus.groovy.ast.tools.GeneralUtils.listX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX; import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX; import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX; import static org.codehaus.groovy.ast.tools.GeneralUtils.param; @@ -209,9 +207,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map Set names = new HashSet<>(); List props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true); - for (String next : names) { - if (hasDuplicates(mNode, propNames, next)) return false; + for (String name : names) { + if (hasDuplicates(mNode, propNames, name)) return false; } + /* GRECLIPSE edit -- GROOVY-10500 List entries = new ArrayList<>(); for (PropertyNode pNode : props) { String name = pNode.getName(); @@ -225,6 +224,17 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map mapParam.addAnnotation(namedParam); } Expression delegateMap = mapX(entries); + */ + for (PropertyNode prop : props) { + // create annotation @NamedParam(value='name', type=PropertyType) + AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE); + namedParam.addMember("value", constX(prop.getName())); + namedParam.addMember("type", classX(prop.getType())); + mapParam.addAnnotation(namedParam); + } + Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new); + Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs)); + // GRECLIPSE end args.addExpression(castX(fromParam.getType(), delegateMap)); return true; } diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java index ccc37e4057..247e50b257 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java @@ -34,7 +34,6 @@ import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.ElvisOperatorExpression; import org.codehaus.groovy.ast.expr.Expression; -import org.codehaus.groovy.ast.expr.MapEntryExpression; import org.codehaus.groovy.ast.expr.MethodCallExpression; import org.codehaus.groovy.ast.stmt.AssertStatement; import org.codehaus.groovy.ast.stmt.BlockStatement; @@ -69,11 +68,9 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.classX; import static org.codehaus.groovy.ast.tools.GeneralUtils.constX; import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX; import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties; import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args; import static org.codehaus.groovy.ast.tools.GeneralUtils.listX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX; import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX; import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX; import static org.codehaus.groovy.ast.tools.GeneralUtils.param; @@ -216,9 +213,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map Set names = new HashSet<>(); List props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true); - for (String next : names) { - if (hasDuplicates(mNode, propNames, next)) return false; + for (String name : names) { + if (hasDuplicates(mNode, propNames, name)) return false; } + /* GRECLIPSE edit -- GROOVY-10500 List entries = new ArrayList<>(); for (PropertyNode pNode : props) { String name = pNode.getName(); @@ -233,6 +231,17 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map mapParam.addAnnotation(namedParam); } Expression delegateMap = mapX(entries); + */ + for (PropertyNode prop : props) { + // create annotation @NamedParam(value='name', type=PropertyType) + AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE); + namedParam.addMember("value", constX(prop.getName())); + namedParam.addMember("type", classX(prop.getType())); + mapParam.addAnnotation(namedParam); + } + Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new); + Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs)); + // GRECLIPSE end args.addExpression(castX(fromParam.getType(), delegateMap)); return true; } diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java index 5f6d887cc0..e41519b034 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java @@ -33,7 +33,6 @@ import org.codehaus.groovy.ast.expr.AnnotationConstantExpression; import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.Expression; -import org.codehaus.groovy.ast.expr.MapEntryExpression; import org.codehaus.groovy.ast.expr.MethodCallExpression; import org.codehaus.groovy.ast.stmt.AssertStatement; import org.codehaus.groovy.ast.stmt.BlockStatement; @@ -45,7 +44,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; import static java.util.stream.Collectors.toList; @@ -69,11 +67,9 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX; import static org.codehaus.groovy.ast.tools.GeneralUtils.defaultValueX; import static org.codehaus.groovy.ast.tools.GeneralUtils.elvisX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX; import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties; import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args; import static org.codehaus.groovy.ast.tools.GeneralUtils.listX; -import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX; import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX; import static org.codehaus.groovy.ast.tools.GeneralUtils.param; import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX; @@ -215,23 +211,19 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map Set names = new HashSet<>(); List props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true); - for (String next : names) { - if (hasDuplicates(this, mNode, propNames, next)) return false; + for (String name : names) { + if (hasDuplicates(this, mNode, propNames, name)) return false; } - List entries = new ArrayList<>(); - for (PropertyNode pNode : props) { - String name = pNode.getName(); - ClassNode type = pNode.getType(); - // create entry [name: __namedArgs.getOrDefault('name', initialValue)] - Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> defaultValueX(type)); - entries.add(entryX(constX(name), asType(callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue)), type, coerce))); + for (PropertyNode prop : props) { // create annotation @NamedParam(value='name', type=PropertyType) AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE); - namedParam.addMember("value", constX(name)); - namedParam.addMember("type", classX(type)); + namedParam.addMember("value", constX(prop.getName())); + namedParam.addMember("type", classX(prop.getType())); mapParam.addAnnotation(namedParam); } - Expression delegateMap = mapX(entries); + + Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new); + Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs)); args.addExpression(castX(fromParam.getType(), delegateMap)); return true; }