use of org.codehaus.groovy.ast.expr.MethodCallExpression in project grails-core by grails.
the class ControllerActionTransformer method addMethodToInvokeClosure.
protected void addMethodToInvokeClosure(ClassNode controllerClassNode, PropertyNode closureProperty, SourceUnit source, GeneratorContext context) {
MethodNode method = controllerClassNode.getMethod(closureProperty.getName(), ZERO_PARAMETERS);
if (method == null || !method.getDeclaringClass().equals(controllerClassNode)) {
ClosureExpression closureExpression = (ClosureExpression) closureProperty.getInitialExpression();
final Parameter[] parameters = closureExpression.getParameters();
final BlockStatement newMethodCode = initializeActionParameters(controllerClassNode, closureProperty, closureProperty.getName(), parameters, source, context);
final ArgumentListExpression closureInvocationArguments = new ArgumentListExpression();
if (parameters != null) {
for (Parameter p : parameters) {
closureInvocationArguments.addExpression(new VariableExpression(p.getName()));
}
}
final MethodCallExpression methodCallExpression = new MethodCallExpression(closureExpression, "call", closureInvocationArguments);
newMethodCode.addStatement(new ExpressionStatement(applyMethodTarget(methodCallExpression, Closure.class, Object.class)));
final MethodNode methodNode = new MethodNode(closureProperty.getName(), Modifier.PUBLIC, new ClassNode(Object.class), ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, newMethodCode);
wrapMethodBodyWithExceptionHandling(controllerClassNode, methodNode);
annotateActionMethod(controllerClassNode, parameters, methodNode);
controllerClassNode.addMethod(methodNode);
}
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project grails-core by grails.
the class TestMixinTransformation method addJunitRuleFields.
protected void addJunitRuleFields(ClassNode classNode) {
if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
return;
}
ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
boolean spockTest = isSpockTest(classNode);
FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
if (spockTest) {
// @ClassRule must be added to @Shared field in spock
FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
} else {
staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
staticRuleFieldNode.addAnnotation(classRuleAnnotation);
}
FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
if (spockTest) {
addSpockFieldMetadata(ruleFieldNode, 0);
}
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class Verifier method visitConstructor.
public void visitConstructor(ConstructorNode node) {
CodeVisitorSupport checkSuper = new CodeVisitorSupport() {
boolean firstMethodCall = true;
String type = null;
public void visitMethodCallExpression(MethodCallExpression call) {
if (!firstMethodCall)
return;
firstMethodCall = false;
String name = call.getMethodAsString();
// the name might be null if the method name is a GString for example
if (name == null)
return;
if (!name.equals("super") && !name.equals("this"))
return;
type = name;
call.getArguments().visit(this);
type = null;
}
public void visitConstructorCallExpression(ConstructorCallExpression call) {
if (!call.isSpecialCall())
return;
type = call.getText();
call.getArguments().visit(this);
type = null;
}
public void visitVariableExpression(VariableExpression expression) {
if (type == null)
return;
String name = expression.getName();
if (!name.equals("this") && !name.equals("super"))
return;
throw new RuntimeParserException("cannot reference " + name + " inside of " + type + "(....) before supertype constructor has been called", expression);
}
};
Statement s = node.getCode();
if (s == null) {
return;
} else {
s.visit(new VerifierCodeVisitor(this));
}
s.visit(checkSuper);
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class BinaryExpressionHelper method evaluateArrayAssignmentWithOperator.
protected void evaluateArrayAssignmentWithOperator(String method, BinaryExpression expression, BinaryExpression leftBinExpr) {
CompileStack compileStack = getController().getCompileStack();
AsmClassGenerator acg = getController().getAcg();
OperandStack os = getController().getOperandStack();
// e.g. x[a] += b
// to avoid loading x and a twice we transform the expression to use
// ExpressionAsVariableSlot
// -> subscript=a, receiver=x, receiver[subscript]+b, =, receiver[subscript]
// -> subscript=a, receiver=x, receiver#getAt(subscript)#plus(b), =, receiver#putAt(subscript)
// -> subscript=a, receiver=x, receiver#putAt(subscript, receiver#getAt(subscript)#plus(b))
// the result of x[a] += b is x[a]+b, thus:
// -> subscript=a, receiver=x, receiver#putAt(subscript, ret=receiver#getAt(subscript)#plus(b)), ret
ExpressionAsVariableSlot subscript = new ExpressionAsVariableSlot(controller, leftBinExpr.getRightExpression(), "subscript");
ExpressionAsVariableSlot receiver = new ExpressionAsVariableSlot(controller, leftBinExpr.getLeftExpression(), "receiver");
MethodCallExpression getAt = new MethodCallExpression(receiver, "getAt", new ArgumentListExpression(subscript));
MethodCallExpression operation = new MethodCallExpression(getAt, method, expression.getRightExpression());
ExpressionAsVariableSlot ret = new ExpressionAsVariableSlot(controller, operation, "ret");
MethodCallExpression putAt = new MethodCallExpression(receiver, "putAt", new ArgumentListExpression(subscript, ret));
putAt.visit(acg);
os.pop();
os.load(ret.getType(), ret.getIndex());
compileStack.removeVar(ret.getIndex());
compileStack.removeVar(subscript.getIndex());
compileStack.removeVar(receiver.getIndex());
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class StatementWriter method writeForInLoop.
protected void writeForInLoop(ForStatement loop) {
controller.getAcg().onLineNumber(loop, "visitForLoop");
writeStatementLabel(loop);
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
// Declare the loop counter.
BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false);
// Then get the iterator and generate the loop control
MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression());
iterator.visit(controller.getAcg());
operandStack.doGroovyCast(ClassHelper.Iterator_TYPE);
final int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
Label continueLabel = compileStack.getContinueLabel();
Label breakLabel = compileStack.getBreakLabel();
mv.visitLabel(continueLabel);
mv.visitVarInsn(ALOAD, iteratorIdx);
writeIteratorHasNext(mv);
// note: ifeq tests for ==0, a boolean is 0 if it is false
mv.visitJumpInsn(IFEQ, breakLabel);
mv.visitVarInsn(ALOAD, iteratorIdx);
writeIteratorNext(mv);
operandStack.push(ClassHelper.OBJECT_TYPE);
operandStack.storeVar(variable);
// Generate the loop body
loop.getLoopBlock().visit(controller.getAcg());
mv.visitJumpInsn(GOTO, continueLabel);
mv.visitLabel(breakLabel);
compileStack.pop();
}
Aggregations