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; } break; } scope = scope.getParent(); } VariableScope end = scope; scope = currentScope; while (scope != end) { if (end.isClassScope() || (end.isReferencedClassVariable(name) && end.getDeclaredVariable(name) == null)) { scope.putReferencedClassVariable(var); } else { // var.setClosureSharedVariable(var.isClosureSharedVariable() || inClosure); scope.putReferencedLocalVariable(var); } scope = scope.getParent(); } return var; }
private void checkVariableContextAccess(Variable v, Expression expr) { if (inPropertyExpression || v.isInStaticContext() || !currentScope.isInStaticContext()) return; String msg = v.getName() + " is declared in a dynamic context, but you tried to" + " access it from a static context."; addError(msg, expr); // declare a static variable to be able to continue the check DynamicVariable v2 = new DynamicVariable(v.getName(), currentScope.isInStaticContext()); currentScope.putDeclaredVariable(v2); }