Example usage for org.eclipse.jdt.internal.core.search.matching MatchingNodeSet matchingNodes

List of usage examples for org.eclipse.jdt.internal.core.search.matching MatchingNodeSet matchingNodes

Introduction

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

Prototype

SimpleLookupTable matchingNodes

To view the source code for org.eclipse.jdt.internal.core.search.matching MatchingNodeSet matchingNodes.

Click Source Link

Document

Map of matching ast nodes that don't need to be resolved to their accuracy level.

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.
 *//*from   w  ww .  java 2s.  c  o 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

/**
 * Report matching in annotations./*from  www  .j a  va  2 s.c  o  m*/
 * @param otherElements TODO
 */
protected void reportMatching(Annotation[] annotations, IJavaElement enclosingElement,
        IJavaElement[] otherElements, Binding elementBinding, MatchingNodeSet nodeSet, boolean matchedContainer,
        boolean enclosesElement) throws CoreException {
    for (int i = 0, al = annotations.length; i < al; i++) {
        Annotation annotationType = annotations[i];
        IJavaElement localAnnotation = null;
        IJavaElement[] otherAnnotations = null;
        int length = otherElements == null ? 0 : otherElements.length;
        boolean handlesCreated = false;

        // Look for annotation type ref
        TypeReference typeRef = annotationType.type;
        Integer level = (Integer) nodeSet.matchingNodes.removeKey(typeRef);
        if (level != null && enclosesElement && matchedContainer) {
            localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
            if (length > 0) {
                otherAnnotations = new IJavaElement[length];
                for (int o = 0; o < length; o++) {
                    otherAnnotations[o] = createHandle(annotationType, (IAnnotatable) otherElements[o]);
                }
            }
            handlesCreated = true;
            this.patternLocator.matchReportReference(typeRef, enclosingElement, localAnnotation,
                    otherAnnotations, elementBinding, level.intValue(), this);
        }

        // Look for attribute ref
        MemberValuePair[] pairs = annotationType.memberValuePairs();
        for (int j = 0, pl = pairs.length; j < pl; j++) {
            MemberValuePair pair = pairs[j];
            level = (Integer) nodeSet.matchingNodes.removeKey(pair);
            if (level != null && enclosesElement) {
                ASTNode reference = (annotationType instanceof SingleMemberAnnotation)
                        ? (ASTNode) annotationType
                        : pair;
                if (!handlesCreated) {
                    localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
                    if (length > 0) {
                        otherAnnotations = new IJavaElement[length];
                        for (int o = 0; o < length; o++) {
                            otherAnnotations[o] = createHandle(annotationType, (IAnnotatable) otherElements[o]);
                        }
                    }
                    handlesCreated = true;
                }
                this.patternLocator.matchReportReference(reference, enclosingElement, localAnnotation,
                        otherAnnotations, pair.binding, level.intValue(), this);
            }
        }

        // Look for reference inside annotation
        ASTNode[] nodes = nodeSet.matchingNodes(annotationType.sourceStart,
                annotationType.declarationSourceEnd);
        if (nodes != null) {
            if (!matchedContainer) {
                for (int j = 0, nl = nodes.length; j < nl; j++) {
                    nodeSet.matchingNodes.removeKey(nodes[j]);
                }
            } else {
                for (int j = 0, nl = nodes.length; j < nl; j++) {
                    ASTNode node = nodes[j];
                    level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        if (!handlesCreated) {
                            localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
                            if (length > 0) {
                                otherAnnotations = new IJavaElement[length];
                                for (int o = 0; o < length; o++) {
                                    otherAnnotations[o] = createHandle(annotationType,
                                            (IAnnotatable) otherElements[o]);
                                }
                            }
                            handlesCreated = true;
                        }
                        this.patternLocator.matchReportReference(node, enclosingElement, localAnnotation,
                                otherAnnotations, elementBinding, level.intValue(), this);
                    }
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Visit the given resolved parse tree and report the nodes that match the search pattern.
 *//* w w  w. j  ava  2s .  co m*/
protected void reportMatching(CompilationUnitDeclaration unit, boolean mustResolve) throws CoreException {
    MatchingNodeSet nodeSet = this.currentPossibleMatch.nodeSet;
    boolean locatorMustResolve = this.patternLocator.mustResolve;
    if (nodeSet.mustResolve)
        this.patternLocator.mustResolve = true;
    if (BasicSearchEngine.VERBOSE) {
        System.out.println("Report matching: "); //$NON-NLS-1$
        int size = nodeSet.matchingNodes == null ? 0 : nodeSet.matchingNodes.elementSize;
        System.out.print("   - node set: accurate=" + size); //$NON-NLS-1$
        size = nodeSet.possibleMatchingNodesSet == null ? 0 : nodeSet.possibleMatchingNodesSet.elementSize;
        System.out.println(", possible=" + size); //$NON-NLS-1$
        System.out.print("   - must resolve: " + mustResolve); //$NON-NLS-1$
        System.out.print(" (locator: " + this.patternLocator.mustResolve); //$NON-NLS-1$
        System.out.println(", nodeSet: " + nodeSet.mustResolve + ')'); //$NON-NLS-1$
        System.out.println("   - fine grain flags=" //$NON-NLS-1$
                + JavaSearchPattern.getFineGrainFlagString(this.patternLocator.fineGrain()));
    }
    if (mustResolve) {
        this.unitScope = unit.scope.compilationUnitScope();
        // move the possible matching nodes that exactly match the search pattern to the matching nodes set
        Object[] nodes = nodeSet.possibleMatchingNodesSet.values;
        for (int i = 0, l = nodes.length; i < l; i++) {
            ASTNode node = (ASTNode) nodes[i];
            if (node == null)
                continue;
            if (node instanceof ImportReference) {
                // special case for import refs: they don't know their binding
                // import ref cannot be in the hierarchy of a type
                if (this.hierarchyResolver != null)
                    continue;

                ImportReference importRef = (ImportReference) node;
                Binding binding = (importRef.bits & ASTNode.OnDemand) != 0
                        ? this.unitScope.getImport(
                                CharOperation.subarray(importRef.tokens, 0, importRef.tokens.length), true,
                                importRef.isStatic())
                        : this.unitScope.getImport(importRef.tokens, false, importRef.isStatic());
                this.patternLocator.matchLevelAndReportImportRef(importRef, binding, this);
            } else {
                nodeSet.addMatch(node, this.patternLocator.resolveLevel(node));
            }
        }
        nodeSet.possibleMatchingNodesSet = new SimpleSet(3);
        if (BasicSearchEngine.VERBOSE) {
            int size = nodeSet.matchingNodes == null ? 0 : nodeSet.matchingNodes.elementSize;
            System.out.print("   - node set: accurate=" + size); //$NON-NLS-1$
            size = nodeSet.possibleMatchingNodesSet == null ? 0 : nodeSet.possibleMatchingNodesSet.elementSize;
            System.out.println(", possible=" + size); //$NON-NLS-1$
        }
    } else {
        this.unitScope = null;
    }

    if (nodeSet.matchingNodes.elementSize == 0)
        return; // no matching nodes were found
    this.methodHandles = new HashSet();

    boolean matchedUnitContainer = (this.matchContainer & PatternLocator.COMPILATION_UNIT_CONTAINER) != 0;

    // report references in javadoc
    if (unit.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(unit.javadoc.sourceStart, unit.javadoc.sourceEnd);
        if (nodes != null) {
            if (!matchedUnitContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                IJavaElement element = createPackageDeclarationHandle(unit);
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (encloses(element)) {
                        this.patternLocator.matchReportReference(node, element, null, null, null/*no binding*/,
                                level.intValue(), this);
                    }
                }
            }
        }
    }

    if (matchedUnitContainer) {
        ImportReference pkg = unit.currentPackage;
        if (pkg != null && pkg.annotations != null) {
            IJavaElement element = createPackageDeclarationHandle(unit);
            if (element != null) {
                reportMatching(pkg.annotations, element, null, null, nodeSet, true, encloses(element));
            }
        }

        ImportReference[] imports = unit.imports;
        if (imports != null) {
            for (int i = 0, l = imports.length; i < l; i++) {
                ImportReference importRef = imports[i];
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(importRef);
                if (level != null) {
                    this.patternLocator.matchReportImportRef(importRef, null /*no binding*/,
                            createImportHandle(importRef), level.intValue(), this);
                }
            }
        }
    }

    TypeDeclaration[] types = unit.types;
    if (types != null) {
        for (int i = 0, l = types.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // reported all the matching nodes
            TypeDeclaration type = types[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(type);
            int accuracy = (level != null && matchedUnitContainer) ? level.intValue() : -1;
            reportMatching(type, null, accuracy, nodeSet, 1);
        }
    }

    // Clear handle cache
    this.methodHandles = null;
    this.bindings.removeKey(this.pattern);
    this.patternLocator.mustResolve = locatorMustResolve;
}

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  av 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;
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * Visit the given type declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 *///from ww  w. j a v  a2 s .com
protected void reportMatching(TypeDeclaration type, IJavaElement parent, int accuracy, MatchingNodeSet nodeSet,
        int occurrenceCount) throws CoreException {
    // create type handle
    IJavaElement enclosingElement = parent;
    if (enclosingElement == null) {
        enclosingElement = createTypeHandle(new String(type.name));
    } else if (enclosingElement instanceof IType) {
        enclosingElement = ((IType) parent).getType(new String(type.name));
    } else if (enclosingElement instanceof IMember) {
        IMember member = (IMember) parent;
        if (member.isBinary()) {
            enclosingElement = ((IClassFile) this.currentPossibleMatch.openable).getType();
        } else {
            enclosingElement = member.getType(new String(type.name), occurrenceCount);
        }
    }
    if (enclosingElement == null)
        return;
    boolean enclosesElement = encloses(enclosingElement);

    // report the type declaration
    if (accuracy > -1 && enclosesElement) {
        int offset = type.sourceStart;
        SearchMatch match = this.patternLocator.newDeclarationMatch(type, enclosingElement, type.binding,
                accuracy, type.sourceEnd - offset + 1, this);
        report(match);
    }

    boolean matchedClassContainer = (this.matchContainer & PatternLocator.CLASS_CONTAINER) != 0;

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

    // report annotations
    if (type.annotations != null) {
        reportMatching(type.annotations, enclosingElement, null, type.binding, nodeSet, matchedClassContainer,
                enclosesElement);
    }

    // report references in javadoc
    if (type.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(type.declarationSourceStart, type.sourceStart);
        if (nodes != null) {
            if (!matchedClassContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                type.binding, level.intValue(), this);
                    }
                }
            }
        }
    }

    // super types
    if ((type.bits & ASTNode.IsAnonymousType) != 0) {
        TypeReference superType = type.allocation.type;
        if (superType != null) {
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(superType);
            if (level != null && matchedClassContainer)
                this.patternLocator.matchReportReference(superType, enclosingElement, null, null, type.binding,
                        level.intValue(), this);
        }
    } else {
        TypeReference superClass = type.superclass;
        if (superClass != null) {
            reportMatchingSuper(superClass, enclosingElement, type.binding, nodeSet, matchedClassContainer);
        }
        TypeReference[] superInterfaces = type.superInterfaces;
        if (superInterfaces != null) {
            for (int i = 0, l = superInterfaces.length; i < l; i++) {
                reportMatchingSuper(superInterfaces[i], enclosingElement, type.binding, nodeSet,
                        matchedClassContainer);
            }
        }
    }

    // filter out element not in hierarchy scope
    boolean typeInHierarchy = type.binding == null || typeInHierarchy(type.binding);
    matchedClassContainer = matchedClassContainer && typeInHierarchy;

    // Visit fields
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        FieldDeclaration[] otherFields = null;
        int first = -1;
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            FieldDeclaration field = fields[i];
            boolean last = field.endPart2Position == 0 || field.declarationEnd == field.endPart2Position;
            // Store first index of multiple field declaration
            if (!last) {
                if (first == -1) {
                    first = i;
                }
            }
            if (first >= 0) {
                // Store all multiple fields but first one for other elements
                if (i > first) {
                    if (otherFields == null) {
                        otherFields = new FieldDeclaration[length - i];
                    }
                    otherFields[i - 1 - first] = field;
                }
                // On last field, report match with all other elements
                if (last) {
                    for (int j = first; j <= i; j++) {
                        Integer level = (Integer) nodeSet.matchingNodes.removeKey(fields[j]);
                        int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                        reportMatching(fields[j], otherFields, type, enclosingElement, value, typeInHierarchy,
                                nodeSet);
                    }
                    first = -1;
                    otherFields = null;
                }
            } else {
                // Single field, report normally
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(field);
                int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                reportMatching(field, null, type, enclosingElement, value, typeInHierarchy, nodeSet);
            }
        }
    }

    // Visit methods
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        for (int i = 0, l = methods.length; i < l; i++) {
            AbstractMethodDeclaration method = methods[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(method);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(method, type, enclosingElement, value, typeInHierarchy, nodeSet);
        }
    }

    // Visit types
    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        for (int i = 0, l = memberTypes.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // end as all matching nodes were reported
            TypeDeclaration memberType = memberTypes[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(memberType);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(memberType, enclosingElement, value, nodeSet, 1);
        }
    }
}

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

License:Open Source License

protected void reportMatchingSuper(TypeReference superReference, IJavaElement enclosingElement,
        Binding elementBinding, MatchingNodeSet nodeSet, boolean matchedClassContainer) throws CoreException {
    ASTNode[] nodes = null;/*from   w w w. j av  a  2s.c  o  m*/
    if (superReference instanceof ParameterizedSingleTypeReference
            || superReference instanceof ParameterizedQualifiedTypeReference) {
        long lastTypeArgumentInfo = findLastTypeArgumentInfo(superReference);
        nodes = nodeSet.matchingNodes(superReference.sourceStart, (int) lastTypeArgumentInfo);
    }
    if (nodes != null) {
        if ((this.matchContainer & PatternLocator.CLASS_CONTAINER) == 0) {
            for (int i = 0, l = nodes.length; i < l; i++)
                nodeSet.matchingNodes.removeKey(nodes[i]);
        } else {
            if (encloses(enclosingElement))
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    this.patternLocator.matchReportReference(node, enclosingElement, null, null, elementBinding,
                            level.intValue(), this);
                }
        }
    } else if (encloses(enclosingElement)) {
        Integer level = (Integer) nodeSet.matchingNodes.removeKey(superReference);
        if (level != null && matchedClassContainer)
            this.patternLocator.matchReportReference(superReference, enclosingElement, null, null,
                    elementBinding, level.intValue(), this);
    }
}