KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > MainMethodSearchEngine


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.pde.internal.ui.launcher;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IMethod;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.ITypeHierarchy;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.search.IJavaSearchConstants;
30 import org.eclipse.jdt.core.search.IJavaSearchScope;
31 import org.eclipse.jdt.core.search.SearchEngine;
32 import org.eclipse.jdt.core.search.SearchMatch;
33 import org.eclipse.jdt.core.search.SearchParticipant;
34 import org.eclipse.jdt.core.search.SearchPattern;
35 import org.eclipse.jdt.core.search.SearchRequestor;
36 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
37 import org.eclipse.jface.operation.IRunnableContext;
38 import org.eclipse.jface.operation.IRunnableWithProgress;
39 import org.eclipse.jface.util.Assert;
40 import org.eclipse.pde.internal.ui.PDEPlugin;
41 import org.eclipse.pde.internal.ui.PDEUIMessages;
42
43 public class MainMethodSearchEngine{
44     
45     private class MethodCollector extends SearchRequestor {
46         private List JavaDoc fResult;
47         private int fStyle;
48
49         public MethodCollector(int style) {
50             fResult = new ArrayList JavaDoc(200);
51             fStyle= style;
52         }
53
54         public List JavaDoc getResult() {
55             return fResult;
56         }
57
58         private boolean considerExternalJars() {
59             return (fStyle & IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS) != 0;
60         }
61                 
62         private boolean considerBinaries() {
63             return (fStyle & IJavaElementSearchConstants.CONSIDER_BINARIES) != 0;
64         }
65
66         /* (non-Javadoc)
67          * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
68          */

69         public void acceptSearchMatch(SearchMatch match) throws CoreException {
70             Object JavaDoc enclosingElement = match.getElement();
71             if (enclosingElement instanceof IMethod) { // defensive code
72
try {
73                     IMethod curr= (IMethod) enclosingElement;
74                     if (curr.isMainMethod()) {
75                         if (!considerExternalJars()) {
76                             IPackageFragmentRoot root= getPackageFragmentRoot(curr);
77                             if (root == null || root.isArchive()) {
78                                 return;
79                             }
80                         }
81                         if (!considerBinaries() && curr.isBinary()) {
82                             return;
83                         }
84                         IType declaringType = curr.getDeclaringType();
85                         fResult.add(declaringType);
86                     }
87                 } catch (JavaModelException e) {
88                     PDEPlugin.log(e.getStatus());
89                 }
90             }
91         }
92     }
93
94     /**
95      * Searches for all main methods in the given scope.
96      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
97      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
98      *
99      * @param pm progress monitor
100      * @param scope search scope
101      * @param style search style
102      * @param includeSubtypes whether to consider types that inherit a main method
103      */

104     public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style, boolean includeSubtypes) {
105         pm.beginTask(PDEUIMessages.MainMethodSearchEngine_search, 100);
106         int searchTicks = 100;
107         if (includeSubtypes) {
108             searchTicks = 25;
109         }
110         
111         SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); //$NON-NLS-1$
112
SearchParticipant[] participants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
113         MethodCollector collector = new MethodCollector(style);
114         IProgressMonitor searchMonitor = new SubProgressMonitor(pm, searchTicks);
115         try {
116             new SearchEngine().search(pattern, participants, scope, collector, searchMonitor);
117         } catch (CoreException ce) {
118             PDEPlugin.log(ce);
119         }
120
121         List JavaDoc result = collector.getResult();
122         if (includeSubtypes) {
123             IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, 75);
124             subtypesMonitor.beginTask(PDEUIMessages.MainMethodSearchEngine_search, result.size());
125             Set JavaDoc set = addSubtypes(result, subtypesMonitor, scope);
126             return (IType[]) set.toArray(new IType[set.size()]);
127         }
128         return (IType[]) result.toArray(new IType[result.size()]);
129     }
130
131     private Set JavaDoc addSubtypes(List JavaDoc types, IProgressMonitor monitor, IJavaSearchScope scope) {
132         Iterator JavaDoc iterator = types.iterator();
133         Set JavaDoc result = new HashSet JavaDoc(types.size());
134         while (iterator.hasNext()) {
135             IType type = (IType) iterator.next();
136             if (result.add(type)) {
137                 ITypeHierarchy hierarchy = null;
138                 try {
139                     hierarchy = type.newTypeHierarchy(monitor);
140                     IType[] subtypes = hierarchy.getAllSubtypes(type);
141                     for (int i = 0; i < subtypes.length; i++) {
142                         IType t = subtypes[i];
143                         if (scope.encloses(t)) {
144                             result.add(t);
145                         }
146                     }
147                 } catch (JavaModelException e) {
148                     PDEPlugin.log(e);
149                 }
150             }
151             monitor.worked(1);
152         }
153         return result;
154     }
155     
156     
157     /**
158      * Returns the package fragment root of <code>IJavaElement</code>. If the given
159      * element is already a package fragment root, the element itself is returned.
160      */

161     public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) {
162         return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
163     }
164     
165     /**
166      * Searches for all main methods in the given scope.
167      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
168      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
169      *
170      * @param includeSubtypes whether to consider types that inherit a main method
171      */

172     public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final int style, final boolean includeSubtypes) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
173         int allFlags= IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS | IJavaElementSearchConstants.CONSIDER_BINARIES;
174         Assert.isTrue((style | allFlags) == allFlags);
175         
176         final IType[][] res= new IType[1][];
177         
178         IRunnableWithProgress runnable= new IRunnableWithProgress() {
179             public void run(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
180                 res[0]= searchMainMethods(pm, scope, style, includeSubtypes);
181             }
182         };
183         context.run(true, true, runnable);
184         
185         return res[0];
186     }
187             
188 }
189
Popular Tags