Search in sources :

Example 21 with Variable

use of org.codehaus.groovy.ast.Variable in project groovy by apache.

the class FinalVariableAnalyzer method visitVariableExpression.

@Override
public void visitVariableExpression(final VariableExpression expression) {
    super.visitVariableExpression(expression);
    if (inAssignment) {
        Map<Variable, VariableState> state = getState();
        Variable key = expression.getAccessedVariable();
        VariableState variableState = state.get(key);
        if (variableState == VariableState.is_uninitialized) {
            variableState = VariableState.is_var;
            state.put(key, variableState);
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable)

Example 22 with Variable

use of org.codehaus.groovy.ast.Variable 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 23 with Variable

use of org.codehaus.groovy.ast.Variable in project groovy by apache.

the class StaticVerifier method visitConstructorOrMethod.

@Override
public void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    MethodNode oldCurrentMethod = currentMethod;
    currentMethod = node;
    super.visitConstructorOrMethod(node, isConstructor);
    if (isConstructor) {
        final Set<String> exceptions = new HashSet<String>();
        for (final Parameter param : node.getParameters()) {
            exceptions.add(param.getName());
            if (param.hasInitialExpression()) {
                param.getInitialExpression().visit(new CodeVisitorSupport() {

                    @Override
                    public void visitVariableExpression(VariableExpression ve) {
                        if (exceptions.contains(ve.getName()))
                            return;
                        Variable av = ve.getAccessedVariable();
                        if (av instanceof DynamicVariable || !av.isInStaticContext()) {
                            addVariableError(ve);
                        }
                    }

                    @Override
                    public void visitMethodCallExpression(MethodCallExpression call) {
                        Expression objectExpression = call.getObjectExpression();
                        if (objectExpression instanceof VariableExpression) {
                            VariableExpression ve = (VariableExpression) objectExpression;
                            if (ve.isThisExpression()) {
                                addError("Can't access instance method '" + call.getMethodAsString() + "' for a constructor parameter default value", param);
                                return;
                            }
                        }
                        super.visitMethodCallExpression(call);
                    }

                    @Override
                    public void visitClosureExpression(ClosureExpression expression) {
                    //skip contents, because of dynamic scope
                    }
                });
            }
        }
    }
    currentMethod = oldCurrentMethod;
}
Also used : Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) HashSet(java.util.HashSet)

Example 24 with Variable

use of org.codehaus.groovy.ast.Variable in project spock by spockframework.

the class InstanceFieldAccessChecker method visitVariableExpression.

@Override
public void visitVariableExpression(VariableExpression expr) {
    super.visitVariableExpression(expr);
    Variable var = expr.getAccessedVariable();
    if (!(var instanceof FieldNode))
        return;
    checkFieldAccess(expr, (FieldNode) var);
}
Also used : Variable(org.codehaus.groovy.ast.Variable) FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 25 with Variable

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

the class BinaryExpressionMultiTypeDispatcher method doAssignmentToLocalVariable.

private boolean doAssignmentToLocalVariable(String method, BinaryExpression binExp) {
    Expression left = binExp.getLeftExpression();
    if (left instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) left;
        Variable v = ve.getAccessedVariable();
        if (v instanceof DynamicVariable)
            return false;
        if (v instanceof PropertyExpression)
            return false;
    /* field and declaration we don't return false */
    } else {
        return false;
    }
    evaluateBinaryExpression(method, binExp);
    getController().getOperandStack().dup();
    getController().getCompileStack().pushLHS(true);
    binExp.getLeftExpression().visit(getController().getAcg());
    getController().getCompileStack().popLHS();
    return true;
}
Also used : Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Aggregations

Variable (org.codehaus.groovy.ast.Variable)43 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)23 ClassNode (org.codehaus.groovy.ast.ClassNode)21 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)15 FieldNode (org.codehaus.groovy.ast.FieldNode)14 MethodNode (org.codehaus.groovy.ast.MethodNode)13 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)13 Parameter (org.codehaus.groovy.ast.Parameter)12 Expression (org.codehaus.groovy.ast.expr.Expression)9 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)7 LinkedList (java.util.LinkedList)6 ASTNode (org.codehaus.groovy.ast.ASTNode)6 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)6 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 VariableScope (org.codehaus.groovy.ast.VariableScope)5 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)5 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)5 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)4