Search in sources :

Example 6 with BooleanExpression

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

the class ControllerActionTransformer method wrapMethodBodyWithExceptionHandling.

/**
     * This will wrap the method body in a try catch block which does something
     * like this:
     * <pre>
     * try {
     *     // original method body here
     * } catch (Exception $caughtException) {
     *     Method $method = getExceptionHandlerMethod($caughtException.getClass())
     *     if($method) {
     *         return $method.invoke(this, $caughtException)
     *     } else {
     *         throw $caughtException
     *     }
     * }
     * </pre>
     * @param methodNode the method to add the try catch block to
     */
protected void wrapMethodBodyWithExceptionHandling(final ClassNode controllerClassNode, final MethodNode methodNode) {
    final BlockStatement catchBlockCode = new BlockStatement();
    final String caughtExceptionArgumentName = "$caughtException";
    final Expression caughtExceptionVariableExpression = new VariableExpression(caughtExceptionArgumentName);
    final Expression caughtExceptionTypeExpression = new PropertyExpression(caughtExceptionVariableExpression, "class");
    final Expression thisExpression = new VariableExpression("this");
    final MethodCallExpression getExceptionHandlerMethodCall = new MethodCallExpression(thisExpression, "getExceptionHandlerMethodFor", caughtExceptionTypeExpression);
    applyDefaultMethodTarget(getExceptionHandlerMethodCall, controllerClassNode);
    final ClassNode reflectMethodClassNode = new ClassNode(Method.class);
    final String exceptionHandlerMethodVariableName = "$method";
    final Expression exceptionHandlerMethodExpression = new VariableExpression(exceptionHandlerMethodVariableName, new ClassNode(Method.class));
    final Expression declareExceptionHandlerMethod = new DeclarationExpression(new VariableExpression(exceptionHandlerMethodVariableName, reflectMethodClassNode), Token.newSymbol(Types.EQUALS, 0, 0), getExceptionHandlerMethodCall);
    final ArgumentListExpression invokeArguments = new ArgumentListExpression();
    invokeArguments.addExpression(thisExpression);
    invokeArguments.addExpression(caughtExceptionVariableExpression);
    final MethodCallExpression invokeExceptionHandlerMethodExpression = new MethodCallExpression(new VariableExpression(exceptionHandlerMethodVariableName), "invoke", invokeArguments);
    applyDefaultMethodTarget(invokeExceptionHandlerMethodExpression, reflectMethodClassNode);
    final Statement returnStatement = new ReturnStatement(invokeExceptionHandlerMethodExpression);
    final Statement throwCaughtExceptionStatement = new ThrowStatement(caughtExceptionVariableExpression);
    final Statement ifExceptionHandlerMethodExistsStatement = new IfStatement(new BooleanExpression(exceptionHandlerMethodExpression), returnStatement, throwCaughtExceptionStatement);
    catchBlockCode.addStatement(new ExpressionStatement(declareExceptionHandlerMethod));
    catchBlockCode.addStatement(ifExceptionHandlerMethodExistsStatement);
    final CatchStatement catchStatement = new CatchStatement(new Parameter(new ClassNode(Exception.class), caughtExceptionArgumentName), catchBlockCode);
    final Statement methodBody = methodNode.getCode();
    BlockStatement tryBlock = new BlockStatement();
    BlockStatement codeToHandleAllowedMethods = getCodeToHandleAllowedMethods(controllerClassNode, methodNode.getName());
    tryBlock.addStatement(codeToHandleAllowedMethods);
    tryBlock.addStatement(methodBody);
    final TryCatchStatement tryCatchStatement = new TryCatchStatement(tryBlock, new EmptyStatement());
    tryCatchStatement.addCatch(catchStatement);
    final ArgumentListExpression argumentListExpression = new ArgumentListExpression();
    argumentListExpression.addExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME));
    final PropertyExpression requestPropertyExpression = new PropertyExpression(new VariableExpression("this"), "request");
    final Expression removeAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "removeAttribute", argumentListExpression);
    final Expression getAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "getAttribute", new ArgumentListExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME)));
    final VariableExpression attributeValueExpression = new VariableExpression("$allowed_methods_attribute_value", ClassHelper.make(Object.class));
    final Expression initializeAttributeValue = new DeclarationExpression(attributeValueExpression, Token.newSymbol(Types.EQUALS, 0, 0), getAttributeMethodCall);
    final Expression attributeValueMatchesMethodNameExpression = new BinaryExpression(new ConstantExpression(methodNode.getName()), Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), attributeValueExpression);
    final Statement ifAttributeValueMatchesMethodName = new IfStatement(new BooleanExpression(attributeValueMatchesMethodNameExpression), new ExpressionStatement(removeAttributeMethodCall), new EmptyStatement());
    final BlockStatement blockToRemoveAttribute = new BlockStatement();
    blockToRemoveAttribute.addStatement(new ExpressionStatement(initializeAttributeValue));
    blockToRemoveAttribute.addStatement(ifAttributeValueMatchesMethodName);
    final TryCatchStatement tryCatchToRemoveAttribute = new TryCatchStatement(blockToRemoveAttribute, new EmptyStatement());
    tryCatchToRemoveAttribute.addCatch(new CatchStatement(new Parameter(ClassHelper.make(Exception.class), "$exceptionRemovingAttribute"), new EmptyStatement()));
    tryCatchStatement.setFinallyStatement(tryCatchToRemoveAttribute);
    methodNode.setCode(tryCatchStatement);
}
Also used : 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DelegatingMethod(grails.compiler.DelegatingMethod) ControllerMethod(grails.web.controllers.ControllerMethod) Method(java.lang.reflect.Method) 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) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) 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) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) RequestParameter(grails.web.RequestParameter) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 7 with BooleanExpression

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

the class ControllerActionTransformer method initializeAndValidateCommandObjectParameter.

protected void initializeAndValidateCommandObjectParameter(final BlockStatement wrapper, final ClassNode controllerNode, final ClassNode commandObjectNode, final ASTNode actionNode, final String actionName, final String paramName, final SourceUnit source, final GeneratorContext context) {
    final DeclarationExpression declareCoExpression = new DeclarationExpression(new VariableExpression(paramName, commandObjectNode), Token.newSymbol(Types.EQUALS, 0, 0), new EmptyExpression());
    wrapper.addStatement(new ExpressionStatement(declareCoExpression));
    if (commandObjectNode.isInterface() || Modifier.isAbstract(commandObjectNode.getModifiers())) {
        final String warningMessage = "The [" + actionName + "] action in [" + controllerNode.getName() + "] accepts a parameter of type [" + commandObjectNode.getName() + "].  Interface types and abstract class types are not supported as command objects.  This parameter will be ignored.";
        GrailsASTUtils.warning(source, actionNode, warningMessage);
    } else {
        initializeCommandObjectParameter(wrapper, commandObjectNode, paramName, source);
        @SuppressWarnings("unchecked") boolean argumentIsValidateable = GrailsASTUtils.hasAnyAnnotations(commandObjectNode, grails.persistence.Entity.class, javax.persistence.Entity.class) || commandObjectNode.implementsInterface(ClassHelper.make(Validateable.class));
        if (!argumentIsValidateable && commandObjectNode.isPrimaryClassNode()) {
            final ModuleNode commandObjectModule = commandObjectNode.getModule();
            if (commandObjectModule != null && this.compilationUnit != null) {
                if (commandObjectModule == controllerNode.getModule() || doesModulePathIncludeSubstring(commandObjectModule, "grails-app" + File.separator + "controllers" + File.separator)) {
                    TraitInjectionUtils.injectTrait(compilationUnit, source, commandObjectNode, Validateable.class);
                    List<ConstructorNode> declaredConstructors = commandObjectNode.getDeclaredConstructors();
                    List<Statement> objectInitializerStatements = commandObjectNode.getObjectInitializerStatements();
                    if (declaredConstructors.isEmpty() && !objectInitializerStatements.isEmpty()) {
                        BlockStatement constructorLogic = new BlockStatement();
                        ConstructorNode constructorNode = new ConstructorNode(Modifier.PUBLIC, constructorLogic);
                        commandObjectNode.addConstructor(constructorNode);
                        constructorLogic.addStatements(objectInitializerStatements);
                    }
                    argumentIsValidateable = true;
                } else if (doesModulePathIncludeSubstring(commandObjectModule, "grails-app" + File.separator + "domain" + File.separator)) {
                    argumentIsValidateable = true;
                }
            }
        }
        if (argumentIsValidateable) {
            final MethodCallExpression validateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "validate", EMPTY_TUPLE);
            final MethodNode validateMethod = commandObjectNode.getMethod("validate", new Parameter[0]);
            if (validateMethod != null) {
                validateMethodCallExpression.setMethodTarget(validateMethod);
            }
            final Statement ifCommandObjectIsNotNullThenValidate = new IfStatement(new BooleanExpression(new VariableExpression(paramName)), new ExpressionStatement(validateMethodCallExpression), new ExpressionStatement(new EmptyExpression()));
            wrapper.addStatement(ifCommandObjectIsNotNullThenValidate);
        } else {
            // try to dynamically invoke the .validate() method if it is available at runtime...
            final Expression respondsToValidateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "respondsTo", new ArgumentListExpression(new ConstantExpression("validate")));
            final Expression validateMethodCallExpression = new MethodCallExpression(new VariableExpression(paramName), "validate", new ArgumentListExpression());
            final Statement ifRespondsToValidateThenValidateStatement = new IfStatement(new BooleanExpression(respondsToValidateMethodCallExpression), new ExpressionStatement(validateMethodCallExpression), new ExpressionStatement(new EmptyExpression()));
            final Statement ifCommandObjectIsNotNullThenValidate = new IfStatement(new BooleanExpression(new VariableExpression(paramName)), ifRespondsToValidateThenValidateStatement, new ExpressionStatement(new EmptyExpression()));
            wrapper.addStatement(ifCommandObjectIsNotNullThenValidate);
            final String warningMessage = "The [" + actionName + "] action accepts a parameter of type [" + commandObjectNode.getName() + "] which does not implement grails.validation.Validateable.  Data binding will still be applied " + "to this command object but the instance will not be validateable.";
            GrailsASTUtils.warning(source, actionNode, warningMessage);
        }
        if (GrailsASTUtils.isInnerClassNode(commandObjectNode)) {
            final String warningMessage = "The [" + actionName + "] action accepts a parameter of type [" + commandObjectNode.getName() + "] which is an inner class. Command object classes should not be inner classes.";
            GrailsASTUtils.warning(source, actionNode, warningMessage);
        } else {
            new DefaultASTDatabindingHelper().injectDatabindingCode(source, context, commandObjectNode);
        }
    }
}
Also used : 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) 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) DefaultASTDatabindingHelper(org.grails.web.databinding.DefaultASTDatabindingHelper) 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) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 8 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression 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 9 with BooleanExpression

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

the class DefaultASTValidateableHelper method addGetConstraintsMethod.

protected void addGetConstraintsMethod(final ClassNode classNode, boolean defaultNullable) {
    final String getConstraintsMethodName = "getConstraints";
    MethodNode getConstraintsMethod = classNode.getMethod(getConstraintsMethodName, ZERO_PARAMETERS);
    if (getConstraintsMethod == null || !getConstraintsMethod.getDeclaringClass().equals(classNode)) {
        final BooleanExpression isConstraintsPropertyNull = new BooleanExpression(new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), new ConstantExpression(null)));
        final BlockStatement ifConstraintsPropertyIsNullBlockStatement = new BlockStatement();
        final ArgumentListExpression getConstrainedPropertiesForClassArguments = new ArgumentListExpression();
        getConstrainedPropertiesForClassArguments.addExpression(new VariableExpression("this"));
        getConstrainedPropertiesForClassArguments.addExpression(new ConstantExpression(defaultNullable));
        final Expression getConstraintsMethodCall = new StaticMethodCallExpression(ClassHelper.make(ValidationSupport.class), "getConstrainedPropertiesForClass", getConstrainedPropertiesForClassArguments);
        final Expression initializeConstraintsFieldExpression = new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.EQUALS, 0, 0), getConstraintsMethodCall);
        final Statement ifConstraintsPropertyIsNullStatement = new IfStatement(isConstraintsPropertyNull, ifConstraintsPropertyIsNullBlockStatement, new ExpressionStatement(new EmptyExpression()));
        ifConstraintsPropertyIsNullBlockStatement.addStatement(new ExpressionStatement(initializeConstraintsFieldExpression));
        if (!defaultNullable) {
            final Map<String, ClassNode> propertiesToConstrain = getPropertiesToEnsureConstraintsFor(classNode);
            for (final Map.Entry<String, ClassNode> entry : propertiesToConstrain.entrySet()) {
                final String propertyName = entry.getKey();
                final ClassNode propertyType = entry.getValue();
                final String cpName = "$" + propertyName + "$constrainedProperty";
                final ArgumentListExpression constrainedPropertyConstructorArgumentList = new ArgumentListExpression();
                constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(classNode));
                constrainedPropertyConstructorArgumentList.addExpression(new ConstantExpression(propertyName));
                constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(propertyType));
                final ConstructorCallExpression constrainedPropertyCtorCallExpression = new ConstructorCallExpression(new ClassNode(ConstrainedProperty.class), constrainedPropertyConstructorArgumentList);
                final Expression declareConstrainedPropertyExpression = new DeclarationExpression(new VariableExpression(cpName, ClassHelper.OBJECT_TYPE), Token.newSymbol(Types.EQUALS, 0, 0), constrainedPropertyCtorCallExpression);
                final ArgumentListExpression applyConstraintMethodArgumentList = new ArgumentListExpression();
                applyConstraintMethodArgumentList.addExpression(new ConstantExpression(ConstrainedProperty.NULLABLE_CONSTRAINT));
                applyConstraintMethodArgumentList.addExpression(new ConstantExpression(defaultNullable));
                final Expression applyNullableConstraintMethodCallExpression = new MethodCallExpression(new VariableExpression(cpName), "applyConstraint", applyConstraintMethodArgumentList);
                final ArgumentListExpression putMethodArgumentList = new ArgumentListExpression();
                putMethodArgumentList.addExpression(new ConstantExpression(propertyName));
                putMethodArgumentList.addExpression(new VariableExpression(cpName));
                final MethodCallExpression addToConstraintsMapExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), "put", putMethodArgumentList);
                final BlockStatement addNullableConstraintBlock = new BlockStatement();
                addNullableConstraintBlock.addStatement(new ExpressionStatement(declareConstrainedPropertyExpression));
                addNullableConstraintBlock.addStatement(new ExpressionStatement(applyNullableConstraintMethodCallExpression));
                addNullableConstraintBlock.addStatement(new ExpressionStatement(addToConstraintsMapExpression));
                final Expression constraintsMapContainsKeyExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME, ClassHelper.make(Map.class)), "containsKey", new ArgumentListExpression(new ConstantExpression(propertyName)));
                final BooleanExpression ifPropertyIsAlreadyConstrainedExpression = new BooleanExpression(constraintsMapContainsKeyExpression);
                final Statement ifPropertyIsAlreadyConstrainedStatement = new IfStatement(ifPropertyIsAlreadyConstrainedExpression, new ExpressionStatement(new EmptyExpression()), addNullableConstraintBlock);
                ifConstraintsPropertyIsNullBlockStatement.addStatement(ifPropertyIsAlreadyConstrainedStatement);
            }
        }
        final BlockStatement methodBlockStatement = new BlockStatement();
        methodBlockStatement.addStatement(ifConstraintsPropertyIsNullStatement);
        final Statement returnStatement = new ReturnStatement(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME));
        methodBlockStatement.addStatement(returnStatement);
        final MethodNode methodNode = new MethodNode(getConstraintsMethodName, Modifier.STATIC | Modifier.PUBLIC, new ClassNode(Map.class), ZERO_PARAMETERS, null, methodBlockStatement);
        if (classNode.redirect() == null) {
            classNode.addMethod(methodNode);
        } else {
            classNode.redirect().addMethod(methodNode);
        }
    }
}
Also used : ValidationSupport(org.grails.web.plugins.support.ValidationSupport) 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) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ClassNode(org.codehaus.groovy.ast.ClassNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with BooleanExpression

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

the class BinaryExpressionTransformer method convertInOperatorToTernary.

private Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) {
    MethodCallExpression call = new MethodCallExpression(rightExpression, "isCase", leftExpression);
    call.setMethodTarget((MethodNode) bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
    call.setSourcePosition(bin);
    call.copyNodeMetaData(bin);
    TernaryExpression tExp = new TernaryExpression(new BooleanExpression(new BinaryExpression(rightExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null))), new BinaryExpression(leftExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)), call);
    return staticCompilationTransformer.transform(tExp);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Aggregations

BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)27 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)18 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)17 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)13 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)12 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)11 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)10 Expression (org.codehaus.groovy.ast.expr.Expression)10 Statement (org.codehaus.groovy.ast.stmt.Statement)10 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)9 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)8 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)8 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)8 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)8 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)7