001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.search;
011:
012: import org.eclipse.core.resources.IResource;
013: import org.eclipse.jdt.core.IJavaElement;
014: import org.eclipse.jdt.internal.core.JavaElement;
015:
016: /**
017: * A search match represents the result of a search query.
018: *
019: * Search matches may be accurate (<code>A_ACCURATE</code>) or they might be
020: * merely potential matches (<code>A_INACCURATE</code>). The latter occurs when
021: * a compile-time problem prevents the search engine from completely resolving
022: * the match.
023: * <p>
024: * This class is intended to be instantiated and subclassed by clients.
025: * </p>
026: *
027: * @see SearchEngine#search(SearchPattern, SearchParticipant[], IJavaSearchScope, SearchRequestor, org.eclipse.core.runtime.IProgressMonitor)
028: * @since 3.0
029: */
030: public class SearchMatch {
031:
032: /**
033: * The search result corresponds an exact match of the search pattern.
034: *
035: * @see #getAccuracy()
036: */
037: public static final int A_ACCURATE = 0;
038:
039: /**
040: * The search result is potentially a match for the search pattern,
041: * but the search engine is unable to fully check it (for example, because
042: * there are errors in the code or the classpath are not correctly set).
043: *
044: * @see #getAccuracy()
045: */
046: public static final int A_INACCURATE = 1;
047:
048: private Object element;
049: private int length;
050: private int offset;
051:
052: private int accuracy;
053: private SearchParticipant participant;
054: private IResource resource;
055:
056: private boolean insideDocComment = false;
057:
058: // store the rule used while reporting the match
059: private final static int ALL_GENERIC_FLAVORS = SearchPattern.R_FULL_MATCH
060: | SearchPattern.R_EQUIVALENT_MATCH
061: | SearchPattern.R_ERASURE_MATCH;
062: private int rule = ALL_GENERIC_FLAVORS;
063:
064: // store other necessary information
065: private boolean raw = false;
066: private boolean implicit = false;
067:
068: /**
069: * Creates a new search match.
070: * <p>
071: * Note that <code>isInsideDocComment()</code> defaults to false.
072: * </p>
073: *
074: * @param element the element that encloses or corresponds to the match,
075: * or <code>null</code> if none
076: * @param accuracy one of {@link #A_ACCURATE} or {@link #A_INACCURATE}
077: * @param offset the offset the match starts at, or -1 if unknown
078: * @param length the length of the match, or -1 if unknown
079: * @param participant the search participant that created the match
080: * @param resource the resource of the element, or <code>null</code> if none
081: */
082: public SearchMatch(IJavaElement element, int accuracy, int offset,
083: int length, SearchParticipant participant,
084: IResource resource) {
085: this .element = element;
086: this .offset = offset;
087: this .length = length;
088: this .accuracy = accuracy & A_INACCURATE;
089: if (accuracy > A_INACCURATE) {
090: int genericFlavors = accuracy & ALL_GENERIC_FLAVORS;
091: if (genericFlavors > 0) {
092: this .rule &= ~ALL_GENERIC_FLAVORS; // reset generic flavors
093: }
094: this .rule |= accuracy & ~A_INACCURATE; // accuracy may have also some rule information
095: }
096: this .participant = participant;
097: this .resource = resource;
098: }
099:
100: /**
101: * Returns the accuracy of this search match.
102: *
103: * @return one of {@link #A_ACCURATE} or {@link #A_INACCURATE}
104: */
105: public final int getAccuracy() {
106: return this .accuracy;
107: }
108:
109: /**
110: * Returns the element of this search match.
111: * In case of a reference match, this is the inner-most enclosing element of the reference.
112: * In case of a declaration match, this is the declaration.
113: *
114: * @return the element of the search match, or <code>null</code> if none
115: */
116: public final Object getElement() {
117: return this .element;
118: }
119:
120: /**
121: * Returns the length of this search match.
122: *
123: * @return the length of this search match, or -1 if unknown
124: */
125: public final int getLength() {
126: return this .length;
127: }
128:
129: /**
130: * Returns the offset of this search match.
131: *
132: * @return the offset of this search match, or -1 if unknown
133: */
134: public final int getOffset() {
135: return this .offset;
136: }
137:
138: /**
139: * Returns the search participant which issued this search match.
140: *
141: * @return the participant which issued this search match
142: */
143: public final SearchParticipant getParticipant() {
144: return this .participant;
145: }
146:
147: /**
148: * Returns the resource containing this search match.
149: *
150: * @return the resource of the match, or <code>null</code> if none
151: */
152: public final IResource getResource() {
153: return this .resource;
154: }
155:
156: /**
157: * Returns the rule used while creating the match.
158: *
159: * @return one of {@link SearchPattern#R_FULL_MATCH}, {@link SearchPattern#R_EQUIVALENT_MATCH}
160: * or {@link SearchPattern#R_ERASURE_MATCH}
161: * @since 3.1
162: */
163: public final int getRule() {
164: return this .rule;
165: }
166:
167: /**
168: * Returns whether match element is compatible with searched pattern or not.
169: * Note that equivalent matches are also erasure ones.
170: *
171: * @return <code>true</code> if match element is compatible
172: * <code>false</code> otherwise
173: * @since 3.1
174: */
175: public final boolean isEquivalent() {
176: return isErasure()
177: && (this .rule & SearchPattern.R_EQUIVALENT_MATCH) != 0;
178: }
179:
180: /**
181: * Returns whether match element only has same erasure than searched pattern or not.
182: * Note that this is always true for both generic and non-generic element as soon
183: * as the accuracy is accurate.
184: *
185: * @return <code>true</code> if match element has same erasure
186: * <code>false</code> otherwise
187: * @since 3.1
188: */
189: public final boolean isErasure() {
190: return (this .rule & SearchPattern.R_ERASURE_MATCH) != 0;
191: }
192:
193: /**
194: * Returns whether element matches exactly searched pattern or not.
195: * Note that exact matches are also erasure and equivalent ones.
196: *
197: * @return <code>true</code> if match is exact
198: * <code>false</code> otherwise
199: * @since 3.1
200: */
201: public final boolean isExact() {
202: return isEquivalent()
203: && (this .rule & SearchPattern.R_FULL_MATCH) != 0;
204: }
205:
206: /**
207: * Returns whether the associated element is implicit or not.
208: *
209: * Note that this piece of information is currently only implemented
210: * for implicit member pair value in annotation.
211: *
212: * @return <code>true</code> if this match is associated to an implicit
213: * element and <code>false</code> otherwise
214: * @since 3.1
215: */
216: public final boolean isImplicit() {
217: return this .implicit;
218: }
219:
220: /**
221: * Returns whether the associated element is a raw type/method or not.
222: *
223: * @return <code>true</code> if this match is associated to a raw
224: * type or method and <code>false</code> otherwise
225: * @since 3.1
226: */
227: public final boolean isRaw() {
228: return this .raw;
229: }
230:
231: /**
232: * Returns whether this search match is inside a doc comment of a Java
233: * source file.
234: *
235: * @return <code>true</code> if this search match is inside a doc
236: * comment, and <code>false</code> otherwise
237: */
238: public final boolean isInsideDocComment() {
239: // default is outside a doc comment
240: return this .insideDocComment;
241: }
242:
243: /**
244: * Sets the accuracy of this match.
245: *
246: * @param accuracy one of {@link #A_ACCURATE} or {@link #A_INACCURATE}
247: */
248: public final void setAccuracy(int accuracy) {
249: this .accuracy = accuracy;
250: }
251:
252: /**
253: * Sets the element of this search match.
254: *
255: * @param element the element that encloses or corresponds to the match,
256: * or <code>null</code> if none
257: */
258: public final void setElement(Object element) {
259: this .element = element;
260: }
261:
262: /**
263: * Sets whether this search match is inside a doc comment of a Java
264: * source file.
265: *
266: * @param insideDoc <code>true</code> if this search match is inside a doc
267: * comment, and <code>false</code> otherwise
268: */
269: public final void setInsideDocComment(boolean insideDoc) {
270: this .insideDocComment = insideDoc;
271: }
272:
273: /**
274: * Sets whether the associated element is implicit or not.
275: * Typically, this is the case when match is on an implicit constructor
276: * or an implicit member pair value in annotation.
277: *
278: * @param implicit <code>true</code> if this match is associated to an implicit
279: * element and <code>false</code> otherwise
280: * @since 3.1
281: */
282: public final void setImplicit(boolean implicit) {
283: this .implicit = implicit;
284: }
285:
286: /**
287: * Sets the length of this search match.
288: *
289: * @param length the length of the match, or -1 if unknown
290: */
291: public final void setLength(int length) {
292: this .length = length;
293: }
294:
295: /**
296: * Sets the offset of this search match.
297: *
298: * @param offset the offset the match starts at, or -1 if unknown
299: */
300: public final void setOffset(int offset) {
301: this .offset = offset;
302: }
303:
304: /**
305: * Sets the participant of this match.
306: *
307: * @param participant the search participant that created this match
308: */
309: public final void setParticipant(SearchParticipant participant) {
310: this .participant = participant;
311: }
312:
313: /**
314: * Sets the resource of this match.
315: *
316: * @param resource the resource of the match, or <code>null</code> if none
317: */
318: public final void setResource(IResource resource) {
319: this .resource = resource;
320: }
321:
322: /**
323: * Set the rule used while reporting the match.
324: *
325: * @param rule one of {@link SearchPattern#R_FULL_MATCH}, {@link SearchPattern#R_EQUIVALENT_MATCH}
326: * or {@link SearchPattern#R_ERASURE_MATCH}
327: * @since 3.1
328: */
329: public final void setRule(int rule) {
330: this .rule = rule;
331: }
332:
333: /**
334: * Set whether the associated element is a raw type/method or not.
335: *
336: * @param raw <code>true</code> if this search match is associated to a raw
337: * type or method and <code>false</code> otherwise
338: * @since 3.1
339: */
340: public final void setRaw(boolean raw) {
341: this .raw = raw;
342: }
343:
344: /* (non-javadoc)
345: * @see java.lang.Object#toString()
346: */
347: public String toString() {
348: StringBuffer buffer = new StringBuffer();
349: buffer.append("Search match"); //$NON-NLS-1$
350: buffer.append("\n accuracy="); //$NON-NLS-1$
351: buffer
352: .append(this .accuracy == A_ACCURATE ? "ACCURATE" : "INACCURATE"); //$NON-NLS-1$ //$NON-NLS-2$
353: buffer.append("\n rule="); //$NON-NLS-1$
354: if ((this .rule & SearchPattern.R_FULL_MATCH) != 0) {
355: buffer.append("EXACT"); //$NON-NLS-1$
356: } else if ((this .rule & SearchPattern.R_EQUIVALENT_MATCH) != 0) {
357: buffer.append("EQUIVALENT"); //$NON-NLS-1$
358: } else if ((this .rule & SearchPattern.R_ERASURE_MATCH) != 0) {
359: buffer.append("ERASURE"); //$NON-NLS-1$
360: }
361: buffer.append("\n raw="); //$NON-NLS-1$
362: buffer.append(this .raw);
363: buffer.append("\n offset="); //$NON-NLS-1$
364: buffer.append(this .offset);
365: buffer.append("\n length="); //$NON-NLS-1$
366: buffer.append(this .length);
367: if (this .element != null) {
368: buffer.append("\n element="); //$NON-NLS-1$
369: buffer.append(((JavaElement) getElement())
370: .toStringWithAncestors());
371: }
372: buffer.append("\n"); //$NON-NLS-1$
373: return buffer.toString();
374: }
375: }
|