Search in sources :

Example 76 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy-core by groovy.

the class TraitASTTransformation method processProperty.

/**
     * Mostly copied from the {@link Verifier} class but does *not* generate bytecode
     *
     * @param cNode
     * @param node
     */
private static void processProperty(final ClassNode cNode, PropertyNode node) {
    String name = node.getName();
    FieldNode field = node.getField();
    int propNodeModifiers = node.getModifiers();
    String getterName = "get" + Verifier.capitalize(name);
    String setterName = "set" + Verifier.capitalize(name);
    // GROOVY-3726: clear volatile, transient modifiers so that they don't get applied to methods
    if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
        propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
    }
    if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
        propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
    }
    Statement getterBlock = node.getGetterBlock();
    if (getterBlock == null) {
        MethodNode getter = cNode.getGetterMethod(getterName);
        if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
            String secondGetterName = "is" + Verifier.capitalize(name);
            getter = cNode.getGetterMethod(secondGetterName);
        }
        if (!node.isPrivate() && methodNeedsReplacement(cNode, getter)) {
            getterBlock = new ExpressionStatement(new FieldExpression(field));
        }
    }
    Statement setterBlock = node.getSetterBlock();
    if (setterBlock == null) {
        // 2nd arg false below: though not usual, allow setter with non-void return type
        MethodNode setter = cNode.getSetterMethod(setterName, false);
        if (!node.isPrivate() && (propNodeModifiers & ACC_FINAL) == 0 && methodNeedsReplacement(cNode, setter)) {
            setterBlock = new ExpressionStatement(new BinaryExpression(new FieldExpression(field), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression("value")));
        }
    }
    if (getterBlock != null) {
        MethodNode getter = new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
        getter.setSynthetic(true);
        cNode.addMethod(getter);
        if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
            String secondGetterName = "is" + Verifier.capitalize(name);
            MethodNode secondGetter = new MethodNode(secondGetterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
            secondGetter.setSynthetic(true);
            cNode.addMethod(secondGetter);
        }
    }
    if (setterBlock != null) {
        Parameter[] setterParameterTypes = { new Parameter(node.getType(), "value") };
        VariableExpression var = (VariableExpression) ((BinaryExpression) ((ExpressionStatement) setterBlock).getExpression()).getRightExpression();
        var.setAccessedVariable(setterParameterTypes[0]);
        MethodNode setter = new MethodNode(setterName, propNodeModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
        setter.setSynthetic(true);
        cNode.addMethod(setter);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 77 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy-core by groovy.

the class TraitASTTransformation method processField.

private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
    Expression initialExpression = field.getInitialExpression();
    MethodNode selectedMethod = field.isStatic() ? staticInitializer : initializer;
    if (initialExpression != null) {
        VariableExpression thisObject = new VariableExpression(selectedMethod.getParameters()[0]);
        ExpressionStatement initCode = new ExpressionStatement(initialExpression);
        processBody(thisObject, selectedMethod, initCode, trait, fieldHelper, knownFields);
        BlockStatement code = (BlockStatement) selectedMethod.getCode();
        MethodCallExpression mce;
        if (field.isStatic()) {
            mce = new MethodCallExpression(new ClassExpression(INVOKERHELPER_CLASSNODE), "invokeStaticMethod", new ArgumentListExpression(thisObject, new ConstantExpression(Traits.helperSetterName(field)), initCode.getExpression()));
        } else {
            mce = new MethodCallExpression(new CastExpression(createReceiverType(field.isStatic(), fieldHelper), thisObject), Traits.helperSetterName(field), new CastExpression(field.getOriginType(), initCode.getExpression()));
        }
        mce.setImplicitThis(false);
        mce.setSourcePosition(initialExpression);
        code.addStatement(new ExpressionStatement(mce));
    }
    // define setter/getter helper methods
    fieldHelper.addMethod(Traits.helperSetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), new Parameter[] { new Parameter(field.getOriginType(), "val") }, ClassNode.EMPTY_ARRAY, null);
    fieldHelper.addMethod(Traits.helperGetterName(field), ACC_PUBLIC | ACC_ABSTRACT, field.getOriginType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
    // dummy fields are only used to carry annotations if instance field
    // and to differentiate from static fields otherwise
    String dummyFieldName = (field.isStatic() ? Traits.STATIC_FIELD_PREFIX : Traits.FIELD_PREFIX) + (field.isPublic() ? Traits.PUBLIC_FIELD_PREFIX : Traits.PRIVATE_FIELD_PREFIX) + Traits.remappedFieldName(field.getOwner(), field.getName());
    FieldNode dummyField = new FieldNode(dummyFieldName, ACC_STATIC | ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, field.getOriginType(), fieldHelper, null);
    // copy annotations from field to dummy field
    List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
    List<AnnotationNode> notCopied = new LinkedList<AnnotationNode>();
    GeneralUtils.copyAnnotatedNodeAnnotations(field, copied, notCopied);
    dummyField.addAnnotations(copied);
    fieldHelper.addField(dummyField);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) LinkedList(java.util.LinkedList) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 78 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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);
}
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) 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) Map(java.util.Map) TypeConvertingMap(grails.util.TypeConvertingMap) HashMap(java.util.HashMap)

Example 79 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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);
    }
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) RequestParameter(grails.web.RequestParameter) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 80 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project grails-core by grails.

the class DefaultASTValidateableHelper method addStaticInitializer.

private void addStaticInitializer(final ClassNode classNode) {
    final Expression nullOutConstrainedPropertiesExpression = new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.EQUALS, 0, 0), new ConstantExpression(null));
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(new ExpressionStatement(nullOutConstrainedPropertiesExpression));
    classNode.addStaticInitializerStatements(statements, true);
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) 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) 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)122 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)78 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)58 Expression (org.codehaus.groovy.ast.expr.Expression)54 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)52 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)51 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)49 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)44 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)39 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)35 Parameter (org.codehaus.groovy.ast.Parameter)30 MethodNode (org.codehaus.groovy.ast.MethodNode)29 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)29 ClassNode (org.codehaus.groovy.ast.ClassNode)27 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArrayList (java.util.ArrayList)25 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)24