Skip to content

Commit

Permalink
GROOVY-10500
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 21, 2022
1 parent 6644009 commit ea0398f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,18 +57,17 @@
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;
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
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;
Expand Down Expand Up @@ -209,9 +207,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map

Set<String> names = new HashSet<>();
List<PropertyNode> 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<MapEntryExpression> entries = new ArrayList<>();
for (PropertyNode pNode : props) {
String name = pNode.getName();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -216,9 +213,10 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map

Set<String> names = new HashSet<>();
List<PropertyNode> 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<MapEntryExpression> entries = new ArrayList<>();
for (PropertyNode pNode : props) {
String name = pNode.getName();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -215,23 +211,19 @@ private boolean processDelegateParam(final MethodNode mNode, final Parameter map

Set<String> names = new HashSet<>();
List<PropertyNode> 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<MapEntryExpression> 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;
}
Expand Down

0 comments on commit ea0398f

Please sign in to comment.