KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > RefactoringSearchEngine2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31
32 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
33 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
34
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IJavaProject;
38 import org.eclipse.jdt.core.JavaCore;
39 import org.eclipse.jdt.core.JavaModelException;
40 import org.eclipse.jdt.core.WorkingCopyOwner;
41 import org.eclipse.jdt.core.search.IJavaSearchScope;
42 import org.eclipse.jdt.core.search.SearchEngine;
43 import org.eclipse.jdt.core.search.SearchMatch;
44 import org.eclipse.jdt.core.search.SearchPattern;
45 import org.eclipse.jdt.core.search.SearchRequestor;
46
47 import org.eclipse.jdt.internal.corext.util.Messages;
48 import org.eclipse.jdt.internal.corext.util.SearchUtils;
49
50 /**
51  * Helper class to use the search engine in refactorings.
52  *
53  * @since 3.1
54  */

55 public final class RefactoringSearchEngine2 {
56
57     /** Default implementation of a search requestor */
58     private static class DefaultSearchRequestor implements IRefactoringSearchRequestor {
59
60         public final SearchMatch acceptSearchMatch(final SearchMatch match) {
61             return match;
62         }
63     }
64
65     /** Search requestor which only collects compilation units */
66     private class RefactoringCompilationUnitCollector extends RefactoringSearchCollector {
67
68         /** The collected compilation units */
69         private final Set JavaDoc fCollectedUnits= new HashSet JavaDoc();
70
71         /** The inaccurate matches */
72         private final Set JavaDoc fInaccurateMatches= new HashSet JavaDoc();
73
74         public final void acceptSearchMatch(final SearchMatch match) throws CoreException {
75             final SearchMatch accepted= fRequestor.acceptSearchMatch(match);
76             if (accepted != null) {
77                 final IResource resource= accepted.getResource();
78                 if (!resource.equals(fLastResource)) {
79                     final IJavaElement element= JavaCore.create(resource);
80                     if (element instanceof ICompilationUnit)
81                         fCollectedUnits.add(element);
82                 }
83                 if (fInaccurate && accepted.getAccuracy() == SearchMatch.A_INACCURATE && !fInaccurateMatches.contains(accepted)) {
84                     fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_inaccurate_match, accepted.getResource().getName()), null, null, RefactoringStatusEntry.NO_CODE);
85                     fInaccurateMatches.add(accepted);
86                 }
87             }
88         }
89
90         public final void clearResults() {
91             super.clearResults();
92             fCollectedUnits.clear();
93             fInaccurateMatches.clear();
94         }
95
96         public final Collection JavaDoc getBinaryResources() {
97             return Collections.EMPTY_SET;
98         }
99
100         public final Collection JavaDoc getCollectedMatches() {
101             return fCollectedUnits;
102         }
103
104         public final Collection JavaDoc getInaccurateMatches() {
105             return fInaccurateMatches;
106         }
107     }
108
109     private abstract class RefactoringSearchCollector extends SearchRequestor {
110
111         protected IResource fLastResource= null;
112
113         public void clearResults() {
114             fLastResource= null;
115         }
116
117         public abstract Collection JavaDoc getBinaryResources();
118
119         public abstract Collection JavaDoc getCollectedMatches();
120
121         public abstract Collection JavaDoc getInaccurateMatches();
122     }
123
124     /** Search requestor which collects every search match */
125     private class RefactoringSearchMatchCollector extends RefactoringSearchCollector {
126
127         /** The binary resources */
128         private final Set JavaDoc fBinaryResources= new HashSet JavaDoc();
129
130         /** The collected matches */
131         private final List JavaDoc fCollectedMatches= new ArrayList JavaDoc();
132
133         /** The inaccurate matches */
134         private final Set JavaDoc fInaccurateMatches= new HashSet JavaDoc();
135
136         public final void acceptSearchMatch(final SearchMatch match) throws CoreException {
137             final SearchMatch accepted= fRequestor.acceptSearchMatch(match);
138             if (accepted != null) {
139                 fCollectedMatches.add(accepted);
140                 final IResource resource= accepted.getResource();
141                 if (!resource.equals(fLastResource)) {
142                     if (fBinary) {
143                         final IJavaElement element= JavaCore.create(resource);
144                         if (!(element instanceof ICompilationUnit)) {
145                             final IProject project= resource.getProject();
146                             if (!fGrouping)
147                                 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_binary_match_ungrouped, project.getName()), null, null, RefactoringStatusEntry.NO_CODE);
148                             else if (!fBinaryResources.contains(resource))
149                                 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_binary_match_grouped, project.getName()), null, null, RefactoringStatusEntry.NO_CODE);
150                             fBinaryResources.add(resource);
151                         }
152                     }
153                     if (fInaccurate && accepted.getAccuracy() == SearchMatch.A_INACCURATE && !fInaccurateMatches.contains(accepted)) {
154                         fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_inaccurate_match, resource.getName()), null, null, RefactoringStatusEntry.NO_CODE);
155                         fInaccurateMatches.add(accepted);
156                     }
157                 }
158             }
159         }
160
161         public final void clearResults() {
162             super.clearResults();
163             fCollectedMatches.clear();
164             fInaccurateMatches.clear();
165             fBinaryResources.clear();
166         }
167
168         public final Collection JavaDoc getBinaryResources() {
169             return fBinaryResources;
170         }
171
172         public final Collection JavaDoc getCollectedMatches() {
173             return fCollectedMatches;
174         }
175
176         public final Collection JavaDoc getInaccurateMatches() {
177             return fInaccurateMatches;
178         }
179     }
180
181     /** The compilation unit granularity */
182     public static final int GRANULARITY_COMPILATION_UNIT= 2;
183
184     /** The search match granularity */
185     public static final int GRANULARITY_SEARCH_MATCH= 1;
186
187     /** Should binary matches be filtered? */
188     private boolean fBinary= false;
189
190     /** The refactoring search collector */
191     private RefactoringSearchCollector fCollector= null;
192
193     /** The search granularity */
194     private int fGranularity= GRANULARITY_SEARCH_MATCH;
195
196     /** Should the matches be grouped by resource? */
197     private boolean fGrouping= true;
198
199     /** Should inaccurate matches be filtered? */
200     private boolean fInaccurate= true;
201
202     /** The working copy owner, or <code>null</code> */
203     private WorkingCopyOwner fOwner= null;
204
205     /** The search pattern, or <code>null</code> */
206     private SearchPattern fPattern= null;
207
208     /** The search requestor */
209     private IRefactoringSearchRequestor fRequestor= new DefaultSearchRequestor();
210
211     /** The search scope */
212     private IJavaSearchScope fScope= SearchEngine.createWorkspaceScope();
213
214     /** The severity */
215     private int fSeverity= RefactoringStatus.WARNING;
216
217     /** The search status */
218     private RefactoringStatus fStatus= new RefactoringStatus();
219
220     /** The working copies */
221     private ICompilationUnit[] fWorkingCopies= {};
222     
223     /**
224      * Creates a new refactoring search engine.
225      */

226     public RefactoringSearchEngine2() {
227         // Do nothing
228
}
229
230     /**
231      * Creates a new refactoring search engine.
232      *
233      * @param pattern the search pattern
234      */

235     public RefactoringSearchEngine2(final SearchPattern pattern) {
236         Assert.isNotNull(pattern);
237         fPattern= pattern;
238     }
239
240     /**
241      * Clears all results found so far, and sets resets the status to {@link RefactoringStatus#OK}.
242      */

243     public final void clearResults() {
244         getCollector().clearResults();
245         fStatus= new RefactoringStatus();
246     }
247
248     /**
249      * Returns the affected compilation units of the previous search queries.
250      * <p>
251      * In order to retrieve the compilation units, grouping by resource must have been enabled before searching.
252      *
253      * @return the compilation units of the previous queries
254      */

255     public final ICompilationUnit[] getAffectedCompilationUnits() {
256         if (fGranularity == GRANULARITY_COMPILATION_UNIT) {
257             final Collection JavaDoc collection= getCollector().getCollectedMatches();
258             final ICompilationUnit[] units= new ICompilationUnit[collection.size()];
259             int index= 0;
260             for (final Iterator JavaDoc iterator= collection.iterator(); iterator.hasNext(); index++)
261                 units[index]= (ICompilationUnit) iterator.next();
262             return units;
263         } else {
264             final SearchResultGroup[] groups= getGroupedMatches();
265             final ICompilationUnit[] units= new ICompilationUnit[groups.length];
266             for (int index= 0; index < groups.length; index++)
267                 units[index]= groups[index].getCompilationUnit();
268             return units;
269         }
270     }
271
272     /**
273      * Returns the affected java projects of the previous search queries.
274      * <p>
275      * In order to retrieve the java projects, grouping by resource must have been enabled before searching.
276      *
277      * @return the java projects of the previous queries (element type: <code>&ltIJavaProject, Collection&ltSearchResultGroup&gt&gt</code>)
278      */

279     public final Map JavaDoc getAffectedProjects() {
280         final Map JavaDoc map= new HashMap JavaDoc();
281         IJavaProject project= null;
282         ICompilationUnit unit= null;
283         if (fGranularity == GRANULARITY_COMPILATION_UNIT) {
284             final ICompilationUnit[] units= getAffectedCompilationUnits();
285             for (int index= 0; index < units.length; index++) {
286                 unit= units[index];
287                 project= unit.getJavaProject();
288                 if (project != null) {
289                     Set JavaDoc set= (Set JavaDoc) map.get(project);
290                     if (set == null) {
291                         set= new HashSet JavaDoc();
292                         map.put(project, set);
293                     }
294                     set.add(unit);
295                 }
296             }
297         } else {
298             final SearchResultGroup[] groups= getGroupedMatches();
299             SearchResultGroup group= null;
300             for (int index= 0; index < groups.length; index++) {
301                 group= groups[index];
302                 unit= group.getCompilationUnit();
303                 if (unit != null) {
304                     project= unit.getJavaProject();
305                     if (project != null) {
306                         Set JavaDoc set= (Set JavaDoc) map.get(project);
307                         if (set == null) {
308                             set= new HashSet JavaDoc();
309                             map.put(project, set);
310                         }
311                         set.add(group);
312                     }
313                 }
314             }
315         }
316         return map;
317     }
318
319     /**
320      * Returns the refactoring search collector.
321      *
322      * @return the refactoring search collector
323      */

324     private RefactoringSearchCollector getCollector() {
325         if (fCollector == null) {
326             if (fGranularity == GRANULARITY_COMPILATION_UNIT)
327                 fCollector= new RefactoringCompilationUnitCollector();
328             else if (fGranularity == GRANULARITY_SEARCH_MATCH)
329                 fCollector= new RefactoringSearchMatchCollector();
330             else
331                 Assert.isTrue(false);
332         }
333         return fCollector;
334     }
335
336     /**
337      * Returns the found search matches in grouped by their containing resource.
338      *
339      * @return the found search matches
340      */

341     private SearchResultGroup[] getGroupedMatches() {
342         final Map JavaDoc grouped= new HashMap JavaDoc();
343         List JavaDoc matches= null;
344         IResource resource= null;
345         SearchMatch match= null;
346         for (final Iterator JavaDoc iterator= getSearchMatches().iterator(); iterator.hasNext();) {
347             match= (SearchMatch) iterator.next();
348             resource= match.getResource();
349             if (!grouped.containsKey(resource))
350                 grouped.put(resource, new ArrayList JavaDoc(4));
351             matches= (List JavaDoc) grouped.get(resource);
352             matches.add(match);
353         }
354         if (fBinary) {
355             final Collection JavaDoc collection= getCollector().getBinaryResources();
356             for (final Iterator JavaDoc iterator= grouped.keySet().iterator(); iterator.hasNext();) {
357                 resource= (IResource) iterator.next();
358                 if (collection.contains(resource))
359                     iterator.remove();
360             }
361         }
362         final SearchResultGroup[] result= new SearchResultGroup[grouped.keySet().size()];
363         int index= 0;
364         for (final Iterator JavaDoc iterator= grouped.keySet().iterator(); iterator.hasNext();) {
365             resource= (IResource) iterator.next();
366             matches= (List JavaDoc) grouped.get(resource);
367             result[index++]= new SearchResultGroup(resource, ((SearchMatch[]) matches.toArray(new SearchMatch[matches.size()])));
368         }
369         return result;
370     }
371
372     /**
373      * Returns the search pattern currently used for searching.
374      *
375      * @return the search pattern
376      */

377     public final SearchPattern getPattern() {
378         return fPattern;
379     }
380
381     /**
382      * Returns the results of the previous search queries.
383      * <p>
384      * The result depends on the following conditions:
385      * <ul>
386      * <li>If the search granularity is {@link #GRANULARITY_COMPILATION_UNIT}, the results are elements of type {@link ICompilationUnit}.</li>
387      * <li>If grouping by resource is enabled, the results are elements of type {@link SearchResultGroup}, otherwise the elements are of type {@link SearchMatch}.</li>
388      * </ul>
389      *
390      * @return the results of the previous queries
391      */

392     public final Object JavaDoc[] getResults() {
393         if (fGranularity == GRANULARITY_COMPILATION_UNIT)
394             return getAffectedCompilationUnits();
395         else {
396             if (fGrouping)
397                 return getGroupedMatches();
398             else
399                 return getUngroupedMatches();
400         }
401     }
402
403     /**
404      * Returns the search matches filtered by their accuracy.
405      *
406      * @return the filtered search matches
407      */

408     private Collection JavaDoc getSearchMatches() {
409         Collection JavaDoc results= null;
410         if (fInaccurate) {
411             results= new LinkedList JavaDoc(getCollector().getCollectedMatches());
412             final Collection JavaDoc collection= getCollector().getInaccurateMatches();
413             SearchMatch match= null;
414             for (final Iterator JavaDoc iterator= results.iterator(); iterator.hasNext();) {
415                 match= (SearchMatch) iterator.next();
416                 if (collection.contains(match))
417                     iterator.remove();
418             }
419         } else
420             results= getCollector().getCollectedMatches();
421         return results;
422     }
423
424     /**
425      * Returns the refactoring status of this search engine.
426      *
427      * @return the refactoring status
428      */

429     public final RefactoringStatus getStatus() {
430         return fStatus;
431     }
432
433     /**
434      * Returns the found search matches in no particular order.
435      *
436      * @return the found search matches
437      */

438     private SearchMatch[] getUngroupedMatches() {
439         Collection JavaDoc results= null;
440         if (fBinary) {
441             results= new LinkedList JavaDoc(getSearchMatches());
442             final Collection JavaDoc collection= getCollector().getBinaryResources();
443             SearchMatch match= null;
444             for (final Iterator JavaDoc iterator= results.iterator(); iterator.hasNext();) {
445                 match= (SearchMatch) iterator.next();
446                 if (collection.contains(match.getResource()))
447                     iterator.remove();
448             }
449         } else
450             results= getSearchMatches();
451         final SearchMatch[] matches= new SearchMatch[results.size()];
452         results.toArray(matches);
453         return matches;
454     }
455
456     /**
457      * Performs the search according to the specified pattern.
458      *
459      * @param monitor the progress monitor, or <code>null</code>
460      * @throws JavaModelException if an error occurs during search
461      */

462     public final void searchPattern(final IProgressMonitor monitor) throws JavaModelException {
463         Assert.isNotNull(fPattern);
464         try {
465             monitor.beginTask("", 1); //$NON-NLS-1$
466
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_occurrences);
467             try {
468                 SearchEngine engine= null;
469                 if (fOwner != null)
470                     engine= new SearchEngine(fOwner);
471                 else
472                     engine= new SearchEngine(fWorkingCopies);
473                 engine.search(fPattern, SearchUtils.getDefaultSearchParticipants(), fScope, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
474             } catch (CoreException exception) {
475                 throw new JavaModelException(exception);
476             }
477         } finally {
478             monitor.done();
479         }
480     }
481
482     /**
483      * Performs the search of referenced fields.
484      *
485      * @param element the java element whose referenced fields have to be found
486      * @param monitor the progress monitor, or <code>null</code>
487      * @throws JavaModelException if an error occurs during search
488      */

489     public final void searchReferencedFields(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException {
490         Assert.isNotNull(element);
491         try {
492             monitor.beginTask("", 1); //$NON-NLS-1$
493
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_fields);
494             try {
495                 SearchEngine engine= null;
496                 if (fOwner != null)
497                     engine= new SearchEngine(fOwner);
498                 else
499                     engine= new SearchEngine(fWorkingCopies);
500                 engine.searchDeclarationsOfAccessedFields(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
501             } catch (CoreException exception) {
502                 throw new JavaModelException(exception);
503             }
504         } finally {
505             monitor.done();
506         }
507     }
508
509     /**
510      * Performs the search of referenced methods.
511      *
512      * @param element the java element whose referenced methods have to be found
513      * @param monitor the progress monitor, or <code>null</code>
514      * @throws JavaModelException if an error occurs during search
515      */

516     public final void searchReferencedMethods(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException {
517         Assert.isNotNull(element);
518         try {
519             monitor.beginTask("", 1); //$NON-NLS-1$
520
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_methods);
521             try {
522                 SearchEngine engine= null;
523                 if (fOwner != null)
524                     engine= new SearchEngine(fOwner);
525                 else
526                     engine= new SearchEngine(fWorkingCopies);
527                 engine.searchDeclarationsOfSentMessages(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
528             } catch (CoreException exception) {
529                 throw new JavaModelException(exception);
530             }
531         } finally {
532             monitor.done();
533         }
534     }
535
536     /**
537      * Performs the search of referenced types.
538      *
539      * @param element the java element whose referenced types have to be found
540      * @param monitor the progress monitor, or <code>null</code>
541      * @throws JavaModelException if an error occurs during search
542      */

543     public final void searchReferencedTypes(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException {
544         Assert.isNotNull(element);
545         try {
546             monitor.beginTask("", 1); //$NON-NLS-1$
547
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_types);
548             try {
549                 SearchEngine engine= null;
550                 if (fOwner != null)
551                     engine= new SearchEngine(fOwner);
552                 else
553                     engine= new SearchEngine(fWorkingCopies);
554                 engine.searchDeclarationsOfReferencedTypes(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
555             } catch (CoreException exception) {
556                 throw new JavaModelException(exception);
557             }
558         } finally {
559             monitor.done();
560         }
561     }
562
563     /**
564      * Sets the conjunction of search patterns to be used during search.
565      * <p>
566      * This method must be called before {@link RefactoringSearchEngine2#searchPattern(IProgressMonitor)}
567      *
568      * @param first the first search pattern to set
569      * @param second the second search pattern to set
570      */

571     public final void setAndPattern(final SearchPattern first, final SearchPattern second) {
572         Assert.isNotNull(first);
573         Assert.isNotNull(second);
574         fPattern= SearchPattern.createAndPattern(first, second);
575     }
576
577     /**
578      * Determines how search matches are filtered.
579      * <p>
580      * This method must be called before start searching. The default is to filter inaccurate matches only.
581      *
582      * @param inaccurate <code>true</code> to filter inaccurate matches, <code>false</code> otherwise
583      * @param binary <code>true</code> to filter binary matches, <code>false</code> otherwise
584      */

585     public final void setFiltering(final boolean inaccurate, final boolean binary) {
586         fInaccurate= inaccurate;
587         fBinary= binary;
588     }
589
590     /**
591      * Sets the granularity to use during the searches.
592      * <p>
593      * This method must be called before start searching. The default is a granularity of {@link #GRANULARITY_SEARCH_MATCH}.
594      *
595      * @param granularity The granularity to use. Must be one of the <code>GRANULARITY_XXX</code> constants.
596      */

597     public final void setGranularity(final int granularity) {
598         Assert.isTrue(granularity == GRANULARITY_COMPILATION_UNIT || granularity == GRANULARITY_SEARCH_MATCH);
599         fGranularity= granularity;
600     }
601
602     /**
603      * Sets the working copies to take precedence during the searches.
604      * <p>
605      * This method must be called before start searching. The default is to use no working copies
606      *
607      * @param copies the working copies to use
608      */

609     public final void setWorkingCopies(final ICompilationUnit[] copies) {
610         Assert.isNotNull(copies);
611         fWorkingCopies= new ICompilationUnit[copies.length];
612         System.arraycopy(copies, 0, fWorkingCopies, 0, copies.length);
613     }
614
615     /**
616      * Determines how search matches are grouped.
617      * <p>
618      * This method must be called before start searching. The default is to group by containing resource.
619      *
620      * @param grouping <code>true</code> to group matches by their containing resource, <code>false</code> otherwise
621      */

622     public final void setGrouping(final boolean grouping) {
623         fGrouping= grouping;
624     }
625
626     /**
627      * Sets the disjunction of search patterns to be used during search.
628      * <p>
629      * This method must be called before {@link RefactoringSearchEngine2#searchPattern(IProgressMonitor)}
630      *
631      * @param first the first search pattern to set
632      * @param second the second search pattern to set
633      */

634     public final void setOrPattern(final SearchPattern first, final SearchPattern second) {
635         Assert.isNotNull(first);
636         Assert.isNotNull(second);
637         fPattern= SearchPattern.createOrPattern(first, second);
638     }
639
640     /**
641      * Sets the working copy owner to use during search.
642      * <p>
643      * This method must be called before start searching. The default is to use no working copy owner.
644      *
645      * @param owner the working copy owner to use, or <code>null</code> to use none
646      */

647     public final void setOwner(final WorkingCopyOwner owner) {
648         fOwner= owner;
649     }
650
651     /**
652      * Sets the search pattern to be used during search.
653      * <p>
654      * This method must be called before {@link RefactoringSearchEngine2#searchPattern(IProgressMonitor)}
655      *
656      * @param elements the set of elements
657      * @param limitTo determines the nature of the expected matches. This is a combination of {@link org.eclipse.jdt.core.search.IJavaSearchConstants}.
658      */

659     public final void setPattern(final IJavaElement[] elements, final int limitTo) {
660         Assert.isNotNull(elements);
661         Assert.isTrue(elements.length > 0);
662         SearchPattern pattern= SearchPattern.createPattern(elements[0], limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
663         IJavaElement element= null;
664         for (int index= 1; index < elements.length; index++) {
665             element= elements[index];
666             pattern= SearchPattern.createOrPattern(pattern, SearchPattern.createPattern(element, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
667         }
668         setPattern(pattern);
669     }
670
671     /**
672      * Sets the search pattern to be used during search.
673      * <p>
674      * This method must be called before {@link RefactoringSearchEngine2#searchPattern(IProgressMonitor)}
675      *
676      * @param pattern the search pattern to set
677      */

678     public final void setPattern(final SearchPattern pattern) {
679         Assert.isNotNull(pattern);
680         fPattern= pattern;
681     }
682
683     /**
684      * Sets the search requestor for this search engine.
685      * <p>
686      * This method must be called before start searching. The default is a non-filtering search requestor.
687      *
688      * @param requestor the search requestor to set
689      */

690     public final void setRequestor(final IRefactoringSearchRequestor requestor) {
691         Assert.isNotNull(requestor);
692         fRequestor= requestor;
693     }
694
695     /**
696      * Sets the search scope for this search engine.
697      * <p>
698      * This method must be called before start searching. The default is the entire workspace as search scope.
699      *
700      * @param scope the search scope to set
701      */

702     public final void setScope(final IJavaSearchScope scope) {
703         Assert.isNotNull(scope);
704         fScope= scope;
705     }
706
707     /**
708      * Sets the severity of the generated status entries.
709      * <p>
710      * This method must be called before start searching. The default is a severity of {@link RefactoringStatus#OK}.
711      *
712      * @param severity the severity to set
713      */

714     public final void setSeverity(final int severity) {
715         Assert.isTrue(severity == RefactoringStatus.WARNING || severity == RefactoringStatus.INFO || severity == RefactoringStatus.FATAL || severity == RefactoringStatus.ERROR);
716         fSeverity= severity;
717     }
718
719     /**
720      * Sets the refactoring status for this search engine.
721      * <p>
722      * This method must be called before start searching. The default is an empty status with status {@link RefactoringStatus#OK}.
723      *
724      * @param status the refactoring status to set
725      */

726     public final void setStatus(final RefactoringStatus status) {
727         Assert.isNotNull(status);
728         fStatus= status;
729     }
730 }
731
Popular Tags