use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class MacroCallTransformingVisitor method visitMethodCallExpression.
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
super.visitMethodCallExpression(call);
final List<Expression> callArguments;
if (call.getArguments() instanceof TupleExpression) {
callArguments = ((TupleExpression) call.getArguments()).getExpressions();
} else {
callArguments = Collections.singletonList(call.getArguments());
}
List<MethodNode> macroMethods = findMacroMethods(call.getMethodAsString(), callArguments);
if (macroMethods.isEmpty()) {
// Early return to avoid macro context and arguments creation
return;
}
MacroContext macroContext = new MacroContext(unit, sourceUnit, call);
Object[] macroArguments = new Object[callArguments.size() + 1];
macroArguments[0] = macroContext;
System.arraycopy(callArguments.toArray(), 0, macroArguments, 1, callArguments.size());
for (MethodNode macroMethodNode : macroMethods) {
if (!(macroMethodNode instanceof ExtensionMethodNode)) {
throw new IllegalStateException(macroMethodNode + " is not an instance of ExtensionMethodNode");
}
if (tryMacroMethod(call, (ExtensionMethodNode) macroMethodNode, macroArguments)) {
break;
}
}
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class MacroGroovyMethods method getMacroArguments.
protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) {
Expression macroCallArguments = call.getArguments();
if (macroCallArguments == null) {
source.addError(new SyntaxException("Call should have arguments" + '\n', call));
return null;
}
if (!(macroCallArguments instanceof TupleExpression)) {
source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments));
return null;
}
TupleExpression tupleArguments = (TupleExpression) macroCallArguments;
if (tupleArguments.getExpressions() == null) {
source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments));
return null;
}
return tupleArguments;
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class MarkupBuilderCodeTransformer method tryTransformInclude.
private Expression tryTransformInclude(final MethodCallExpression exp) {
Expression arguments = exp.getArguments();
if (arguments instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
MapExpression map = (MapExpression) expressions.get(0);
List<MapEntryExpression> entries = map.getMapEntryExpressions();
if (entries.size() == 1) {
MapEntryExpression mapEntry = entries.get(0);
Expression keyExpression = mapEntry.getKeyExpression();
try {
IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
call.setImplicitThis(true);
call.setSafe(exp.isSafe());
call.setSpreadSafe(exp.isSpreadSafe());
call.setSourcePosition(exp);
return call;
} catch (IllegalArgumentException e) {
// not a valid import type, do not modify the code
}
}
}
}
return super.transform(exp);
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class ContractClosureWriter method createClosureClass.
public ClassNode createClosureClass(ClassNode classNode, MethodNode methodNode, ClosureExpression expression, boolean addOldVariable, boolean addResultVariable, int mods) {
ClassNode outerClass = getOutermostClass(classNode);
String name = outerClass.getName() + "$" + getClosureInnerName(outerClass, classNode);
// fetch all method parameters, and possibly add 'old' and 'result'
ArrayList<Parameter> parametersTemp = new ArrayList<Parameter>(Arrays.asList(expression.getParameters()));
removeParameter("old", parametersTemp);
removeParameter("result", parametersTemp);
if (methodNode != null && addResultVariable && !isPrimitiveVoid(methodNode.getReturnType())) {
parametersTemp.add(new Parameter(methodNode.getReturnType(), "result"));
}
if (addOldVariable) {
parametersTemp.add(new Parameter(new ClassNode(Map.class), "old"));
}
// contains all params of the original method
ArrayList<Parameter> closureParameters = new ArrayList<Parameter>();
for (Parameter param : parametersTemp) {
Parameter closureParameter = new Parameter(param.getType().getPlainNodeReference(), param.getName());
closureParameters.add(closureParameter);
}
ClassNode answer = new ClassNode(name, mods | ACC_FINAL, ClassHelper.CLOSURE_TYPE.getPlainNodeReference());
answer.setSynthetic(true);
answer.setSourcePosition(expression);
MethodNode method = answer.addMethod("doCall", ACC_PUBLIC, ClassHelper.Boolean_TYPE, closureParameters.toArray(new Parameter[closureParameters.size()]), ClassNode.EMPTY_ARRAY, expression.getCode());
method.setSourcePosition(expression);
VariableScope varScope = expression.getVariableScope();
if (varScope == null) {
throw new RuntimeException("Must have a VariableScope by now! for expression: " + expression + " class: " + name);
} else {
method.setVariableScope(varScope.copy());
}
// let's add a typesafe call method
ArgumentListExpression arguments = new ArgumentListExpression();
for (Parameter parameter : closureParameters) {
arguments.addExpression(varX(parameter));
}
MethodNode call = answer.addMethod("call", ACC_PUBLIC, ClassHelper.Boolean_TYPE, closureParameters.toArray(new Parameter[closureParameters.size()]), ClassNode.EMPTY_ARRAY, returnS(callThisX("doCall", arguments)));
call.setSourcePosition(expression);
call.setSynthetic(true);
// let's make the constructor
BlockStatement block = new BlockStatement();
// this block does not get a source position, because we don't
// want this synthetic constructor to show up in corbertura reports
VariableExpression outer = varX("_outerInstance");
outer.setSourcePosition(expression);
block.getVariableScope().putReferencedLocalVariable(outer);
VariableExpression thisObject = varX("_thisObject");
thisObject.setSourcePosition(expression);
block.getVariableScope().putReferencedLocalVariable(thisObject);
TupleExpression conArgs = new TupleExpression(outer, thisObject);
block.addStatement(stmt(ctorSuperX(conArgs)));
Parameter[] consParams = params(param(ClassHelper.OBJECT_TYPE, "_outerInstance"), param(ClassHelper.OBJECT_TYPE, "_thisObject"));
ASTNode sn = answer.addConstructor(ACC_PUBLIC, consParams, ClassNode.EMPTY_ARRAY, block);
sn.setSourcePosition(expression);
correctAccessedVariable(method, expression);
return answer;
}
use of org.codehaus.groovy.ast.expr.TupleExpression in project groovy by apache.
the class TraitReceiverTransformer method createArgumentList.
private ArgumentListExpression createArgumentList(final Expression self, final Expression arguments) {
ArgumentListExpression newArgs = new ArgumentListExpression();
newArgs.addExpression(self);
if (arguments instanceof TupleExpression) {
for (Expression argument : (TupleExpression) arguments) {
newArgs.addExpression(transform(argument));
}
} else {
newArgs.addExpression(transform(arguments));
}
return newArgs;
}
Aggregations