Search in sources :

Example 46 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.

the class ControllerActionTransformer method initializePrimitiveOrTypeWrapperParameter.

protected void initializePrimitiveOrTypeWrapperParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
    final ClassNode paramTypeClassNode = param.getType();
    final String methodParamName = param.getName();
    final Expression defaultValueExpression;
    if (paramTypeClassNode.equals(ClassHelper.Boolean_TYPE)) {
        defaultValueExpression = new ConstantExpression(false);
    } else if (PRIMITIVE_CLASS_NODES.contains(paramTypeClassNode)) {
        defaultValueExpression = new ConstantExpression(0);
    } else {
        defaultValueExpression = new ConstantExpression(null);
    }
    final ConstantExpression paramConstantExpression = new ConstantExpression(requestParameterName);
    final Expression paramsTypeConversionMethodArguments = new ArgumentListExpression(paramConstantExpression, /*, defaultValueExpression*/
    new ConstantExpression(null));
    final String conversionMethodName;
    if (TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.containsKey(paramTypeClassNode)) {
        conversionMethodName = TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.get(paramTypeClassNode);
    } else {
        conversionMethodName = paramTypeClassNode.getName();
    }
    Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
    final MethodCallExpression retrieveConvertedValueExpression = new MethodCallExpression(getParamsExpression, conversionMethodName, paramsTypeConversionMethodArguments);
    // choose any
    Class<?> defaultValueClass = null;
    if ("char".equals(conversionMethodName)) {
        // TypeConvertingMap.'char' method has 2 different signatures, choose the one with "Character 'char'(String name, Integer defaultValue)" signature
        defaultValueClass = Integer.class;
    }
    applyMethodTarget(retrieveConvertedValueExpression, TypeConvertingMap.class, null, defaultValueClass);
    final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(paramConstantExpression);
    final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
    final Token equalsToken = Token.newSymbol(Types.EQUALS, 0, 0);
    final VariableExpression convertedValueExpression = new VariableExpression("___converted_" + methodParamName, new ClassNode(Object.class));
    final DeclarationExpression declareConvertedValueExpression = new DeclarationExpression(convertedValueExpression, equalsToken, new EmptyExpression());
    Statement declareVariableStatement = new ExpressionStatement(declareConvertedValueExpression);
    wrapper.addStatement(declareVariableStatement);
    final VariableExpression methodParamExpression = new VariableExpression(methodParamName, paramTypeClassNode);
    final DeclarationExpression declareParameterVariableStatement = new DeclarationExpression(methodParamExpression, equalsToken, new EmptyExpression());
    declareVariableStatement = new ExpressionStatement(declareParameterVariableStatement);
    wrapper.addStatement(declareVariableStatement);
    final Expression assignmentExpression = new BinaryExpression(convertedValueExpression, equalsToken, new TernaryExpression(containsKeyExpression, retrieveConvertedValueExpression, defaultValueExpression));
    wrapper.addStatement(new ExpressionStatement(assignmentExpression));
    Expression rejectValueMethodCallExpression = getRejectValueExpression(classNode, methodParamName);
    BlockStatement ifConvertedValueIsNullBlockStatement = new BlockStatement();
    ifConvertedValueIsNullBlockStatement.addStatement(new ExpressionStatement(rejectValueMethodCallExpression));
    ifConvertedValueIsNullBlockStatement.addStatement(new ExpressionStatement(new BinaryExpression(methodParamExpression, equalsToken, defaultValueExpression)));
    final BooleanExpression isConvertedValueNullExpression = new BooleanExpression(new BinaryExpression(convertedValueExpression, Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), new ConstantExpression(null)));
    final ExpressionStatement assignConvertedValueToParamStatement = new ExpressionStatement(new BinaryExpression(methodParamExpression, equalsToken, convertedValueExpression));
    final Statement ifStatement = new IfStatement(isConvertedValueNullExpression, ifConvertedValueIsNullBlockStatement, assignConvertedValueToParamStatement);
    wrapper.addStatement(new IfStatement(new BooleanExpression(containsKeyExpression), ifStatement, new ExpressionStatement(new EmptyExpression())));
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) Token(org.codehaus.groovy.syntax.Token) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Map(java.util.Map) TypeConvertingMap(grails.util.TypeConvertingMap) HashMap(java.util.HashMap)

Example 47 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.

the class ControllerActionTransformer method initializeCommandObjectParameter.

protected void initializeCommandObjectParameter(final BlockStatement wrapper, final ClassNode commandObjectNode, final String paramName, SourceUnit source) {
    final ArgumentListExpression initializeCommandObjectArguments = new ArgumentListExpression();
    initializeCommandObjectArguments.addExpression(new ClassExpression(commandObjectNode));
    initializeCommandObjectArguments.addExpression(new ConstantExpression(paramName));
    final MethodCallExpression initializeCommandObjectMethodCall = new MethodCallExpression(new VariableExpression("this"), "initializeCommandObject", initializeCommandObjectArguments);
    applyDefaultMethodTarget(initializeCommandObjectMethodCall, commandObjectNode);
    final Expression assignCommandObjectToParameter = new BinaryExpression(new VariableExpression(paramName), Token.newSymbol(Types.EQUALS, 0, 0), initializeCommandObjectMethodCall);
    wrapper.addStatement(new ExpressionStatement(assignCommandObjectToParameter));
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 48 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.

the class ControllerActionTransformer method getRejectValueExpression.

protected Expression getRejectValueExpression(final ClassNode classNode, final String methodParamName) {
    ArgumentListExpression rejectValueArgs = new ArgumentListExpression();
    rejectValueArgs.addExpression(new ConstantExpression(methodParamName));
    rejectValueArgs.addExpression(new ConstantExpression("params." + methodParamName + ".conversion.error"));
    Expression getErrorsExpression = buildGetPropertyExpression(new VariableExpression("this", classNode), "errors", classNode);
    Expression rejectValueMethodCallExpression = applyDefaultMethodTarget(new MethodCallExpression(getErrorsExpression, "rejectValue", rejectValueArgs), Errors.class);
    return rejectValueMethodCallExpression;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 49 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.

the class ControllerActionTransformer method processClosures.

private void processClosures(ClassNode classNode, SourceUnit source, GeneratorContext context) {
    List<PropertyNode> propertyNodes = new ArrayList<PropertyNode>(classNode.getProperties());
    Expression initialExpression;
    ClosureExpression closureAction;
    for (PropertyNode property : propertyNodes) {
        initialExpression = property.getInitialExpression();
        if (!property.isStatic() && initialExpression != null && initialExpression.getClass().equals(ClosureExpression.class)) {
            closureAction = (ClosureExpression) initialExpression;
            if (converterEnabled) {
                transformClosureToMethod(classNode, closureAction, property, source, context);
            } else {
                addMethodToInvokeClosure(classNode, property, source, context);
            }
        }
    }
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ArrayList(java.util.ArrayList) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 50 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project grails-core by grails.

the class DefaultASTValidateableHelper method addValidateMethod.

protected void addValidateMethod(final ClassNode classNode) {
    String fieldsToValidateParameterName = "$fieldsToValidate";
    final MethodNode listArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, new Parameter[] { new Parameter(new ClassNode(List.class), fieldsToValidateParameterName) });
    if (listArgValidateMethod == null) {
        final BlockStatement validateMethodCode = new BlockStatement();
        final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
        validateInstanceArguments.addExpression(new VariableExpression("this"));
        validateInstanceArguments.addExpression(new VariableExpression(fieldsToValidateParameterName, ClassHelper.LIST_TYPE));
        final ClassNode validationSupportClassNode = ClassHelper.make(ValidationSupport.class);
        final StaticMethodCallExpression invokeValidateInstanceExpression = new StaticMethodCallExpression(validationSupportClassNode, "validateInstance", validateInstanceArguments);
        validateMethodCode.addStatement(new ExpressionStatement(invokeValidateInstanceExpression));
        final Parameter fieldsToValidateParameter = new Parameter(new ClassNode(List.class), fieldsToValidateParameterName);
        classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, new Parameter[] { fieldsToValidateParameter }, EMPTY_CLASS_ARRAY, validateMethodCode));
    }
    final MethodNode noArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, ZERO_PARAMETERS);
    if (noArgValidateMethod == null) {
        final BlockStatement validateMethodCode = new BlockStatement();
        final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
        validateInstanceArguments.addExpression(new CastExpression(new ClassNode(List.class), new ConstantExpression(null)));
        final Expression callListArgValidateMethod = new MethodCallExpression(new VariableExpression("this"), VALIDATE_METHOD_NAME, validateInstanceArguments);
        validateMethodCode.addStatement(new ReturnStatement(callListArgValidateMethod));
        classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, validateMethodCode));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

Expression (org.codehaus.groovy.ast.expr.Expression)369 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)257 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)244 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)187 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)187 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)152 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)151 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)149 ClassNode (org.codehaus.groovy.ast.ClassNode)146 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)134 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)130 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)113 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)103 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)95 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)84 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)78 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)63 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)63 Statement (org.codehaus.groovy.ast.stmt.Statement)63 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)61