Example usage for org.eclipse.jdt.internal.core.search.matching MemberDeclarationVisitor getOtherElements

List of usage examples for org.eclipse.jdt.internal.core.search.matching MemberDeclarationVisitor getOtherElements

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.matching MemberDeclarationVisitor getOtherElements.

Prototype

IJavaElement[] getOtherElements(int idx) 

Source Link

Usage

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Visit the given method declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 * Note that the method declaration has already been checked.
 *///ww w  .  j  av a 2s  .  co m
protected void reportMatching(AbstractMethodDeclaration method, TypeDeclaration type, IJavaElement parent,
        int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet) throws CoreException {
    IJavaElement enclosingElement = null;

    // report method declaration itself
    if (accuracy > -1) {
        enclosingElement = createHandle(method, parent);
        if (enclosingElement != null) { // skip if unable to find method
            // compute source positions of the selector
            Scanner scanner = this.parser.scanner;
            int nameSourceStart = method.sourceStart;
            scanner.setSource(this.currentPossibleMatch.getContents());
            scanner.resetTo(nameSourceStart, method.sourceEnd);
            try {
                scanner.getNextToken();
            } catch (InvalidInputException e) {
                // ignore
            }
            if (encloses(enclosingElement)) {
                SearchMatch match = null;
                if (method.isDefaultConstructor()) {
                    // Use type for match associated element as default constructor does not exist in source
                    int offset = type.sourceStart;
                    match = this.patternLocator.newDeclarationMatch(type, parent, type.binding, accuracy,
                            type.sourceEnd - offset + 1, this);
                } else {
                    int length = scanner.currentPosition - nameSourceStart;
                    match = this.patternLocator.newDeclarationMatch(method, enclosingElement, method.binding,
                            accuracy, length, this);
                }
                if (match != null) {
                    report(match);
                }
            }
        }
    }

    // handle nodes for the local type first
    if ((method.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        // Traverse method declaration to report matches both in local types declaration
        // and in local variables declaration
        ASTNode[] nodes = typeInHierarchy
                ? nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd)
                : null;
        boolean report = (this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this);
        try {
            method.traverse(declarationVisitor, (ClassScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                if (report && level != null) {
                    this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            method.binding, level.intValue(), this);
                }
            }
        }
    }

    // report the type parameters
    TypeParameter[] typeParameters = method.typeParameters();
    if (typeParameters != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(typeParameters, enclosingElement, parent, method.binding, nodeSet);
        }
    }

    // report annotations
    if (method.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(method, parent);
        }
        if (enclosingElement != null) {
            reportMatching(method.annotations, enclosingElement, null, method.binding, nodeSet, true, true);
        }
    }

    // references in this method
    if (typeInHierarchy) {
        ASTNode[] nodes = nodeSet.matchingNodes(method.declarationSourceStart, method.declarationSourceEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.METHOD_CONTAINER) != 0) {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(method, parent);
                }
                if (encloses(enclosingElement)) {
                    if (this.pattern.mustResolve) {
                        // Visit only if the pattern must resolve
                        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(
                                enclosingElement, nodes, nodeSet, this);
                        method.traverse(declarationVisitor, (ClassScope) null);
                        int length = nodes.length;
                        for (int i = 0; i < length; i++) {
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(nodes[i]);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(nodes[i], enclosingElement,
                                        declarationVisitor.getLocalElement(i),
                                        declarationVisitor.getOtherElements(i), method.binding,
                                        level.intValue(), this);
                            }
                        }
                    } else {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (level != null) { // ensure that the reference has not been already reported while visiting
                                this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                        method.binding, level.intValue(), this);
                            }
                        }
                    }
                    return;
                }
            }
            // Remove all remaining nodes
            for (int i = 0, l = nodes.length; i < l; i++) {
                nodeSet.matchingNodes.removeKey(nodes[i]);
            }
        }
    }
}

From source file:org.eclipse.jdt.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Visit the given field declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 *//*  w w w . j  a v  a  2s .  c  o  m*/
protected void reportMatching(FieldDeclaration field, FieldDeclaration[] otherFields, TypeDeclaration type,
        IJavaElement parent, int accuracy, boolean typeInHierarchy, MatchingNodeSet nodeSet)
        throws CoreException {
    IJavaElement enclosingElement = null;
    if (accuracy > -1) {
        enclosingElement = createHandle(field, type, parent);
        if (encloses(enclosingElement)) {
            int offset = field.sourceStart;
            SearchMatch match = newDeclarationMatch(enclosingElement, field.binding, accuracy, offset,
                    field.sourceEnd - offset + 1);
            if (field.initialization instanceof AllocationExpression) {
                reportAccurateEnumConstructorReference(match, field,
                        (AllocationExpression) field.initialization);
            } else {
                report(match);
            }
        }
    }

    // handle the nodes for the local type first
    if ((field.bits & ASTNode.HasLocalType) != 0) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(field, type, parent);
        }
        // Traverse field declaration(s) to report matches both in local types declaration
        // and in local variables declaration
        int fieldEnd = field.endPart2Position == 0 ? field.declarationSourceEnd : field.endPart2Position;
        ASTNode[] nodes = typeInHierarchy ? nodeSet.matchingNodes(field.sourceStart, fieldEnd) : null;
        boolean report = (this.matchContainer & PatternLocator.FIELD_CONTAINER) != 0
                && encloses(enclosingElement);
        MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                report ? nodes : null, nodeSet, this);
        try {
            field.traverse(declarationVisitor, (MethodScope) null);
        } catch (WrappedCoreException e) {
            throw e.coreException;
        }
        // Report all nodes and remove them
        if (nodes != null) {
            int length = nodes.length;
            for (int i = 0; i < length; i++) {
                ASTNode node = nodes[i];
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                if (report && level != null) {
                    if (node instanceof TypeDeclaration) {
                        // use field declaration to report match (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88174)
                        AllocationExpression allocation = ((TypeDeclaration) node).allocation;
                        if (allocation != null && allocation.enumConstant != null) {
                            node = field;
                        }
                    }
                    this.patternLocator.matchReportReference(node, enclosingElement,
                            declarationVisitor.getLocalElement(i), declarationVisitor.getOtherElements(i),
                            field.binding, level.intValue(), this);
                }
            }
        }
    }

    // report annotations
    IJavaElement[] otherElements = null;
    if (field.annotations != null) {
        if (enclosingElement == null) {
            enclosingElement = createHandle(field, type, parent);
        }
        if (otherFields != null) {
            otherElements = createHandles(otherFields, type, parent);
        }
        reportMatching(field.annotations, enclosingElement, otherElements, field.binding, nodeSet, true, true);
    }

    if (typeInHierarchy) {
        // Look at field declaration
        if (field.endPart1Position != 0) { // not necessary if field is an initializer
            ASTNode[] nodes = nodeSet.matchingNodes(field.declarationSourceStart, field.endPart1Position);
            if (nodes != null) {
                if ((this.matchContainer & PatternLocator.FIELD_CONTAINER) == 0) {
                    for (int i = 0, l = nodes.length; i < l; i++)
                        nodeSet.matchingNodes.removeKey(nodes[i]);
                } else {
                    if (enclosingElement == null)
                        enclosingElement = createHandle(field, type, parent);
                    if (encloses(enclosingElement)) {
                        for (int i = 0, l = nodes.length; i < l; i++) {
                            ASTNode node = nodes[i];
                            Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                            if (otherFields != null && otherElements == null) {
                                otherElements = createHandles(otherFields, type, parent);
                            }
                            this.patternLocator.matchReportReference(node, enclosingElement, null,
                                    otherElements, field.binding, level.intValue(), this);
                        }
                    }
                }
            }
        }

        // Look in initializer
        int fieldEnd = field.endPart2Position == 0 ? field.declarationSourceEnd : field.endPart2Position;
        ASTNode[] nodes = nodeSet.matchingNodes(field.sourceStart, fieldEnd);
        if (nodes != null) {
            if ((this.matchContainer & PatternLocator.FIELD_CONTAINER) == 0) {
                for (int i = 0, l = nodes.length; i < l; i++) {
                    nodeSet.matchingNodes.removeKey(nodes[i]);
                }
            } else {
                if (enclosingElement == null) {
                    enclosingElement = createHandle(field, type, parent);
                }
                if (encloses(enclosingElement)) {
                    MemberDeclarationVisitor declarationVisitor = new MemberDeclarationVisitor(enclosingElement,
                            nodes, nodeSet, this);
                    field.traverse(declarationVisitor, (MethodScope) null);
                    int length = nodes.length;
                    for (int i = 0; i < length; i++) {
                        ASTNode node = nodes[i];
                        Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                        if (level != null) { // ensure that the reference has not been already reported while visiting
                            if (node instanceof TypeDeclaration) {
                                // use field declaration to report match (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=88174)
                                AllocationExpression allocation = ((TypeDeclaration) node).allocation;
                                if (allocation != null && allocation.enumConstant != null) {
                                    node = field;
                                }
                            }
                            this.patternLocator.matchReportReference(node, enclosingElement,
                                    declarationVisitor.getLocalElement(i),
                                    declarationVisitor.getOtherElements(i), field.binding, level.intValue(),
                                    this);
                        }
                    }
                    return;
                }
            }
        }
    }
}