use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class AstBuilder method createEnumConstantInitExpression.
private Expression createEnumConstantInitExpression(final ArgumentsContext ctx, final InnerClassNode anonymousInnerClassNode) {
if (!asBoolean(ctx) && !asBoolean(anonymousInnerClassNode)) {
return null;
}
TupleExpression argumentListExpression = (TupleExpression) this.visitArguments(ctx);
List<Expression> expressions = argumentListExpression.getExpressions();
if (expressions.size() == 1) {
Expression expression = expressions.get(0);
if (expression instanceof NamedArgumentListExpression) {
// e.g. SOME_ENUM_CONSTANT(a: "1", b: "2")
List<MapEntryExpression> mapEntryExpressionList = ((NamedArgumentListExpression) expression).getMapEntryExpressions();
ListExpression listExpression = new ListExpression(mapEntryExpressionList.stream().map(e -> (Expression) e).collect(Collectors.toList()));
if (asBoolean(anonymousInnerClassNode)) {
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
}
if (mapEntryExpressionList.size() > 1) {
listExpression.setWrapped(true);
}
return configureAST(listExpression, ctx);
}
if (!asBoolean(anonymousInnerClassNode)) {
if (expression instanceof ListExpression) {
ListExpression listExpression = new ListExpression();
listExpression.addExpression(expression);
return configureAST(listExpression, ctx);
}
return expression;
}
ListExpression listExpression = new ListExpression();
if (expression instanceof ListExpression) {
((ListExpression) expression).getExpressions().forEach(listExpression::addExpression);
} else {
listExpression.addExpression(expression);
}
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
return configureAST(listExpression, ctx);
}
ListExpression listExpression = new ListExpression(expressions);
if (asBoolean(anonymousInnerClassNode)) {
listExpression.addExpression(configureAST(new ClassExpression(anonymousInnerClassNode), anonymousInnerClassNode));
}
if (asBoolean(ctx)) {
listExpression.setWrapped(true);
}
return asBoolean(ctx) ? configureAST(listExpression, ctx) : configureAST(listExpression, anonymousInnerClassNode);
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class ImmutablePropertyUtils method getKnownImmutables.
public static List<String> getKnownImmutables(final AbstractASTTransformation xform, final ClassNode cNode) {
List<AnnotationNode> annotations = cNode.getAnnotations(ImmutablePropertyUtils.IMMUTABLE_OPTIONS_TYPE);
AnnotationNode anno = annotations.isEmpty() ? null : annotations.get(0);
final List<String> immutables = new ArrayList<String>();
if (anno == null)
return immutables;
final Expression expression = anno.getMember(MEMBER_KNOWN_IMMUTABLES);
if (expression == null)
return immutables;
if (!(expression instanceof ListExpression)) {
xform.addError("Use the Groovy list notation [el1, el2] to specify known immutable property names via \"" + MEMBER_KNOWN_IMMUTABLES + "\"", anno);
return immutables;
}
final ListExpression listExpression = (ListExpression) expression;
for (Expression listItemExpression : listExpression.getExpressions()) {
if (listItemExpression instanceof ConstantExpression) {
immutables.add((String) ((ConstantExpression) listItemExpression).getValue());
}
}
if (!xform.checkPropertyList(cNode, immutables, "knownImmutables", anno, "immutable class", false))
return immutables;
return immutables;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class ExpressionUtils method transformListOfConstants.
/**
* Given a list of constants, transform each item in the list.
*
* @param origList the list to transform
* @param attrType the target type
* @return the transformed list or the original if nothing was changed
*/
public static Expression transformListOfConstants(final ListExpression origList, final ClassNode attrType) {
ListExpression newList = new ListExpression();
boolean changed = false;
for (Expression e : origList.getExpressions()) {
try {
Expression transformed = transformInlineConstants(e, attrType);
newList.addExpression(transformed);
if (transformed != e)
changed = true;
} catch (Exception ignored) {
newList.addExpression(e);
}
}
if (changed) {
newList.setSourcePosition(origList);
return newList;
}
return origList;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project gradle by gradle.
the class GradleResolveVisitor method transformBinaryExpression.
@Override
protected Expression transformBinaryExpression(BinaryExpression be) {
Expression left = transform(be.getLeftExpression());
int type = be.getOperation().getType();
if ((type == Types.ASSIGNMENT_OPERATOR || type == Types.EQUAL) && left instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) left;
String error = "you tried to assign a value to the class '" + ce.getType().getName() + "'";
if (ce.getType().isScript()) {
error += ". Do you have a script with this name?";
}
addError(error, be.getLeftExpression());
return be;
}
if (left instanceof ClassExpression && isLeftSquareBracket(type)) {
if (be.getRightExpression() instanceof ListExpression) {
ListExpression list = (ListExpression) be.getRightExpression();
if (list.getExpressions().isEmpty()) {
// we have C[] if the list is empty -> should be an array then!
final ClassExpression ce = new ClassExpression(left.getType().makeArray());
ce.setSourcePosition(be);
return ce;
} else {
// may be we have C[k1:v1, k2:v2] -> should become (C)([k1:v1, k2:v2])
boolean map = true;
for (Expression expression : list.getExpressions()) {
if (!(expression instanceof MapEntryExpression)) {
map = false;
break;
}
}
if (map) {
final MapExpression me = new MapExpression();
for (Expression expression : list.getExpressions()) {
me.addMapEntryExpression((MapEntryExpression) transform(expression));
}
me.setSourcePosition(list);
final CastExpression ce = new CastExpression(left.getType(), me);
ce.setSourcePosition(be);
return ce;
}
}
} else if (be.getRightExpression() instanceof SpreadMapExpression) {
// we have C[*:map] -> should become (C) map
SpreadMapExpression mapExpression = (SpreadMapExpression) be.getRightExpression();
Expression right = transform(mapExpression.getExpression());
Expression ce = new CastExpression(left.getType(), right);
ce.setSourcePosition(be);
return ce;
}
if (be.getRightExpression() instanceof MapEntryExpression) {
// may be we have C[k1:v1] -> should become (C)([k1:v1])
final MapExpression me = new MapExpression();
me.addMapEntryExpression((MapEntryExpression) transform(be.getRightExpression()));
me.setSourcePosition(be.getRightExpression());
final CastExpression ce = new CastExpression(left.getType(), me);
ce.setSourcePosition(be);
return ce;
}
}
Expression right = transform(be.getRightExpression());
be.setLeftExpression(left);
be.setRightExpression(right);
return be;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy-core by groovy.
the class ImmutableASTTransformation method getKnownImmutableClasses.
private List<String> getKnownImmutableClasses(AnnotationNode node) {
final ArrayList<String> immutableClasses = new ArrayList<String>();
final Expression expression = node.getMember(MEMBER_KNOWN_IMMUTABLE_CLASSES);
if (expression == null)
return immutableClasses;
if (!(expression instanceof ListExpression)) {
addError("Use the Groovy list notation [el1, el2] to specify known immutable classes via \"" + MEMBER_KNOWN_IMMUTABLE_CLASSES + "\"", node);
return immutableClasses;
}
final ListExpression listExpression = (ListExpression) expression;
for (Expression listItemExpression : listExpression.getExpressions()) {
if (listItemExpression instanceof ClassExpression) {
immutableClasses.add(listItemExpression.getType().getName());
}
}
return immutableClasses;
}
Aggregations