public boolean isDynamic(VariableExpression var) { return var.getAccessedVariable() instanceof DynamicVariable; }
/** * The inferred type, in case of a variable expression, can be set on the accessed variable, so we take it instead * of the facade one. * * @param ve the variable expression for which to return the target expression * @return the target variable expression */ private static VariableExpression getTarget(VariableExpression ve) { if (ve.getAccessedVariable() == null || ve.getAccessedVariable() == ve || (!(ve.getAccessedVariable() instanceof VariableExpression))) return ve; return getTarget((VariableExpression) ve.getAccessedVariable()); } }
private boolean matchesCandidate(Expression arg) { return arg instanceof VariableExpression && ((VariableExpression) arg).getAccessedVariable() == candidate.getVariableExpression().getAccessedVariable(); }
/** * Given a variable expression, returns the ultimately accessed variable. * * @param ve a variable expression * @return the target variable */ protected static Variable findTargetVariable(VariableExpression ve) { final Variable accessedVariable = ve.getAccessedVariable() != null ? ve.getAccessedVariable() : ve; if (accessedVariable != ve) { if (accessedVariable instanceof VariableExpression) return findTargetVariable((VariableExpression) accessedVariable); } return accessedVariable; }
private void cleanLocalVars(Map<Variable, VariableState> origState, Map<Variable, VariableState> state) { // clean local vars added during visit of closure for (Iterator<Map.Entry<Variable, VariableState>> iter = state.entrySet().iterator(); iter.hasNext(); ) { Map.Entry<Variable, VariableState> next = iter.next(); Variable key = next.getKey(); if (key instanceof VariableExpression && ((VariableExpression)key).getAccessedVariable() == key && !origState.containsKey(key)) { // remove local variable iter.remove(); } } }
@Override public void visitVariableExpression(VariableExpression ve) { Variable v = ve.getAccessedVariable(); if (v instanceof DynamicVariable) { if (!inPropertyExpression || inSpecialConstructorCall) addStaticVariableError(ve); } }
private static Variable getTarget(Variable v) { if (v instanceof VariableExpression) { Variable t = ((VariableExpression) v).getAccessedVariable(); if (t == v) return t; return getTarget(t); } return v; }
@Override public void visitVariableExpression(VariableExpression expr) { super.visitVariableExpression(expr); Variable var = expr.getAccessedVariable(); if (!(var instanceof FieldNode)) return; checkFieldAccess(expr, (FieldNode) var); }
@Override public void visitVariableExpression(VariableExpression ve) { if (exceptions.contains(ve.getName())) return; Variable av = ve.getAccessedVariable(); if (av instanceof DynamicVariable || !av.isInStaticContext()) { addVariableError(ve); } }
private void checkPrePostfixOperation(final Expression variable, final Expression originalExpression) { if (variable instanceof Variable) { recordAssignment((Variable) variable, false, false, true, originalExpression); if (variable instanceof VariableExpression) { Variable accessed = ((VariableExpression) variable).getAccessedVariable(); if (accessed != variable) { recordAssignment(accessed, false, false, true, originalExpression); } } } }
@Override public void variableNotAlwaysInitialized(final VariableExpression var) { if (Modifier.isFinal(var.getAccessedVariable().getModifiers())) throw new RuntimeParserException("The variable [" + var.getName() + "] may be uninitialized", var); } };
public static boolean isWildcardRef(Expression expr) { VariableExpression varExpr = ObjectUtil.asInstance(expr, VariableExpression.class); if (varExpr == null || !varExpr.getName().equals(Wildcard.INSTANCE.toString())) return false; Variable accessedVar = varExpr.getAccessedVariable(); if (!(accessedVar instanceof FieldNode)) return false; return ((FieldNode) accessedVar).getOwner().getName().equals(Specification.class.getName()); }
public static ClassNode isTraitSelf(VariableExpression vexp) { if (Traits.THIS_OBJECT.equals(vexp.getName())) { Variable accessedVariable = vexp.getAccessedVariable(); ClassNode type = accessedVariable != null ? accessedVariable.getType() : null; if (accessedVariable instanceof Parameter && Traits.isTrait(type)) { return type; } } return null; } }
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); } } }
@Override public void visitVariableExpression(VariableExpression expression) { Variable v = expression.getAccessedVariable(); if (v==null) return; if (!(v instanceof FieldNode)) return; String name = expression.getName(); FieldNode fn = closureClass.getDeclaredField(name); if (fn != null) { // only overwrite if we find something more specific expression.setAccessedVariable(fn); } } };
private boolean isInternalFieldAccess(final Expression exp) { if (exp instanceof VariableExpression) { Variable accessedVariable = ((VariableExpression) exp).getAccessedVariable(); if (accessedVariable instanceof FieldNode) { return knownFields.contains(accessedVariable.getName()); } } if (exp instanceof PropertyExpression) { if (((PropertyExpression) exp).isImplicitThis() || "this".equals(((PropertyExpression) exp).getObjectExpression().getText())) { return knownFields.contains(((PropertyExpression) exp).getProperty().getText()); } } return false; }
@Override public void variableNotFinal(Variable var, Expression bexp) { if (var instanceof VariableExpression) { var = ((VariableExpression) var).getAccessedVariable(); } if (var instanceof VariableExpression && isFinal(var.getModifiers())) { throw new RuntimeParserException("The variable [" + var.getName() + "] is declared final but is reassigned", bexp); } if (var instanceof Parameter && isFinal(var.getModifiers())) { throw new RuntimeParserException("The parameter [" + var.getName() + "] is declared final but is reassigned", bexp); } }
private Expression copyVarExpr(VariableExpression varExpr) { VariableExpression newVarExpr = new VariableExpression(varExpr.getName(), varExpr.getOriginType()); newVarExpr.setAccessedVariable(varExpr.getAccessedVariable()); newVarExpr.setSourcePosition(varExpr); return newVarExpr; } }
private void addStaticVariableError(VariableExpression ve) { // closures are always dynamic // propertyExpressions will handle the error a bit differently if (!inSpecialConstructorCall && (inClosure || !ve.isInStaticContext())) return; if (ve.isThisExpression() || ve.isSuperExpression()) return; Variable v = ve.getAccessedVariable(); if (currentMethod != null && currentMethod.isStatic()) { FieldNode fieldNode = getDeclaredOrInheritedField(currentMethod.getDeclaringClass(), ve.getName()); if (fieldNode != null && fieldNode.isStatic()) return; } if (v != null && !(v instanceof DynamicVariable) && v.isInStaticContext()) return; addVariableError(ve); }
private void verifyDataProcessorVariable(VariableExpression varExpr) { Variable accessedVar = varExpr.getAccessedVariable(); if (accessedVar instanceof VariableExpression) { // local variable resources.getErrorReporter().error(varExpr, "A variable named '%s' already exists in this scope", varExpr.getName()); return; } if (isDataProcessorVariable(varExpr.getName())) { resources.getErrorReporter().error(varExpr, "Duplicate declaration of data variable '%s'", varExpr.getName()); return; } if (whereBlock.getParent().getAst().getParameters().length > 0 && !(accessedVar instanceof Parameter)) { resources.getErrorReporter().error(varExpr, "Data variable '%s' needs to be declared as method parameter", varExpr.getName()); } }