private void resolveScope(final ClassNode cNode) { // we need to resolve again! VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(unit); scopeVisitor.visitClass(cNode); }
private void checkForFinal(final Expression expression, VariableExpression ve) { Variable v = ve.getAccessedVariable(); if (v != null) { boolean isFinal = isFinal(v.getModifiers()); boolean isParameter = v instanceof Parameter; if (isFinal && isParameter) { addError("Cannot assign a value to final variable '" + v.getName() + "'", expression); } } }
public void visitPropertyExpression(PropertyExpression expression) { expression.getObjectExpression().visit(this); expression.getProperty().visit(this); checkPropertyOnExplicitThis(expression); }
public void visitFieldExpression(FieldExpression expression) { String name = expression.getFieldName(); //TODO: change that to get the correct scope Variable v = checkVariableNameForDeclaration(name, expression); checkVariableContextAccess(v, expression); }
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) { pushState(node.isStatic()); inConstructor = isConstructor; node.setVariableScope(currentScope); visitAnnotations(node); // GROOVY-2156 Parameter[] parameters = node.getParameters(); for (Parameter parameter : parameters) { visitAnnotations(parameter); } declare(node.getParameters(), node); visitClassCodeContainer(node.getCode()); popState(); }
public void visitClass(ClassNode node) { // AIC are already done, doing them here again will lead to wrong scopes if (node instanceof InnerClassNode) { InnerClassNode in = (InnerClassNode) node; if (in.isAnonymous() && !in.isEnum()) return; } pushState(); prepareVisit(node); super.visitClass(node); if (recurseInnerClasses) { Iterator<InnerClassNode> innerClasses = node.getInnerClasses(); while (innerClasses.hasNext()) { visitClass(innerClasses.next()); } } popState(); }
private Variable findClassMember(ClassNode cn, String name) { if (cn == null) return null; if (cn.isScript()) { return new DynamicVariable(name, false); } List l = cn.getFields(); for (Iterator iter = l.iterator(); iter.hasNext();) { FieldNode f = (FieldNode) iter.next(); if (f.getName().equals(name)) return f; } l = cn.getMethods(); for (Iterator iter = l.iterator(); iter.hasNext();) { MethodNode f = (MethodNode) iter.next(); String methodName = f.getName(); String pName = getPropertyName(f); if (pName == null) continue; if (!pName.equals(name)) continue; PropertyNode var = new PropertyNode(pName, f.getModifiers(), getPropertyType(f), cn, null, null, null); return var; } l = cn.getProperties(); for (Iterator iter = l.iterator(); iter.hasNext();) { PropertyNode f = (PropertyNode) iter.next(); if (f.getName().equals(name)) return f; } Variable ret = findClassMember(cn.getSuperClass(), name); if (ret != null) return ret; return findClassMember(cn.getOuterClass(), name); }
Variable member = findClassMember(classScope, var.getName()); if (member != null) { boolean cc = currentScope.isInStaticContext() || isSpecialConstructorCall; declare(var, expression); addError("The variable " + var.getName() + " is undefined in the current scope", expression); } else if (scope.isRoot() && dynamicScope != null) {
/** * a property on "this", like this.x is transformed to a * direct field access, so we need to check the * static context here * * @param pe the property expression to check */ private void checkPropertyOnExplicitThis(PropertyExpression pe) { if (!currentScope.isInStaticContext()) return; Expression object = pe.getObjectExpression(); if (!(object instanceof VariableExpression)) return; VariableExpression ve = (VariableExpression) object; if (!ve.getName().equals("this")) return; String name = pe.getPropertyAsString(); if (name == null || name.equals("class")) return; Variable member = findClassMember(currentClass, name); if (member == null) return; checkVariableContextAccess(member, pe); }
public void visitClosureExpression(ClosureExpression expression) { pushState(); expression.setVariableScope(currentScope); if (expression.isParameterSpecified()) { Parameter[] parameters = expression.getParameters(); for (Parameter parameter : parameters) { parameter.setInStaticContext(currentScope.isInStaticContext()); if (parameter.hasInitialExpression()) { parameter.getInitialExpression().visit(this); } declare(parameter, expression); } } else if (expression.getParameters() != null) { Parameter var = new Parameter(ClassHelper.OBJECT_TYPE, "it"); var.setInStaticContext(currentScope.isInStaticContext()); currentScope.putDeclaredVariable(var); } super.visitClosureExpression(expression); markClosureSharedVariables(); popState(); }
if (!call.isUsingAnonymousInnerClass()) return; pushState(); InnerClassNode innerClass = (InnerClassNode) call.getType(); innerClass.setVariableScope(currentScope); if (parameters.length == 0) parameters = null; // null means no implicit "it" ClosureExpression cl = new ClosureExpression(parameters, method.getCode()); visitClosureExpression(cl); pushState(field.isStatic()); if (expression != null) { if (expression instanceof VariableExpression) { if (vexp.getAccessedVariable() instanceof Parameter) { popState(); continue; popState(); statement.visit(this); markClosureSharedVariables(); popState();
private void declare(VariableExpression vex) { vex.setInStaticContext(currentScope.isInStaticContext()); declare(vex, vex); vex.setAccessedVariable(vex); }
public void visitDeclarationExpression(DeclarationExpression expression) { visitAnnotations(expression); // visit right side first to avoid the usage of a // variable before its declaration expression.getRightExpression().visit(this); if (expression.isMultipleAssignmentDeclaration()) { TupleExpression list = expression.getTupleExpression(); for (Expression e : list.getExpressions()) { declare((VariableExpression) e); } } else { declare(expression.getVariableExpression()); } }
private Variable findClassMember(ClassNode cn, String name) { if (cn == null) return null; if (cn.isScript()) { return new DynamicVariable(name, false); } for (FieldNode fn : cn.getFields()) { if (fn.getName().equals(name)) return fn; } for (MethodNode mn : cn.getMethods()) { String pName = getPropertyName(mn); if (name.equals(pName)) { PropertyNode property = new PropertyNode(name, mn.getModifiers(), ClassHelper.OBJECT_TYPE, cn, null, null, null); property.getField().setHasNoRealSourcePosition(true); property.getField().setSynthetic(true); property.getField().setDeclaringClass(cn); property.setDeclaringClass(cn); return property; } } for (PropertyNode pn : cn.getProperties()) { if (pn.getName().equals(name)) return pn; } Variable ret = findClassMember(cn.getSuperClass(), name); if (ret != null) return ret; return findClassMember(cn.getOuterClass(), name); }
private void pushState() { pushState(currentScope.isInStaticContext()); }
public void visitVariableExpression(VariableExpression expression) { String name = expression.getName(); Variable v = checkVariableNameForDeclaration(name, expression); if (v == null) return; expression.setAccessedVariable(v); checkVariableContextAccess(v, expression); }