private Variable checkVariableNameForDeclaration(String name, Expression expression) {
if ("super".equals(name) || "this".equals(name)) return null;
VariableScope scope = currentScope;
Variable var = new DynamicVariable(name, currentScope.isInStaticContext());
// try to find a declaration of a variable
while (true) {
Variable var1;
var1 = scope.getDeclaredVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedLocalVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
var1 = scope.getReferencedClassVariable(var.getName());
if (var1 != null) {
var = var1;
break;
}
ClassNode classScope = scope.getClassScope();
if (classScope != null) {
Variable member = findClassMember(classScope, var.getName());
if (member != null) {
boolean staticScope = currentScope.isInStaticContext() || isSpecialConstructorCall;
boolean staticMember = member.isInStaticContext();
// We don't allow a static context (e.g. a static method) to access
// a non-static variable (e.g. a non-static field).
if (! (staticScope && ! staticMember))
var = member;
}