Search in sources :

Example 56 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 57 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 58 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 59 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)

Example 60 with VariableExpression

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

the class VariableExpressionTest method testIsDynamicTypedExplicitObject.

public void testIsDynamicTypedExplicitObject() {
    VariableExpression intExpression = new VariableExpression("foo", new ClassNode(Object.class));
    assertFalse(intExpression.isDynamicTyped());
}
Also used : VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)176 Expression (org.codehaus.groovy.ast.expr.Expression)86 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)72 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)67 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)61 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)49 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)47 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)47 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)45 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)41 Parameter (org.codehaus.groovy.ast.Parameter)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