Search in sources :

Example 96 with ExpressionStatement

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

the class Verifier method addFieldInitialization.

protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
    Expression expression = fieldNode.getInitialExpression();
    if (expression != null) {
        final FieldExpression fe = new FieldExpression(fieldNode);
        if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
            fe.setUseReferenceDirectly(true);
        }
        ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
        if (fieldNode.isStatic()) {
            // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
            // initialized first so that code dependent on it does not see their values as empty
            Expression initialValueExpression = fieldNode.getInitialValueExpression();
            if (initialValueExpression instanceof ConstantExpression) {
                ConstantExpression cexp = (ConstantExpression) initialValueExpression;
                cexp = transformToPrimitiveConstantIfPossible(cexp);
                if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
                    // GROOVY-5150: primitive type constants will be initialized directly
                    return;
                }
                staticList.add(0, statement);
            } else {
                staticList.add(statement);
            }
            // to avoid double initialization in case of several constructors
            fieldNode.setInitialValueExpression(null);
            /*
                 * If it is a statement for an explicitly declared static field inside an enum, store its
                 * reference. For enums, they need to be handled differently as such init statements should
                 * come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
                 */
            if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
                initStmtsAfterEnumValuesInit.add(statement);
            }
        } else {
            list.add(statement);
        }
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 97 with ExpressionStatement

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

the class PluginUseScriptBlockMetadataExtractor method extract.

public void extract(SourceUnit sourceUnit, ScriptBlock scriptBlock) {
    ClosureExpression closureArg = scriptBlock.getClosureExpression();
    closureArg.getCode().visit(new RestrictiveCodeVisitor(sourceUnit, formatErrorMessage(BASE_MESSAGE)) {

        @Override
        public void visitBlockStatement(BlockStatement block) {
            for (Statement statement : block.getStatements()) {
                statement.visit(this);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!call.isImplicitThis()) {
                Expression target = call.getObjectExpression();
                if (!(target instanceof MethodCallExpression)) {
                    restrict(target, formatErrorMessage(BASE_MESSAGE));
                    return;
                }
                visitMethodCallExpression((MethodCallExpression) target);
            }
            if (call.getMethod() instanceof ConstantExpression) {
                ConstantExpression methodName = (ConstantExpression) call.getMethod();
                if (isOfType(methodName, String.class)) {
                    String methodNameText = methodName.getText();
                    if (methodNameText.equals("id") || methodNameText.equals("version")) {
                        ConstantExpression argumentExpression = hasSingleConstantArgOfType(call, String.class);
                        if (argumentExpression == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        String argStringValue = argumentExpression.getValue().toString();
                        if (argStringValue.length() == 0) {
                            restrict(argumentExpression, formatErrorMessage(NEED_SINGLE_STRING));
                            return;
                        }
                        if (methodName.getText().equals("id")) {
                            if (call.isImplicitThis()) {
                                try {
                                    DefaultPluginId.validate(argStringValue);
                                    call.setNodeMetaData(PluginDependencySpec.class, pluginRequestCollector.createSpec(call.getLineNumber()).id(argStringValue));
                                } catch (InvalidPluginIdException e) {
                                    restrict(argumentExpression, formatErrorMessage(e.getReason()));
                                }
                            } else {
                                restrict(call, formatErrorMessage(BASE_MESSAGE));
                            }
                        }
                        if (methodName.getText().equals("version")) {
                            PluginDependencySpec spec = getSpecFor(call);
                            if (spec == null) {
                                return;
                            }
                            spec.version(argStringValue);
                            call.setNodeMetaData(PluginDependencySpec.class, spec);
                        }
                    } else if (methodNameText.equals("apply")) {
                        ConstantExpression arguments = hasSingleConstantArgOfType(call, boolean.class);
                        if (arguments == null) {
                            restrict(call, formatErrorMessage(NEED_SINGLE_BOOLEAN));
                            return;
                        }
                        PluginDependencySpec spec = getSpecFor(call);
                        if (spec == null) {
                            return;
                        }
                        spec.apply((Boolean) arguments.getValue());
                    } else {
                        if (!call.isImplicitThis()) {
                            restrict(methodName, formatErrorMessage(EXTENDED_MESSAGE));
                        } else {
                            restrict(methodName, formatErrorMessage(BASE_MESSAGE));
                        }
                    }
                } else {
                    restrict(methodName, formatErrorMessage(NOT_LITERAL_ID_METHOD_NAME));
                }
            } else {
                restrict(call);
            }
        }

        private PluginDependencySpec getSpecFor(MethodCallExpression call) {
            Expression objectExpression = call.getObjectExpression();
            if (objectExpression instanceof MethodCallExpression) {
                return objectExpression.getNodeMetaData(PluginDependencySpec.class);
            } else {
                restrict(call, formatErrorMessage(BASE_MESSAGE));
                return null;
            }
        }

        @Override
        public void visitExpressionStatement(ExpressionStatement statement) {
            statement.getExpression().visit(this);
        }
    });
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) RestrictiveCodeVisitor(org.gradle.groovy.scripts.internal.RestrictiveCodeVisitor) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) InvalidPluginIdException(org.gradle.plugin.internal.InvalidPluginIdException) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) PluginDependencySpec(org.gradle.plugin.use.PluginDependencySpec) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 98 with ExpressionStatement

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

the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.

protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
    FieldNode fieldNode = classNode.getField(apiProperty);
    if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
        fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
        classNode.addField(fieldNode);
        String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
        Parameter setterParameter = new Parameter(implementationNode, apiProperty);
        BlockStatement setterBody = new BlockStatement();
        setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
        GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression)

Example 99 with ExpressionStatement

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

the class ASTValidationErrorsHelper method addSetErrorsMethod.

protected void addSetErrorsMethod(final ClassNode paramTypeClassNode) {
    final String errorsArgumentName = "$errorsArg";
    MethodNode setErrorsMethod = paramTypeClassNode.getMethod(SET_ERRORS_METHOD_NAME, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) });
    if (setErrorsMethod == null) {
        final Expression assignErrorsExpression = new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, new VariableExpression(errorsArgumentName));
        setErrorsMethod = new MethodNode(SET_ERRORS_METHOD_NAME, Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ERRORS_CLASS_NODE, errorsArgumentName) }, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, new ExpressionStatement(assignErrorsExpression));
        paramTypeClassNode.addMethod(setErrorsMethod);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) 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) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 100 with ExpressionStatement

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

the class TupleListTest method assertIterate.

protected void assertIterate(String methodName, Expression listExpression) throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));
    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
    block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement));
    classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    System.out.println("################ Now about to invoke method");
    try {
        InvokerHelper.invokeMethod(bean, methodName, null);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)123 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)79 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)59 Expression (org.codehaus.groovy.ast.expr.Expression)55 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)43 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