use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class AbstractASTTransformation method getMemberClassList.
public List<ClassNode> getMemberClassList(AnnotationNode anno, String name) {
List<ClassNode> list = new ArrayList<>();
Expression expr = anno.getMember(name);
if (expr == null) {
return null;
}
if (expr instanceof ListExpression) {
final ListExpression listExpression = (ListExpression) expr;
if (isUndefinedMarkerList(listExpression)) {
return null;
}
list = getTypeList(anno, name, listExpression);
} else if (expr instanceof ClassExpression) {
ClassNode cn = expr.getType();
if (isUndefined(cn))
return null;
if (cn != null)
list.add(cn);
} else if (expr instanceof VariableExpression) {
addError("Expecting to find a class value for '" + name + "' but found variable: " + expr.getText() + ". Missing import or unknown class?", anno);
} else if (expr instanceof ConstantExpression) {
addError("Expecting to find a class value for '" + name + "' but found constant: " + expr.getText() + "!", anno);
}
return list;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class AnnotationCollectorTransform method getTargetListFromValue.
private List<AnnotationNode> getTargetListFromValue(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, SourceUnit source) {
Expression memberValue = collector.getMember("value");
if (memberValue == null) {
return Collections.emptyList();
}
if (!(memberValue instanceof ListExpression)) {
addError("Annotation collector expected a list of classes, but got a " + memberValue.getClass(), collector, source);
return Collections.emptyList();
}
ListExpression memberListExp = (ListExpression) memberValue;
List<Expression> memberList = memberListExp.getExpressions();
if (memberList.isEmpty()) {
return Collections.emptyList();
}
List<AnnotationNode> ret = new ArrayList<>();
for (Expression e : memberList) {
AnnotationNode toAdd = new AnnotationNode(e.getType());
toAdd.setSourcePosition(aliasAnnotationUsage);
ret.add(toAdd);
}
return ret;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class JavaStubGenerator method getAnnotationValue.
private String getAnnotationValue(Object memberValue) {
String val = "null";
boolean replaceDollars = true;
if (memberValue instanceof ListExpression) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
ListExpression le = (ListExpression) memberValue;
for (Expression e : le.getExpressions()) {
if (first)
first = false;
else
sb.append(",");
sb.append(getAnnotationValue(e));
}
sb.append("}");
val = sb.toString();
} else if (memberValue instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) memberValue;
Object constValue = ce.getValue();
if (constValue instanceof AnnotationNode) {
Writer writer = new StringBuilderWriter();
PrintWriter out = new PrintWriter(writer);
printAnnotation(out, (AnnotationNode) constValue);
val = writer.toString();
} else if (constValue instanceof Number || constValue instanceof Boolean) {
val = constValue.toString();
} else {
val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
replaceDollars = false;
}
} else if (memberValue instanceof PropertyExpression) {
// assume must be static class field or enum value or class that Java can resolve
val = ((Expression) memberValue).getText();
} else if (memberValue instanceof VariableExpression) {
val = ((Expression) memberValue).getText();
// check for an alias
ImportNode alias = currentModule.getStaticImports().get(val);
if (alias != null)
val = alias.getClassName() + "." + alias.getFieldName();
} else if (memberValue instanceof ClosureExpression) {
// annotation closure; replaced with this specific class literal to cover the
// case where annotation type uses Class<? extends Closure> for the closure's type
val = "groovy.lang.Closure.class";
} else if (memberValue instanceof ClassExpression) {
val = ((Expression) memberValue).getText() + ".class";
}
return replaceDollars ? val.replace('$', '.') : val;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class MacroGroovyMethods method buildSubstitutions.
public static ListExpression buildSubstitutions(final SourceUnit source, final ASTNode expr) {
final ListExpression listExpression = new ListExpression();
ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
@Override
protected SourceUnit getSourceUnit() {
return null;
}
@Override
public void visitClass(final ClassNode node) {
super.visitClass(node);
Iterator<InnerClassNode> it = node.getInnerClasses();
while (it.hasNext()) {
InnerClassNode next = it.next();
visitClass(next);
}
}
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
super.visitMethodCallExpression(call);
if (DOLLAR_VALUE.equals(call.getMethodAsString())) {
ClosureExpression substitutionClosureExpression = getClosureArgument(source, call);
if (substitutionClosureExpression == null) {
return;
}
Statement code = substitutionClosureExpression.getCode();
if (code instanceof BlockStatement) {
((BlockStatement) code).setVariableScope(null);
}
listExpression.addExpression(substitutionClosureExpression);
}
}
};
if (expr instanceof ClassNode) {
visitor.visitClass((ClassNode) expr);
} else {
expr.visit(visitor);
}
return listExpression;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class ResolveVisitor method transformBinaryExpression.
protected Expression transformBinaryExpression(final BinaryExpression be) {
Expression left = transform(be.getLeftExpression());
if (be.getOperation().isA(Types.ASSIGNMENT_OPERATOR) && 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 && be.getOperation().isOneOf(new int[] { Types.ARRAY_EXPRESSION, Types.SYNTH_LIST, Types.SYNTH_MAP })) {
if (be.getRightExpression() instanceof ListExpression) {
ListExpression list = (ListExpression) be.getRightExpression();
if (list.getExpressions().isEmpty()) {
return new ClassExpression(left.getType().makeArray());
} else {
// maybe 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) {
MapExpression me = new MapExpression();
for (Expression expression : list.getExpressions()) {
me.addMapEntryExpression((MapEntryExpression) transform(expression));
}
me.setSourcePosition(list);
return CastExpression.asExpression(left.getType(), me);
}
}
} else if (be.getRightExpression() instanceof SpreadMapExpression) {
// we have C[*:map] -> should become (C) map
SpreadMapExpression mapExpression = (SpreadMapExpression) be.getRightExpression();
Expression right = transform(mapExpression.getExpression());
return CastExpression.asExpression(left.getType(), right);
}
if (be.getRightExpression() instanceof MapEntryExpression) {
// may be we have C[k1:v1] -> should become (C)([k1:v1])
MapExpression me = new MapExpression();
me.addMapEntryExpression((MapEntryExpression) transform(be.getRightExpression()));
me.setSourcePosition(be.getRightExpression());
return new CastExpression(left.getType(), me);
}
}
Expression right = transform(be.getRightExpression());
be.setLeftExpression(left);
be.setRightExpression(right);
return be;
}
Aggregations