Java Code Examples for org.codehaus.groovy.ast.expr.VariableExpression#getAccessedVariable()

The following examples show how to use org.codehaus.groovy.ast.expr.VariableExpression#getAccessedVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean doAssignmentToLocalVariable(final String method, final 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);
    controller.getOperandStack().dup();
    controller.getCompileStack().pushLHS(true);
    binExp.getLeftExpression().visit(controller.getAcg());
    controller.getCompileStack().popLHS();

    return true;
}
 
Example 2
Source File: VariableFinderVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitDeclarationExpression(DeclarationExpression expression) {
    // if we are in the same block we check position, if it occurs after
    // current position we ignore it
    if (blocks.isEmpty()
            && expression.getLineNumber() >= 0 && expression.getColumnNumber() >= 0
            && path.getLineNumber() >= 0 && path.getColumnNumber() >= 0
            && (expression.getLineNumber() > path.getLineNumber()
            || (expression.getLineNumber() == path.getLineNumber() && expression.getColumnNumber() >= path.getColumnNumber()))) {
        return;
    }

    if (!expression.isMultipleAssignmentDeclaration()) {
        VariableExpression variableExpression = expression.getVariableExpression();
        if (variableExpression.getAccessedVariable() != null) {
            String name = variableExpression.getAccessedVariable().getName();
            variables.put(name, variableExpression.getAccessedVariable());
        }
    }
    // perhaps we could visit just declaration or do nothing
    super.visitDeclarationExpression(expression);
}
 
Example 3
Source File: VariableFinderVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitBinaryExpression(BinaryExpression expression) {
    // if we are in the same block we check position, if it occurs after
    // current position we ignore it
    if (blocks.isEmpty()
            && expression.getLineNumber() >= 0 && expression.getColumnNumber() >= 0
            && path.getLineNumber() >= 0 && path.getColumnNumber() >= 0
            && (expression.getLineNumber() > path.getLineNumber()
            || (expression.getLineNumber() == path.getLineNumber() && expression.getColumnNumber() >= path.getColumnNumber()))) {
        return;
    }

    Expression leftExpression = expression.getLeftExpression();
    if (leftExpression instanceof VariableExpression) {
        if (expression.getOperation().isA(Types.EQUAL)) {
            VariableExpression variableExpression = (VariableExpression) leftExpression;
            if (variableExpression.getAccessedVariable() != null) {
                String name = variableExpression.getAccessedVariable().getName();
                if (!variables.containsKey(name)) {
                    variables.put(name, variableExpression.getAccessedVariable());
                }
            }
        }
    }
    super.visitBinaryExpression(expression);
}
 
Example 4
Source File: FindMethodUsagesVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethodCallExpression(MethodCallExpression methodCall) {
    Expression expression = methodCall.getObjectExpression();

    if (expression instanceof VariableExpression) {
        VariableExpression variableExpression = ((VariableExpression) expression);
        Variable variable = variableExpression.getAccessedVariable();

        if (variable != null) {
            if (variable.isDynamicTyped()) {
                addDynamicVarUsages(methodCall, variable);
            } else {
                addStaticVarUsages(methodCall, variable);
            }
        } else {
            addThisUsages(methodCall);
        }

    } else if (expression instanceof ConstructorCallExpression) {
        addConstructorUsages(methodCall, (ConstructorCallExpression) expression);
    }
    super.visitMethodCallExpression(methodCall);
}
 
Example 5
Source File: StaticVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression ve) {
    if (ve.getAccessedVariable() instanceof DynamicVariable && (ve.isInStaticContext() || inSpecialConstructorCall) && !inClosure) {
        // GROOVY-5687: interface constants not visible to implementing sub-class in static context
        if (methodNode != null && methodNode.isStatic()) {
            FieldNode fieldNode = getDeclaredOrInheritedField(methodNode.getDeclaringClass(), ve.getName());
            if (fieldNode != null && fieldNode.isStatic()) {
                return;
            }
        }
        addError("Apparent variable '" + ve.getName() + "' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:\n" +
                "You attempted to reference a variable in the binding or an instance variable from a static context.\n" +
                "You misspelled a classname or statically imported field. Please check the spelling.\n" +
                "You attempted to use a method '" + ve.getName() + "' but left out brackets in a place not allowed by the grammar.", ve);
    }
}
 
Example 6
Source File: ProcessVariableRenamer.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    final Variable accessedVar = expression.getAccessedVariable();
    // look for dynamic variables since the parameters already have the
    // new names, the actual references to the parameters are using the
    // old names
    if (accessedVar instanceof DynamicVariable) {
        final String newName = findReplacement(accessedVar.getName());
        if (newName != null) {
            ReplaceEdit replaceEdit = new ReplaceEdit(expression.getStart(), expression.getLength(), newName);
            try {
                edits.addChild(replaceEdit);
            }catch (MalformedTreeException e) {
                BonitaStudioLog.error(e);
            }
        }
    }
}
 
Example 7
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    super.visitVariableExpression(expression);
    Map<Variable, VariableState> state = getState();
    Variable key = expression.getAccessedVariable();
    if (key == null) {
        fixVar(expression);
        key = expression.getAccessedVariable();
    }
    if (key != null && !key.isClosureSharedVariable() && callback != null) {
        VariableState variableState = state.get(key);
        if ((inAssignmentRHS || inArgumentList) && (variableState == VariableState.is_uninitialized || variableState == VariableState.is_ambiguous)) {
            callback.variableNotAlwaysInitialized(expression);
        }
    }
}
 
Example 8
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression expression) {
        if (expression.isSuperExpression()) {
            guessedType = expression.getType().getSuperClass();
        }
        if (null != expression.getAccessedVariable()) {
            Variable accessedVariable = expression.getAccessedVariable();

            if (accessedVariable.hasInitialExpression()) {
                Expression initialExpression = expression.getAccessedVariable().getInitialExpression();
                if (initialExpression instanceof ConstantExpression
                        && !initialExpression.getText().equals("null")) { // NOI18N
                    guessedType = ((ConstantExpression) initialExpression).getType();
                } else if (initialExpression instanceof ConstructorCallExpression) {
                    guessedType = ClassHelper.make(((ConstructorCallExpression) initialExpression).getType().getName());
                } else if (initialExpression instanceof MethodCallExpression) {
                    int newOffset = ASTUtils.getOffset(doc, initialExpression.getLineNumber(), initialExpression.getColumnNumber());
                    AstPath newPath = new AstPath(path.root(), newOffset, doc);
                    guessedType = MethodInference.findCallerType(initialExpression, newPath, doc, newOffset);
                } else if (initialExpression instanceof ListExpression) {
                    guessedType = ((ListExpression) initialExpression).getType();
                } else if (initialExpression instanceof MapExpression) {
                    guessedType = ((MapExpression) initialExpression).getType();
                } else if (initialExpression instanceof RangeExpression) {
                    // this should work, but the type is Object - nut sure why
                    // guessedType = ((RangeExpression)initialExpression).getType();
                    guessedType = ClassHelper.makeWithoutCaching(Range.class, true);                
                }
            } else if (accessedVariable instanceof Parameter) {
                Parameter param = (Parameter) accessedVariable;
                guessedType = param.getType();
            }
        } else if (!expression.getType().getName().equals("java.lang.Object")) {
            guessedType = expression.getType();

        }
    super.visitVariableExpression(expression);
}
 
Example 9
Source File: SemanticAnalysisVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression node) {
    Variable var = node.getAccessedVariable();

    if (var instanceof FieldNode) {
        if (isInSource(node)) {
            OffsetRange range = ASTUtils.getRange(node, doc);
            highlights.put(range, ColoringAttributes.FIELD_SET);
        }
    }
    super.visitVariableExpression(node);
}
 
Example 10
Source File: FindVariableUsages.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression expression) {
    final VariableExpression variableExpression = expression;
    final Variable variable = variableExpression.getAccessedVariable();

    if (variable != null) {
        final String varName = variableExpression.getText();
        final String varType = variable.getType().getName();

        addIfEqual(expression, varType, varName);
    }
    super.visitVariableExpression(expression);
}
 
Example 11
Source File: StaticTypesTypeChooser.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * 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(final VariableExpression ve) {
    Variable variable = ve.getAccessedVariable();
    if (variable != ve && variable instanceof VariableExpression) {
        return getTarget((VariableExpression) variable);
    }
    return ve;
}
 
Example 12
Source File: ClosureWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    Variable v = expression.getAccessedVariable();
    if (v == null) return;
    if (!(v instanceof FieldNode)) return;
    String name = expression.getName();
    FieldNode fn = icn.getDeclaredField(name);
    if (fn != null) { // only overwrite if we find something more specific
        expression.setAccessedVariable(fn);
    }
}
 
Example 13
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Given a variable expression, returns the ultimately accessed variable.
 *
 * @param ve a variable expression
 * @return the target variable
 */
protected static Variable findTargetVariable(final VariableExpression ve) {
    Variable accessedVariable = ve.getAccessedVariable();
    if (accessedVariable != null && accessedVariable != ve) {
        if (accessedVariable instanceof VariableExpression) {
            return findTargetVariable((VariableExpression) accessedVariable);
        }
        return accessedVariable;
    }
    return ve;
}
 
Example 14
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ClassNode isTraitSelf(final 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;
}
 
Example 15
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkFinalFieldAccess(Expression expression) {
    if (!(expression instanceof VariableExpression) && !(expression instanceof PropertyExpression)) return;
    Variable v = null;
    if (expression instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) expression;
        v = ve.getAccessedVariable();
    } else {
        PropertyExpression propExp = ((PropertyExpression) expression);
        Expression objectExpression = propExp.getObjectExpression();
        if (objectExpression instanceof VariableExpression) {
            VariableExpression varExp = (VariableExpression) objectExpression;
            if (varExp.isThisExpression()) {
                v = currentClass.getDeclaredField(propExp.getPropertyAsString());
            }
        }
    }
    if (v instanceof FieldNode) {
        FieldNode fn = (FieldNode) v;

        /*
         *  if it is static final but not accessed inside a static constructor, or,
         *  if it is an instance final but not accessed inside a instance constructor, it is an error
         */
        boolean isFinal = fn.isFinal();
        boolean isStatic = fn.isStatic();
        boolean error = isFinal && ((isStatic && !inStaticConstructor) || (!isStatic && !inConstructor));

        if (error) addError("cannot modify" + (isStatic ? " static" : "") + " final field '" + fn.getName() +
                "' outside of " + (isStatic ? "static initialization block." : "constructor."), expression);
    }
}
 
Example 16
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected Expression transformVariableExpression(final VariableExpression ve) {
    visitAnnotations(ve);
    Variable v = ve.getAccessedVariable();

    if(!(v instanceof DynamicVariable) && !checkingVariableTypeInDeclaration) {
        /*
         *  GROOVY-4009: when a normal variable is simply being used, there is no need to try to
         *  resolve its type. Variable type resolve should proceed only if the variable is being declared.
         */
        return ve;
    }
    if (v instanceof DynamicVariable) {
        String name = ve.getName();
        ClassNode t = ClassHelper.make(name);
        // asking isResolved here allows to check if a primitive
        // type name like "int" was used to make t. In such a case
        // we have nothing left to do.
        boolean isClass = t.isResolved();
        if (!isClass) {
            // It was no primitive type, so next we see if the name,
            // which is a vanilla name, starts with a lower case letter.
            // In that case we change it to a LowerCaseClass to let the
            // compiler skip the resolving at several places in this class.
            if (Character.isLowerCase(name.charAt(0))) {
                t = new LowerCaseClass(name);
            }
            isClass = resolve(t);
        }
        if (isClass) {
            // the name is a type so remove it from the scoping
            // as it is only a classvariable, it is only in
            // referencedClassVariables, but must be removed
            // for each parentscope too
            for (VariableScope scope = currentScope; scope != null && !scope.isRoot(); scope = scope.getParent()) {
                if (scope.removeReferencedClassVariable(ve.getName()) == null) break;
            }
            return new ClassExpression(t);
        }
    }
    resolveOrFail(ve.getType(), ve);
    ClassNode origin = ve.getOriginType();
    if (origin != ve.getType()) resolveOrFail(origin, ve);
    return ve;
}
 
Example 17
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 4 votes vote down vote up
public boolean isDynamic(VariableExpression var) {
    return var.getAccessedVariable() instanceof DynamicVariable;
}
 
Example 18
Source File: GroovyASTUtils.java    From groovy-language-server with Apache License 2.0 4 votes vote down vote up
public static ASTNode getDefinition(ASTNode node, boolean strict, ASTNodeVisitor astVisitor) {
    if (node == null) {
        return null;
    }
    ASTNode parentNode = astVisitor.getParent(node);
    if (node instanceof ExpressionStatement) {
        ExpressionStatement statement = (ExpressionStatement) node;
        node = statement.getExpression();
    }
    if (node instanceof ClassNode) {
        return tryToResolveOriginalClassNode((ClassNode) node, strict, astVisitor);
    } else if (node instanceof ConstructorCallExpression) {
        ConstructorCallExpression callExpression = (ConstructorCallExpression) node;
        return GroovyASTUtils.getMethodFromCallExpression(callExpression, astVisitor);
    } else if (node instanceof DeclarationExpression) {
        DeclarationExpression declExpression = (DeclarationExpression) node;
        if (!declExpression.isMultipleAssignmentDeclaration()) {
            ClassNode originType = declExpression.getVariableExpression().getOriginType();
            return tryToResolveOriginalClassNode(originType, strict, astVisitor);
        }
    } else if (node instanceof ClassExpression) {
        ClassExpression classExpression = (ClassExpression) node;
        return tryToResolveOriginalClassNode(classExpression.getType(), strict, astVisitor);
    } else if (node instanceof ImportNode) {
        ImportNode importNode = (ImportNode) node;
        return tryToResolveOriginalClassNode(importNode.getType(), strict, astVisitor);
    } else if (node instanceof MethodNode) {
        return node;
    } else if (node instanceof ConstantExpression && parentNode != null) {
        if (parentNode instanceof MethodCallExpression) {
            MethodCallExpression methodCallExpression = (MethodCallExpression) parentNode;
            return GroovyASTUtils.getMethodFromCallExpression(methodCallExpression, astVisitor);
        } else if (parentNode instanceof PropertyExpression) {
            PropertyExpression propertyExpression = (PropertyExpression) parentNode;
            PropertyNode propNode = GroovyASTUtils.getPropertyFromExpression(propertyExpression, astVisitor);
            if (propNode != null) {
                return propNode;
            }
            return GroovyASTUtils.getFieldFromExpression(propertyExpression, astVisitor);
        }
    } else if (node instanceof VariableExpression) {
        VariableExpression variableExpression = (VariableExpression) node;
        Variable accessedVariable = variableExpression.getAccessedVariable();
        if (accessedVariable instanceof ASTNode) {
            return (ASTNode) accessedVariable;
        }
        // DynamicVariable is not an ASTNode, so skip it
        return null;
    } else if (node instanceof Variable) {
        return node;
    }
    return null;
}
 
Example 19
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp instanceof MethodCallExpression) {
        return transformMethodCall((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        ClosureExpression cl = (ClosureExpression) exp;
        cl.getCode().visit(this);
        return cl;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) exp;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            MethodCallExpression callGetModel = new MethodCallExpression(
                    new VariableExpression("this"),
                    "getModel",
                    ArgumentListExpression.EMPTY_ARGUMENTS
            );
            callGetModel.setImplicitThis(true);
            callGetModel.setSourcePosition(exp);
            String varName = var.getName();
            if ("model".equals(varName) || "unescaped".equals(varName)) {
                return callGetModel;
            }
            MethodCallExpression mce = new MethodCallExpression(
                    callGetModel,
                    "get",
                    new ArgumentListExpression(new ConstantExpression(varName))
            );
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            MethodCallExpression yield = new MethodCallExpression(
                    new VariableExpression("this"),
                    "tryEscape",
                    new ArgumentListExpression(mce)
            );
            yield.setImplicitThis(true);
            yield.setSourcePosition(exp);
            yield.putNodeMetaData(TARGET_VARIABLE, varName);
            return autoEscape?yield:mce;
        }
    }
    return super.transform(exp);
}
 
Example 20
Source File: FindTypeUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static OffsetRange getVariableRange(VariableExpression variable, BaseDocument doc, int cursorOffset) {
    if (variable == null || variable.getAccessedVariable() == null || variable.isDynamicTyped()) {
        return OffsetRange.NONE;
    }
    return getRange(variable.getAccessedVariable().getOriginType(), doc, cursorOffset);
}