KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > MainMethodSearchEngine


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.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 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IMethod;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.ITypeHierarchy;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.search.IJavaSearchConstants;
29 import org.eclipse.jdt.core.search.IJavaSearchScope;
30 import org.eclipse.jdt.core.search.SearchEngine;
31 import org.eclipse.jdt.core.search.SearchMatch;
32 import org.eclipse.jdt.core.search.SearchParticipant;
33 import org.eclipse.jdt.core.search.SearchPattern;
34 import org.eclipse.jdt.core.search.SearchRequestor;
35 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
36 import org.eclipse.jface.operation.IRunnableContext;
37 import org.eclipse.jface.operation.IRunnableWithProgress;
38
39 public class MainMethodSearchEngine{
40     
41     private class MethodCollector extends SearchRequestor {
42         private List JavaDoc fResult;
43
44         public MethodCollector() {
45             fResult = new ArrayList JavaDoc(200);
46         }
47
48         public List JavaDoc getResult() {
49             return fResult;
50         }
51
52         /* (non-Javadoc)
53          * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
54          */

55         public void acceptSearchMatch(SearchMatch match) throws CoreException {
56             Object JavaDoc enclosingElement = match.getElement();
57             if (enclosingElement instanceof IMethod) { // defensive code
58
try {
59                     IMethod curr= (IMethod) enclosingElement;
60                     if (curr.isMainMethod()) {
61                         IType declaringType = curr.getDeclaringType();
62                         fResult.add(declaringType);
63                     }
64                 } catch (JavaModelException e) {
65                     JDIDebugUIPlugin.log(e.getStatus());
66                 }
67             }
68         }
69     }
70
71     /**
72      * Searches for all main methods in the given scope.
73      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
74      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
75      *
76      * @param pm progress monitor
77      * @param scope search scope
78      * @param includeSubtypes whether to consider types that inherit a main method
79      */

80     public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, boolean includeSubtypes) {
81         pm.beginTask(LauncherMessages.MainMethodSearchEngine_1, 100);
82         int searchTicks = 100;
83         if (includeSubtypes) {
84             searchTicks = 25;
85         }
86         
87         SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); //$NON-NLS-1$
88
SearchParticipant[] participants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
89         MethodCollector collector = new MethodCollector();
90         IProgressMonitor searchMonitor = new SubProgressMonitor(pm, searchTicks);
91         try {
92             new SearchEngine().search(pattern, participants, scope, collector, searchMonitor);
93         } catch (CoreException ce) {
94             JDIDebugUIPlugin.log(ce);
95         }
96
97         List JavaDoc result = collector.getResult();
98         if (includeSubtypes) {
99             IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, 75);
100             subtypesMonitor.beginTask(LauncherMessages.MainMethodSearchEngine_2, result.size());
101             Set JavaDoc set = addSubtypes(result, subtypesMonitor, scope);
102             return (IType[]) set.toArray(new IType[set.size()]);
103         }
104         return (IType[]) result.toArray(new IType[result.size()]);
105     }
106
107     /**
108      * Adds subtypes and enclosed types to the listing of 'found' types
109      * @param types the list of found types thus far
110      * @param monitor progress monitor
111      * @param scope the scope of elements
112      * @return as set of all types to consider
113      */

114     private Set JavaDoc addSubtypes(List JavaDoc types, IProgressMonitor monitor, IJavaSearchScope scope) {
115         Iterator JavaDoc iterator = types.iterator();
116         Set JavaDoc result = new HashSet JavaDoc(types.size());
117         IType type = null;
118         ITypeHierarchy hierarchy = null;
119         IType[] subtypes = null;
120         while (iterator.hasNext()) {
121             type = (IType) iterator.next();
122             if (result.add(type)) {
123                 try {
124                     hierarchy = type.newTypeHierarchy(monitor);
125                     subtypes = hierarchy.getAllSubtypes(type);
126                     for (int i = 0; i < subtypes.length; i++) {
127                         if (scope.encloses(subtypes[i])) {
128                             result.add(subtypes[i]);
129                         }
130                     }
131                 }
132                 catch (JavaModelException e) {JDIDebugUIPlugin.log(e);}
133             }
134             monitor.worked(1);
135         }
136         return result;
137     }
138     
139     
140     /**
141      * Returns the package fragment root of <code>IJavaElement</code>. If the given
142      * element is already a package fragment root, the element itself is returned.
143      */

144     public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) {
145         return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
146     }
147     
148     /**
149      * Searches for all main methods in the given scope.
150      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
151      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
152      *
153      * @param includeSubtypes whether to consider types that inherit a main method
154      */

155     public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final boolean includeSubtypes) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
156         final IType[][] res= new IType[1][];
157         
158         IRunnableWithProgress runnable= new IRunnableWithProgress() {
159             public void run(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
160                 res[0]= searchMainMethods(pm, scope, includeSubtypes);
161             }
162         };
163         context.run(true, true, runnable);
164         
165         return res[0];
166     }
167             
168 }
169
Popular Tags