Skip to content

Commit ea0398f

Browse files
committed
GROOVY-10500
1 parent 6644009 commit ea0398f

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/NamedVariantTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,6 @@ public void testNamedVariant12() {
303303
};
304304
//@formatter:on
305305

306-
runConformTest(sources, /*TODO:"Pogo(42)"*/"", "groovy.lang.MissingPropertyException: No such property: V for class: Script");
306+
runConformTest(sources, "Pogo(42)");
307307
}
308308
}

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
3434
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
3535
import org.codehaus.groovy.ast.expr.Expression;
36-
import org.codehaus.groovy.ast.expr.MapEntryExpression;
3736
import org.codehaus.groovy.ast.expr.MethodCallExpression;
3837
import org.codehaus.groovy.ast.stmt.AssertStatement;
3938
import org.codehaus.groovy.ast.stmt.BlockStatement;
@@ -58,18 +57,17 @@
5857
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
5958
import static org.codehaus.groovy.ast.ClassHelper.make;
6059
import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching;
60+
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
6161
import static org.codehaus.groovy.ast.tools.GeneralUtils.boolX;
6262
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
6363
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
6464
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
6565
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
6666
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
6767
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
68-
import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX;
6968
import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
7069
import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args;
7170
import static org.codehaus.groovy.ast.tools.GeneralUtils.listX;
72-
import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
7371
import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX;
7472
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
7573
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
@@ -209,9 +207,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map
209207

210208
Set<String> names = new HashSet<>();
211209
List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
212-
for (String next : names) {
213-
if (hasDuplicates(mNode, propNames, next)) return false;
210+
for (String name : names) {
211+
if (hasDuplicates(mNode, propNames, name)) return false;
214212
}
213+
/* GRECLIPSE edit -- GROOVY-10500
215214
List<MapEntryExpression> entries = new ArrayList<>();
216215
for (PropertyNode pNode : props) {
217216
String name = pNode.getName();
@@ -225,6 +224,17 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map
225224
mapParam.addAnnotation(namedParam);
226225
}
227226
Expression delegateMap = mapX(entries);
227+
*/
228+
for (PropertyNode prop : props) {
229+
// create annotation @NamedParam(value='name', type=PropertyType)
230+
AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
231+
namedParam.addMember("value", constX(prop.getName()));
232+
namedParam.addMember("type", classX(prop.getType()));
233+
mapParam.addAnnotation(namedParam);
234+
}
235+
Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new);
236+
Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs));
237+
// GRECLIPSE end
228238
args.addExpression(castX(fromParam.getType(), delegateMap));
229239
return true;
230240
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
3535
import org.codehaus.groovy.ast.expr.ElvisOperatorExpression;
3636
import org.codehaus.groovy.ast.expr.Expression;
37-
import org.codehaus.groovy.ast.expr.MapEntryExpression;
3837
import org.codehaus.groovy.ast.expr.MethodCallExpression;
3938
import org.codehaus.groovy.ast.stmt.AssertStatement;
4039
import org.codehaus.groovy.ast.stmt.BlockStatement;
@@ -69,11 +68,9 @@
6968
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
7069
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
7170
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
72-
import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX;
7371
import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
7472
import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args;
7573
import static org.codehaus.groovy.ast.tools.GeneralUtils.listX;
76-
import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
7774
import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX;
7875
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
7976
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
@@ -216,9 +213,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map
216213

217214
Set<String> names = new HashSet<>();
218215
List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
219-
for (String next : names) {
220-
if (hasDuplicates(mNode, propNames, next)) return false;
216+
for (String name : names) {
217+
if (hasDuplicates(mNode, propNames, name)) return false;
221218
}
219+
/* GRECLIPSE edit -- GROOVY-10500
222220
List<MapEntryExpression> entries = new ArrayList<>();
223221
for (PropertyNode pNode : props) {
224222
String name = pNode.getName();
@@ -233,6 +231,17 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map
233231
mapParam.addAnnotation(namedParam);
234232
}
235233
Expression delegateMap = mapX(entries);
234+
*/
235+
for (PropertyNode prop : props) {
236+
// create annotation @NamedParam(value='name', type=PropertyType)
237+
AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
238+
namedParam.addMember("value", constX(prop.getName()));
239+
namedParam.addMember("type", classX(prop.getType()));
240+
mapParam.addAnnotation(namedParam);
241+
}
242+
Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new);
243+
Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs));
244+
// GRECLIPSE end
236245
args.addExpression(castX(fromParam.getType(), delegateMap));
237246
return true;
238247
}

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/NamedVariantASTTransformation.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
3434
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
3535
import org.codehaus.groovy.ast.expr.Expression;
36-
import org.codehaus.groovy.ast.expr.MapEntryExpression;
3736
import org.codehaus.groovy.ast.expr.MethodCallExpression;
3837
import org.codehaus.groovy.ast.stmt.AssertStatement;
3938
import org.codehaus.groovy.ast.stmt.BlockStatement;
@@ -45,7 +44,6 @@
4544
import java.util.ArrayList;
4645
import java.util.HashSet;
4746
import java.util.List;
48-
import java.util.Optional;
4947
import java.util.Set;
5048

5149
import static java.util.stream.Collectors.toList;
@@ -69,11 +67,9 @@
6967
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
7068
import static org.codehaus.groovy.ast.tools.GeneralUtils.defaultValueX;
7169
import static org.codehaus.groovy.ast.tools.GeneralUtils.elvisX;
72-
import static org.codehaus.groovy.ast.tools.GeneralUtils.entryX;
7370
import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
7471
import static org.codehaus.groovy.ast.tools.GeneralUtils.list2args;
7572
import static org.codehaus.groovy.ast.tools.GeneralUtils.listX;
76-
import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
7773
import static org.codehaus.groovy.ast.tools.GeneralUtils.notNullX;
7874
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
7975
import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX;
@@ -215,23 +211,19 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map
215211

216212
Set<String> names = new HashSet<>();
217213
List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true);
218-
for (String next : names) {
219-
if (hasDuplicates(this, mNode, propNames, next)) return false;
214+
for (String name : names) {
215+
if (hasDuplicates(this, mNode, propNames, name)) return false;
220216
}
221-
List<MapEntryExpression> entries = new ArrayList<>();
222-
for (PropertyNode pNode : props) {
223-
String name = pNode.getName();
224-
ClassNode type = pNode.getType();
225-
// create entry [name: __namedArgs.getOrDefault('name', initialValue)]
226-
Expression defaultValue = Optional.ofNullable(pNode.getInitialExpression()).orElseGet(() -> defaultValueX(type));
227-
entries.add(entryX(constX(name), asType(callX(varX(mapParam), "getOrDefault", args(constX(name), defaultValue)), type, coerce)));
217+
for (PropertyNode prop : props) {
228218
// create annotation @NamedParam(value='name', type=PropertyType)
229219
AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE);
230-
namedParam.addMember("value", constX(name));
231-
namedParam.addMember("type", classX(type));
220+
namedParam.addMember("value", constX(prop.getName()));
221+
namedParam.addMember("type", classX(prop.getType()));
232222
mapParam.addAnnotation(namedParam);
233223
}
234-
Expression delegateMap = mapX(entries);
224+
225+
Expression[] subMapArgs = names.stream().map(name -> constX(name)).toArray(Expression[]::new);
226+
Expression delegateMap = callX(varX(mapParam), "subMap", args(subMapArgs));
235227
args.addExpression(castX(fromParam.getType(), delegateMap));
236228
return true;
237229
}

0 commit comments

Comments
 (0)