Search in sources :

Example 81 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project AutoRefactor by JnRouvignac.

the class ASTHelper method isSameVariable.

/**
 * Returns whether the two provided nodes represent the same variable.
 *
 * @param node1 the first node to compare
 * @param node2 the second node to compare
 * @return true if the two provided nodes represent the same variable, false otherwise
 */
public static boolean isSameVariable(ASTNode node1, ASTNode node2) {
    node1 = removeParentheses(node1);
    node2 = removeParentheses(node2);
    if (node1 == null || node2 == null) {
        return false;
    }
    switch(node1.getNodeType()) {
        case THIS_EXPRESSION:
            return node2.getNodeType() == THIS_EXPRESSION;
        case SIMPLE_NAME:
            final SimpleName sn = (SimpleName) node1;
            switch(node2.getNodeType()) {
                case QUALIFIED_NAME:
                    return isSameVariable(sn, (QualifiedName) node2);
                case FIELD_ACCESS:
                    return isSameVariable(sn, (FieldAccess) node2);
            }
            break;
        case QUALIFIED_NAME:
            final QualifiedName qn = (QualifiedName) node1;
            switch(node2.getNodeType()) {
                case SIMPLE_NAME:
                    return isSameVariable((SimpleName) node2, qn);
                case QUALIFIED_NAME:
                    return isSameVariable(qn, (QualifiedName) node2);
                case FIELD_ACCESS:
                    return isSameVariable(qn, (FieldAccess) node2);
            }
            break;
        case FIELD_ACCESS:
            final FieldAccess fa = (FieldAccess) node1;
            switch(node2.getNodeType()) {
                case SIMPLE_NAME:
                    return isSameVariable((SimpleName) node2, fa);
                case QUALIFIED_NAME:
                    return isSameVariable((QualifiedName) node2, fa);
                case FIELD_ACCESS:
                    return isSameVariable(fa, (FieldAccess) node2);
            }
    }
    return areVariableBindingsEqual(node1, node2);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 82 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class CodeScopeBuilder method visit.

@Override
public boolean visit(MethodInvocation node) {
    Expression receiver = node.getExpression();
    if (receiver == null) {
        SimpleName name = node.getName();
        if (fIgnoreBinding == null || !Bindings.equals(fIgnoreBinding, name.resolveBinding())) {
            node.getName().accept(this);
        }
    } else {
        receiver.accept(this);
    }
    accept(node.arguments());
    return false;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 83 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class JdtASTMatcher method match.

@Override
public boolean match(SimpleName node, Object other) {
    boolean isomorphic = super.match(node, other);
    if (!isomorphic || !(other instanceof SimpleName)) {
        return false;
    }
    SimpleName name = (SimpleName) other;
    IBinding nodeBinding = node.resolveBinding();
    IBinding otherBinding = name.resolveBinding();
    if (nodeBinding == null) {
        if (otherBinding != null) {
            return false;
        }
    } else {
        if (nodeBinding != otherBinding) {
            return false;
        }
    }
    if (node.resolveTypeBinding() != name.resolveTypeBinding()) {
        return false;
    }
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding)

Example 84 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class LinkedNodeFinder method findByProblems.

public static SimpleName[] findByProblems(ASTNode parent, SimpleName nameNode) {
    ArrayList<SimpleName> res = new ArrayList<>();
    ASTNode astRoot = parent.getRoot();
    if (!(astRoot instanceof CompilationUnit)) {
        return null;
    }
    IProblem[] problems = ((CompilationUnit) astRoot).getProblems();
    int nameNodeKind = getNameNodeProblemKind(problems, nameNode);
    if (nameNodeKind == 0) {
        // no problem on node
        return null;
    }
    int bodyStart = parent.getStartPosition();
    int bodyEnd = bodyStart + parent.getLength();
    String name = nameNode.getIdentifier();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        int probStart = curr.getSourceStart();
        int probEnd = curr.getSourceEnd() + 1;
        if (probStart > bodyStart && probEnd < bodyEnd) {
            int currKind = getProblemKind(curr);
            if ((nameNodeKind & currKind) != 0) {
                ASTNode node = NodeFinder.perform(parent, probStart, (probEnd - probStart));
                if (node instanceof SimpleName && name.equals(((SimpleName) node).getIdentifier())) {
                    res.add((SimpleName) node);
                }
            }
        }
    }
    return res.toArray(new SimpleName[res.size()]);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 85 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project eclipse.jdt.ls by eclipse.

the class RenameNodeCorrectionProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    // build a full AST
    CompilationUnit unit = CoreASTProvider.getInstance().getAST(getCompilationUnit(), CoreASTProvider.WAIT_YES, null);
    ASTNode name = NodeFinder.perform(unit, fOffset, fLength);
    if (name instanceof SimpleName) {
        SimpleName[] names = LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
        if (names != null) {
            for (int i = 0; i < names.length; i++) {
                SimpleName curr = names[i];
                root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
            }
            return;
        }
    }
    root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32