use of org.codehaus.groovy.ast.expr.ListExpression in project groovy-core by groovy.
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-core by groovy.
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;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy-core by groovy.
the class NewifyASTTransformation method newifyMethodOrField.
private void newifyMethodOrField(AnnotatedNode parent, boolean autoFlag, ListExpression list) {
final ListExpression oldClassesToNewify = classesToNewify;
final boolean oldAuto = auto;
checkClassLevelClashes(list);
checkAutoClash(autoFlag, parent);
classesToNewify = list;
auto = autoFlag;
if (parent instanceof FieldNode) {
super.visitField((FieldNode) parent);
} else {
super.visitMethod((MethodNode) parent);
}
classesToNewify = oldClassesToNewify;
auto = oldAuto;
}
use of org.codehaus.groovy.ast.expr.ListExpression in project groovy-core by groovy.
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-core by groovy.
the class AbstractASTTransformation method getClassList.
public List<ClassNode> getClassList(AnnotationNode anno, String name) {
List<ClassNode> list = new ArrayList<ClassNode>();
Expression expr = anno.getMember(name);
if (expr != null && expr instanceof ListExpression) {
final ListExpression listExpression = (ListExpression) expr;
for (Expression itemExpr : listExpression.getExpressions()) {
if (itemExpr != null && itemExpr instanceof ClassExpression) {
ClassNode cn = itemExpr.getType();
if (cn != null)
list.add(cn);
}
}
} else if (expr != null && expr instanceof ClassExpression) {
ClassNode cn = expr.getType();
if (cn != null)
list.add(cn);
}
return list;
}
Aggregations