Search in sources :

Example 26 with VariableExpression

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

the class FinalVariableAnalyzer method visitBinaryExpression.

@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    boolean assignment = StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType());
    boolean isDeclaration = expression instanceof DeclarationExpression;
    Expression leftExpression = expression.getLeftExpression();
    Expression rightExpression = expression.getRightExpression();
    if (isDeclaration && leftExpression instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) leftExpression;
        if (Modifier.isFinal(var.getModifiers())) {
            declaredFinalVariables.add(var);
        }
    }
    leftExpression.visit(this);
    inAssignment = assignment;
    rightExpression.visit(this);
    inAssignment = false;
    if (assignment) {
        if (leftExpression instanceof Variable) {
            boolean uninitialized = isDeclaration && rightExpression == EmptyExpression.INSTANCE;
            recordAssignment((Variable) leftExpression, isDeclaration, uninitialized, false, expression);
        } else if (leftExpression instanceof TupleExpression) {
            TupleExpression te = (TupleExpression) leftExpression;
            for (Expression next : te.getExpressions()) {
                if (next instanceof Variable) {
                    recordAssignment((Variable) next, isDeclaration, false, false, next);
                }
            }
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 27 with VariableExpression

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

the class FinalVariableAnalyzer method visitBlockStatement.

@Override
public void visitBlockStatement(final BlockStatement block) {
    Set<VariableExpression> old = declaredFinalVariables;
    declaredFinalVariables = new HashSet<VariableExpression>();
    super.visitBlockStatement(block);
    if (callback != null) {
        Map<Variable, VariableState> state = getState();
        for (VariableExpression declaredFinalVariable : declaredFinalVariables) {
            VariableState variableState = state.get(declaredFinalVariable.getAccessedVariable());
            if (variableState == null || variableState != VariableState.is_final) {
                callback.variableNotAlwaysInitialized(declaredFinalVariable);
            }
        }
    }
    declaredFinalVariables = old;
}
Also used : Variable(org.codehaus.groovy.ast.Variable) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 28 with VariableExpression

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

the class ForTest method testNonLoop.

public void testNonLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    Parameter[] parameters = { new Parameter(ClassHelper.OBJECT_TYPE, "coll") };
    Statement statement = createPrintlnStatement(new VariableExpression("coll"));
    classNode.addMethod(new MethodNode("oneParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));
    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 a method without looping");
    Object value = new Integer(10000);
    try {
        InvokerHelper.invokeMethod(bean, "oneParamDemo", new Object[] { value });
    } 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) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 29 with VariableExpression

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

the class ForTest method testManyParam.

public void testManyParam() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    Parameter[] parameters = { new Parameter(ClassHelper.OBJECT_TYPE, "coll1"), new Parameter(ClassHelper.OBJECT_TYPE, "coll2"), new Parameter(ClassHelper.OBJECT_TYPE, "coll3") };
    BlockStatement statement = new BlockStatement();
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll1")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll2")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll3")));
    classNode.addMethod(new MethodNode("manyParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));
    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 a method with many parameters");
    Object[] array = { new Integer(1000 * 1000), "foo-", "bar~" };
    try {
        InvokerHelper.invokeMethod(bean, "manyParamDemo", array);
    } 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 : InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 30 with VariableExpression

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

the class VariableExpressionTest method testIsDynamicTyped_DYNMAMIC_TYPE.

public void testIsDynamicTyped_DYNMAMIC_TYPE() {
    VariableExpression intExpression = new VariableExpression("foo", ClassHelper.DYNAMIC_TYPE);
    assertTrue(intExpression.isDynamicTyped());
}
Also used : VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)174 Expression (org.codehaus.groovy.ast.expr.Expression)84 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)84 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)76 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)70 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)66 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)59 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)58 ClassNode (org.codehaus.groovy.ast.ClassNode)52 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)47 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)47 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)46 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)43 Parameter (org.codehaus.groovy.ast.Parameter)40 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)40 MethodNode (org.codehaus.groovy.ast.MethodNode)37 FieldNode (org.codehaus.groovy.ast.FieldNode)36 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)35 Statement (org.codehaus.groovy.ast.stmt.Statement)33