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);
}
}
}
}
}
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;
}
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");
}
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");
}
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());
}
Aggregations