use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class SuperCallTraitTransformer method transformMethodCallExpression.
private Expression transformMethodCallExpression(final MethodCallExpression exp) {
Expression objectExpression = transform(exp.getObjectExpression());
ClassNode traitReceiver = objectExpression.getNodeMetaData(SuperCallTraitTransformer.class);
if (traitReceiver != null) {
TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
// (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
ClassExpression receiver = new ClassExpression(helpers.getHelper());
ArgumentListExpression newArgs = new ArgumentListExpression();
Expression arguments = exp.getArguments();
newArgs.addExpression(new VariableExpression("this"));
if (arguments instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
for (Expression expression : expressions) {
newArgs.addExpression(transform(expression));
}
} else {
newArgs.addExpression(arguments);
}
MethodCallExpression result = new MethodCallExpression(receiver, exp.getMethod(), newArgs);
result.setImplicitThis(false);
result.setSpreadSafe(exp.isSpreadSafe());
result.setSafe(exp.isSafe());
result.setSourcePosition(exp);
return result;
}
return super.transform(exp);
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class TraitASTTransformation method processField.
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
Expression initialExpression = field.getInitialExpression();
MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
if (initialExpression != null) {
VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
ExpressionStatement initCode = new ExpressionStatement(initialExpression);
processBody(thisObject, selectedMethod, initCode, trait, fieldHelper, knownFields);
BlockStatement code = (BlockStatement) selectedMethod.getCode();
MethodCallExpression mce;
if (field.isStatic()) {
mce = new MethodCallExpression(new ClassExpression(INVOKERHELPER_CLASSNODE), "invokeStaticMethod", new ArgumentListExpression(thisObject, new ConstantExpression(Traits.helperSetterName(field)), initCode.getExpression()));
} else {
mce = new MethodCallExpression(new CastExpression(createReceiverType(field.isStatic(), fieldHelper), thisObject), Traits.helperSetterName(field), new CastExpression(field.getOriginType(), initCode.getExpression()));
}
mce.setImplicitThis(false);
mce.setSourcePosition(initialExpression);
code.addStatement(new ExpressionStatement(mce));
}
// define setter/getter helper methods
fieldHelper.addMethod(Traits.helperSetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), new Parameter[] { new Parameter(field.getOriginType(), "val") }, ClassNode.EMPTY_ARRAY, null);
fieldHelper.addMethod(Traits.helperGetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
// dummy fields are only used to carry annotations if instance field
// and to differentiate from static fields otherwise
String dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) + (field.isPublic() ? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX) + Traits.remappedFieldName(field.getOwner(), field.getName());
FieldNode dummyField = new FieldNode(dummyFieldName, ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
// copy annotations from field to dummy field
List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
List<AnnotationNode> notCopied = new LinkedList<AnnotationNode>();
GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
dummyField.addAnnotations(copied);
fieldHelper.addField(dummyField);
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy-core by groovy.
the class MethodCallExpressionTransformer method transformToMopSuperCall.
private MethodCallExpression transformToMopSuperCall(final ClassNode superCallReceiver, final MethodCallExpression expr) {
MethodNode mn = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
String mopName = MopWriter.getMopMethodName(mn, false);
MethodNode direct = new MethodNode(mopName, ACC_PUBLIC | ACC_SYNTHETIC, mn.getReturnType(), mn.getParameters(), mn.getExceptions(), EmptyStatement.INSTANCE);
direct.setDeclaringClass(superCallReceiver);
MethodCallExpression result = new MethodCallExpression(new VariableExpression("this"), mopName, expr.getArguments());
result.setImplicitThis(true);
result.setSpreadSafe(false);
result.setSafe(false);
result.setSourcePosition(expr);
result.setMethodTarget(direct);
return result;
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project grails-core by grails.
the class ControllerActionTransformer method initializeStringParameter.
protected void initializeStringParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
final ClassNode paramTypeClassNode = param.getType();
final String methodParamName = param.getName();
Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(new ConstantExpression(requestParameterName));
final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
final Statement initializeParameterStatement = new ExpressionStatement(new DeclarationExpression(new VariableExpression(methodParamName, paramTypeClassNode), Token.newSymbol(Types.EQUALS, 0, 0), new TernaryExpression(containsKeyExpression, buildGetMapExpression(getParamsExpression, requestParameterName), new ConstantExpression(null))));
wrapper.addStatement(initializeParameterStatement);
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project grails-core by grails.
the class ControllerActionTransformer method addOriginalMethodCall.
private Statement addOriginalMethodCall(MethodNode methodNode, BlockStatement blockStatement) {
if (blockStatement == null) {
return null;
}
final ArgumentListExpression arguments = new ArgumentListExpression();
for (Parameter p : methodNode.getParameters()) {
arguments.addExpression(new VariableExpression(p.getName(), p.getType()));
}
MethodCallExpression callExpression = new MethodCallExpression(new VariableExpression("this"), methodNode.getName(), arguments);
callExpression.setMethodTarget(methodNode);
blockStatement.addStatement(new ReturnStatement(callExpression));
return blockStatement;
}
Aggregations