use of org.codehaus.groovy.ast.expr.ArgumentListExpression 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.ArgumentListExpression in project grails-core by grails.
the class TestMixinTransformation method addLegacyMixinFieldIfNonExistent.
protected static FieldNode addLegacyMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) {
ClassNode targetAwareInterface = GrailsASTUtils.findInterface(fieldType, new ClassNode(TestMixinTargetAware.class).getPlainNodeReference());
if (classNode != null && classNode.getField(fieldName) == null) {
Expression constructorArgument = new ArgumentListExpression();
if (targetAwareInterface != null) {
MapExpression namedArguments = new MapExpression();
namedArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("target"), new VariableExpression("this")));
constructorArgument = namedArguments;
}
return classNode.addField(fieldName, Modifier.PRIVATE, fieldType, new ConstructorCallExpression(fieldType, constructorArgument));
}
return null;
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression 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;
}
use of org.codehaus.groovy.ast.expr.ArgumentListExpression 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.ArgumentListExpression in project groovy-core by groovy.
the class EnumCompletionVisitor method transformConstructor.
/**
* If constructor does not define a call to super, then transform constructor
* to get String,int parameters at beginning and add call super(String,int).
*/
private void transformConstructor(ConstructorNode ctor, boolean isAic) {
boolean chainedThisConstructorCall = false;
ConstructorCallExpression cce = null;
if (ctor.firstStatementIsSpecialConstructorCall()) {
Statement code = ctor.getFirstStatement();
cce = (ConstructorCallExpression) ((ExpressionStatement) code).getExpression();
if (cce.isSuperCall())
return;
// must be call to this(...)
chainedThisConstructorCall = true;
}
// we need to add parameters
Parameter[] oldP = ctor.getParameters();
Parameter[] newP = new Parameter[oldP.length + 2];
String stringParameterName = getUniqueVariableName("__str", ctor.getCode());
newP[0] = new Parameter(ClassHelper.STRING_TYPE, stringParameterName);
String intParameterName = getUniqueVariableName("__int", ctor.getCode());
newP[1] = new Parameter(ClassHelper.int_TYPE, intParameterName);
System.arraycopy(oldP, 0, newP, 2, oldP.length);
ctor.setParameters(newP);
VariableExpression stringVariable = new VariableExpression(newP[0]);
VariableExpression intVariable = new VariableExpression(newP[1]);
if (chainedThisConstructorCall) {
TupleExpression args = (TupleExpression) cce.getArguments();
List<Expression> argsExprs = args.getExpressions();
argsExprs.add(0, stringVariable);
argsExprs.add(1, intVariable);
} else {
// add a super call
List<Expression> args = new ArrayList<Expression>();
args.add(stringVariable);
args.add(intVariable);
if (isAic) {
for (Parameter parameter : oldP) {
args.add(new VariableExpression(parameter.getName()));
}
}
cce = new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(args));
BlockStatement code = new BlockStatement();
code.addStatement(new ExpressionStatement(cce));
Statement oldCode = ctor.getCode();
if (oldCode != null)
code.addStatement(oldCode);
ctor.setCode(code);
}
}
Aggregations