Example usage for org.eclipse.jdt.core.search ReferenceMatch getLocalElement

List of usage examples for org.eclipse.jdt.core.search ReferenceMatch getLocalElement

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search ReferenceMatch getLocalElement.

Prototype

final public IJavaElement getLocalElement() 

Source Link

Document

Returns the local element of this search match, or null if none.

Usage

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

License:Open Source License

protected void report(SearchMatch match) throws CoreException {
    if (match == null) {
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("Cannot report a null match!!!"); //$NON-NLS-1$
        }//from  w w w  . ja v  a 2  s  .  c  o m
        return;
    }
    if (filterEnum(match)) {
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("Filtered package with name enum"); //$NON-NLS-1$
        }
        return;
    }
    long start = -1;
    if (BasicSearchEngine.VERBOSE) {
        start = System.currentTimeMillis();
        System.out.println("Reporting match"); //$NON-NLS-1$
        System.out.println("\tResource: " + match.getResource());//$NON-NLS-1$
        System.out.println("\tPositions: [offset=" + match.getOffset() + ", length=" + match.getLength() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        try {
            if (this.parser != null && match.getOffset() > 0 && match.getLength() > 0
                    && !(match.getElement() instanceof BinaryMember)) {
                String selection = new String(this.parser.scanner.source, match.getOffset(), match.getLength());
                System.out.println("\tSelection: -->" + selection + "<--"); //$NON-NLS-1$ //$NON-NLS-2$
            }
        } catch (Exception e) {
            // it's just for debug purposes... ignore all exceptions in this area
        }
        try {
            JavaElement javaElement = (JavaElement) match.getElement();
            System.out.println("\tJava element: " + javaElement.toStringWithAncestors()); //$NON-NLS-1$
            if (!javaElement.exists()) {
                System.out.println("\t\tWARNING: this element does NOT exist!"); //$NON-NLS-1$
            }
        } catch (Exception e) {
            // it's just for debug purposes... ignore all exceptions in this area
        }
        if (match instanceof ReferenceMatch) {
            try {
                ReferenceMatch refMatch = (ReferenceMatch) match;
                JavaElement local = (JavaElement) refMatch.getLocalElement();
                if (local != null) {
                    System.out.println("\tLocal element: " + local.toStringWithAncestors()); //$NON-NLS-1$
                }
                if (match instanceof TypeReferenceMatch) {
                    IJavaElement[] others = ((TypeReferenceMatch) refMatch).getOtherElements();
                    if (others != null) {
                        int length = others.length;
                        if (length > 0) {
                            System.out.println("\tOther elements:"); //$NON-NLS-1$
                            for (int i = 0; i < length; i++) {
                                JavaElement other = (JavaElement) others[i];
                                System.out.println("\t\t- " + other.toStringWithAncestors()); //$NON-NLS-1$
                            }
                        }
                    }
                }
            } catch (Exception e) {
                // it's just for debug purposes... ignore all exceptions in this area
            }
        }
        System.out.println(match.getAccuracy() == SearchMatch.A_ACCURATE ? "\tAccuracy: EXACT_MATCH" //$NON-NLS-1$
                : "\tAccuracy: POTENTIAL_MATCH"); //$NON-NLS-1$
        System.out.print("\tRule: "); //$NON-NLS-1$
        if (match.isExact()) {
            System.out.print("EXACT"); //$NON-NLS-1$
        } else if (match.isEquivalent()) {
            System.out.print("EQUIVALENT"); //$NON-NLS-1$
        } else if (match.isErasure()) {
            System.out.print("ERASURE"); //$NON-NLS-1$
        } else {
            System.out.print("INVALID RULE"); //$NON-NLS-1$
        }
        if (match instanceof MethodReferenceMatch) {
            MethodReferenceMatch methodReferenceMatch = (MethodReferenceMatch) match;
            if (methodReferenceMatch.isSuperInvocation()) {
                System.out.print("+SUPER INVOCATION"); //$NON-NLS-1$
            }
            if (methodReferenceMatch.isImplicit()) {
                System.out.print("+IMPLICIT"); //$NON-NLS-1$
            }
            if (methodReferenceMatch.isSynthetic()) {
                System.out.print("+SYNTHETIC"); //$NON-NLS-1$
            }
        }
        System.out.println("\n\tRaw: " + match.isRaw()); //$NON-NLS-1$
    }
    this.requestor.acceptSearchMatch(match);
    if (BasicSearchEngine.VERBOSE)
        this.resultCollectorTime += System.currentTimeMillis() - start;
}

From source file:org.eclipse.wb.internal.core.utils.jdt.core.CodeUtils.java

License:Open Source License

/**
 * @return references of given {@link IJavaElement} in given {@link IJavaSearchScope}.
 *///from w ww. ja  va2s  .  c  o  m
private static List<IJavaElement> searchReferences(IJavaSearchScope scope, IJavaElement element)
        throws Exception {
    final List<IJavaElement> references = Lists.newArrayList();
    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(SearchMatch match) {
            if (match instanceof ReferenceMatch) {
                ReferenceMatch refMatch = (ReferenceMatch) match;
                IJavaElement matchElement = (IJavaElement) refMatch.getElement();
                {
                    IJavaElement localElement = refMatch.getLocalElement();
                    if (localElement != null) {
                        matchElement = localElement;
                    }
                }
                references.add(matchElement);
            }
        }
    };
    // do search
    SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES);
    SearchEngine searchEngine = new SearchEngine();
    searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
            requestor, new NullProgressMonitor());
    // done
    return references;
}