Search in sources :

Example 1 with TypeConvertingMap

use of grails.util.TypeConvertingMap 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)

Aggregations

TypeConvertingMap (grails.util.TypeConvertingMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)1 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)1 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)1 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)1 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)1 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)1 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)1 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)1 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)1 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)1 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)1