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