use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class AbstractASTTransformation method getMemberStringList.
public static List<String> getMemberStringList(AnnotationNode anno, String name) {
Expression expr = anno.getMember(name);
if (expr == null) {
return null;
}
if (expr instanceof ListExpression) {
List<String> list = new ArrayList<String>();
final ListExpression listExpression = (ListExpression) expr;
if (isUndefinedMarkerList(listExpression)) {
return null;
}
for (Expression itemExpr : listExpression.getExpressions()) {
if (itemExpr != null && itemExpr instanceof ConstantExpression) {
Object value = ((ConstantExpression) itemExpr).getValue();
if (value != null)
list.add(value.toString());
}
}
return list;
}
return tokenize(getMemberStringValue(anno, name));
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class CastExpressionOptimizer method transformCastExpression.
public Expression transformCastExpression(final CastExpression cast) {
if (cast.isCoerce()) {
Expression expression = cast.getExpression();
ClassNode exprInferredType = transformer.getTypeChooser().resolveType(expression, transformer.getClassNode());
ClassNode castType = cast.getType();
if (castType.isArray() && expression instanceof ListExpression) {
ArrayExpression arrayExpression = new ArrayExpression(castType.getComponentType(), ((ListExpression) expression).getExpressions());
arrayExpression.setSourcePosition(cast);
return transformer.transform(arrayExpression);
}
if (isOptimizable(exprInferredType, castType)) {
// coerce is not needed
CastExpression trn = new CastExpression(castType, transformer.transform(expression));
trn.setSourcePosition(cast);
trn.copyNodeMetaData(cast);
return trn;
}
} else if (ClassHelper.char_TYPE.equals(cast.getType())) {
Expression expression = cast.getExpression();
if (expression instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) expression;
if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
String val = (String) ce.getValue();
if (val != null && val.length() == 1) {
ConstantExpression result = new ConstantExpression(val.charAt(0), true);
result.setSourcePosition(cast);
return result;
}
}
}
}
return transformer.superTransform(cast);
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class ListExpressionTransformer method transformRegularConstructor.
private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
// can be replaced with a direct constructor call
List<Expression> transformedArgs = transformArguments(expr);
ConstructorCallExpression cce = new ConstructorCallExpression(target.getDeclaringClass(), new ArgumentListExpression(transformedArgs));
cce.setSourcePosition(expr);
cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
return cce;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class NewifyASTTransformation method newifyClass.
private void newifyClass(ClassNode cNode, boolean autoFlag, ListExpression list) {
String cName = cNode.getName();
if (cNode.isInterface()) {
addError("Error processing interface '" + cName + "'. @" + MY_NAME + " not allowed for interfaces.", cNode);
}
final ListExpression oldClassesToNewify = classesToNewify;
final boolean oldAuto = auto;
classesToNewify = list;
auto = autoFlag;
super.visitClass(cNode);
classesToNewify = oldClassesToNewify;
auto = oldAuto;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.
the class NewifyASTTransformation method determineClasses.
/** allow non-strict mode in scripts because parsing not complete at that point */
private ListExpression determineClasses(Expression expr, boolean searchSourceUnit) {
ListExpression list = new ListExpression();
if (expr instanceof ClassExpression) {
list.addExpression(expr);
} else if (expr instanceof VariableExpression && searchSourceUnit) {
VariableExpression ve = (VariableExpression) expr;
ClassNode fromSourceUnit = getSourceUnitClass(ve);
if (fromSourceUnit != null) {
ClassExpression found = classX(fromSourceUnit);
found.setSourcePosition(ve);
list.addExpression(found);
} else {
addError(BASE_BAD_PARAM_ERROR + "an unresolvable reference to '" + ve.getName() + "'.", expr);
}
} else if (expr instanceof ListExpression) {
list = (ListExpression) expr;
final List<Expression> expressions = list.getExpressions();
for (int i = 0; i < expressions.size(); i++) {
Expression next = expressions.get(i);
if (next instanceof VariableExpression && searchSourceUnit) {
VariableExpression ve = (VariableExpression) next;
ClassNode fromSourceUnit = getSourceUnitClass(ve);
if (fromSourceUnit != null) {
ClassExpression found = classX(fromSourceUnit);
found.setSourcePosition(ve);
expressions.set(i, found);
} else {
addError(BASE_BAD_PARAM_ERROR + "a list containing an unresolvable reference to '" + ve.getName() + "'.", next);
}
} else if (!(next instanceof ClassExpression)) {
addError(BASE_BAD_PARAM_ERROR + "a list containing type: " + next.getType().getName() + ".", next);
}
}
checkDuplicateNameClashes(list);
} else if (expr != null) {
addError(BASE_BAD_PARAM_ERROR + "a type: " + expr.getType().getName() + ".", expr);
}
return list;
}
Aggregations