Search in sources :

Example 1 with MapEntryExpression

use of org.codehaus.groovy.ast.expr.MapEntryExpression in project groovy by apache.

the class MarkupBuilderCodeTransformer method tryTransformInclude.

private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                // not a valid import type, do not modify the code
                }
            }
        }
    }
    return super.transform(exp);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) 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) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 2 with MapEntryExpression

use of org.codehaus.groovy.ast.expr.MapEntryExpression in project groovy by apache.

the class StaticImportVisitor method transformMapEntryExpression.

// if you have a Bar class with a static foo property, and this:
//   import static Bar.foo as baz
// then this constructor (not normal usage of statics):
//   new Bar(baz:1)
// will become:
//   new Bar(foo:1)
private Expression transformMapEntryExpression(MapEntryExpression me, ClassNode constructorCallType) {
    Expression key = me.getKeyExpression();
    Expression value = me.getValueExpression();
    ModuleNode module = currentClass.getModule();
    if (module != null && key instanceof ConstantExpression) {
        Map<String, ImportNode> importNodes = module.getStaticImports();
        if (importNodes.containsKey(key.getText())) {
            ImportNode importNode = importNodes.get(key.getText());
            if (importNode.getType().equals(constructorCallType)) {
                String newKey = importNode.getFieldName();
                return new MapEntryExpression(new ConstantExpression(newKey), value.transformExpression(this));
            }
        }
    }
    return me;
}
Also used : MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) 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) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 3 with MapEntryExpression

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

the class ControllerActionTransformer method getCodeToHandleAllowedMethods.

protected BlockStatement getCodeToHandleAllowedMethods(ClassNode controllerClass, String methodName) {
    GrailsASTUtils.addEnhancedAnnotation(controllerClass, DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY);
    final BlockStatement checkAllowedMethodsBlock = new BlockStatement();
    final PropertyExpression requestPropertyExpression = new PropertyExpression(new VariableExpression("this"), "request");
    final FieldNode allowedMethodsField = controllerClass.getField(DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY);
    if (allowedMethodsField != null) {
        final Expression initialAllowedMethodsExpression = allowedMethodsField.getInitialExpression();
        if (initialAllowedMethodsExpression instanceof MapExpression) {
            boolean actionIsRestricted = false;
            final MapExpression allowedMethodsMapExpression = (MapExpression) initialAllowedMethodsExpression;
            final List<MapEntryExpression> allowedMethodsMapEntryExpressions = allowedMethodsMapExpression.getMapEntryExpressions();
            for (MapEntryExpression allowedMethodsMapEntryExpression : allowedMethodsMapEntryExpressions) {
                final Expression allowedMethodsMapEntryKeyExpression = allowedMethodsMapEntryExpression.getKeyExpression();
                if (allowedMethodsMapEntryKeyExpression instanceof ConstantExpression) {
                    final ConstantExpression allowedMethodsMapKeyConstantExpression = (ConstantExpression) allowedMethodsMapEntryKeyExpression;
                    final Object allowedMethodsMapKeyValue = allowedMethodsMapKeyConstantExpression.getValue();
                    if (methodName.equals(allowedMethodsMapKeyValue)) {
                        actionIsRestricted = true;
                        break;
                    }
                }
            }
            if (actionIsRestricted) {
                final PropertyExpression responsePropertyExpression = new PropertyExpression(new VariableExpression("this"), "response");
                final ArgumentListExpression isAllowedArgumentList = new ArgumentListExpression();
                isAllowedArgumentList.addExpression(new ConstantExpression(methodName));
                isAllowedArgumentList.addExpression(new PropertyExpression(new VariableExpression("this"), "request"));
                isAllowedArgumentList.addExpression(new PropertyExpression(new VariableExpression("this"), DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY));
                final Expression isAllowedMethodCall = new StaticMethodCallExpression(ClassHelper.make(AllowedMethodsHelper.class), "isAllowed", isAllowedArgumentList);
                final BooleanExpression isValidRequestMethod = new BooleanExpression(isAllowedMethodCall);
                final MethodCallExpression sendErrorMethodCall = new MethodCallExpression(responsePropertyExpression, "sendError", new ConstantExpression(HttpServletResponse.SC_METHOD_NOT_ALLOWED));
                final ReturnStatement returnStatement = new ReturnStatement(new ConstantExpression(null));
                final BlockStatement blockToSendError = new BlockStatement();
                blockToSendError.addStatement(new ExpressionStatement(sendErrorMethodCall));
                blockToSendError.addStatement(returnStatement);
                final IfStatement ifIsValidRequestMethodStatement = new IfStatement(isValidRequestMethod, new ExpressionStatement(new EmptyExpression()), blockToSendError);
                checkAllowedMethodsBlock.addStatement(ifIsValidRequestMethodStatement);
            }
        }
    }
    final ArgumentListExpression argumentListExpression = new ArgumentListExpression();
    argumentListExpression.addExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME));
    argumentListExpression.addExpression(new ConstantExpression(methodName));
    final Expression setAttributeMethodCall = new MethodCallExpression(requestPropertyExpression, "setAttribute", argumentListExpression);
    final BlockStatement codeToExecuteIfAttributeIsNotSet = new BlockStatement();
    codeToExecuteIfAttributeIsNotSet.addStatement(new ExpressionStatement(setAttributeMethodCall));
    codeToExecuteIfAttributeIsNotSet.addStatement(checkAllowedMethodsBlock);
    final BooleanExpression attributeIsSetBooleanExpression = new BooleanExpression(new MethodCallExpression(requestPropertyExpression, "getAttribute", new ArgumentListExpression(new ConstantExpression(ALLOWED_METHODS_HANDLED_ATTRIBUTE_NAME))));
    final Statement ifAttributeIsAlreadySetStatement = new IfStatement(attributeIsSetBooleanExpression, new EmptyStatement(), codeToExecuteIfAttributeIsNotSet);
    final BlockStatement code = new BlockStatement();
    code.addStatement(ifAttributeIsAlreadySetStatement);
    return code;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) 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) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) AllowedMethodsHelper(grails.artefact.controller.support.AllowedMethodsHelper) 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) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) 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) 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) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 4 with MapEntryExpression

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

the class MarkupBuilderCodeTransformer method tryTransformInclude.

private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                // not a valid import type, do not modify the code
                }
            }
        }
    }
    return super.transform(exp);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) 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) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 5 with MapEntryExpression

use of org.codehaus.groovy.ast.expr.MapEntryExpression 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;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)7 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)7 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)6 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)6 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)6 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)5 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)5 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)5 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)5 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)5 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)5 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)5 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)4 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)3 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)3 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)3 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)2 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)2 GStringExpression (org.codehaus.groovy.ast.expr.GStringExpression)2