]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.refactoring; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.List; | |
15 | ||
16 | import org.eclipse.core.runtime.CoreException; | |
17 | ||
18 | import org.eclipse.jdt.core.IClassFile; | |
19 | import org.eclipse.jdt.core.ICompilationUnit; | |
20 | import org.eclipse.jdt.core.IMember; | |
21 | import org.eclipse.jdt.core.IPackageFragment; | |
22 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
23 | import org.eclipse.jdt.core.JavaModelException; | |
24 | import org.eclipse.jdt.core.search.SearchMatch; | |
25 | import org.eclipse.jdt.core.search.SearchRequestor; | |
26 | ||
27 | import org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext; | |
28 | ||
29 | /** | |
30 | * Collects the results returned by a <code>SearchEngine</code>. | |
31 | * If a {@link ReferencesInBinaryContext} is passed, matches that are | |
32 | * inside a binary element are not collected (but added to the context if they are accurate). | |
33 | */ | |
34 | public class CollectingSearchRequestor extends SearchRequestor { | |
35 | private final ArrayList<SearchMatch> fFound; | |
36 | private final ReferencesInBinaryContext fBinaryRefs; | |
37 | ||
38 | public CollectingSearchRequestor() { | |
39 | this(null); | |
40 | } | |
41 | ||
42 | public CollectingSearchRequestor(ReferencesInBinaryContext binaryRefs) { | |
43 | fFound= new ArrayList<SearchMatch>(); | |
44 | fBinaryRefs= binaryRefs; | |
45 | } | |
46 | ||
47 | /** | |
48 | * The default implementation calls {@link #collectMatch(SearchMatch)} for | |
49 | * all matches that make it through {@link #filterMatch(SearchMatch)}. | |
50 | * | |
51 | * @param match the found match | |
52 | * @throws CoreException | |
53 | * | |
54 | * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch) | |
55 | */ | |
56 | @Override | |
57 | public void acceptSearchMatch(SearchMatch match) throws CoreException { | |
58 | if (! filterMatch(match)) | |
59 | collectMatch(match); | |
60 | } | |
61 | ||
62 | public void collectMatch(SearchMatch match) { | |
63 | fFound.add(match); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Returns whether the given match should be filtered out. | |
68 | * The default implementation filters out matches in binaries iff | |
69 | * {@link #CollectingSearchRequestor(ReferencesInBinaryContext)} has been called with a | |
70 | * non-<code>null</code> argument. Accurate binary matches are added to the {@link ReferencesInBinaryContext}. | |
71 | * | |
72 | * @param match the match to test | |
73 | * @return <code>true</code> iff the given match should <em>not</em> be collected | |
74 | * @throws CoreException | |
75 | */ | |
76 | public boolean filterMatch(SearchMatch match) throws CoreException { | |
77 | if (fBinaryRefs == null) | |
78 | return false; | |
79 | ||
80 | if (match.getAccuracy() == SearchMatch.A_ACCURATE && isBinaryElement(match.getElement())) { | |
81 | // binary classpaths are often incomplete -> avoiding false positives from inaccurate matches | |
82 | fBinaryRefs.add(match); | |
83 | return true; | |
84 | } | |
85 | ||
86 | return false; | |
87 | } | |
88 | ||
89 | private static boolean isBinaryElement(Object element) throws JavaModelException { | |
90 | if (element instanceof IMember) { | |
91 | return ((IMember)element).isBinary(); | |
92 | ||
93 | } else if (element instanceof ICompilationUnit) { | |
94 | return true; | |
95 | ||
96 | } else if (element instanceof IClassFile) { | |
97 | return false; | |
98 | ||
99 | } else if (element instanceof IPackageFragment) { | |
100 | return isBinaryElement(((IPackageFragment) element).getParent()); | |
101 | ||
102 | } else if (element instanceof IPackageFragmentRoot) { | |
103 | return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_BINARY; | |
104 | ||
105 | } | |
106 | return false; | |
107 | ||
108 | } | |
109 | ||
110 | /** | |
111 | * @return a List of {@link SearchMatch}es (not sorted) | |
112 | */ | |
113 | public List<SearchMatch> getResults() { | |
114 | return fFound; | |
115 | } | |
116 | } | |
117 | ||
118 |