KickJava   Java API By Example, From Geeks To Geeks.

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

65             public void acceptSearchMatch(SearchMatch match) throws CoreException {
66                 Object JavaDoc enclosingElement= match.getElement();
67                 if (enclosingElement instanceof IMethod) { // defensive code
68
try {
69                         IMethod curr= (IMethod) enclosingElement;
70                         if (curr.isMainMethod()) {
71                             if (!considerExternalJars()) {
72                                 IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(curr);
73                                 if (root == null || root.isArchive()) {
74                                     return;
75                                 }
76                             }
77                             if (!considerBinaries() && curr.isBinary()) {
78                                 return;
79                             }
80                             fResult.add(curr.getDeclaringType());
81                         }
82                     } catch (JavaModelException e) {
83                         JavaPlugin.log(e.getStatus());
84                     }
85                 }
86             }
87     }
88
89     /**
90      * Searches for all main methods in the given scope.
91      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
92      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
93      */

94     public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException {
95         List JavaDoc typesFound= new ArrayList JavaDoc(200);
96         
97         SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", //$NON-NLS-1$
98
IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
99         SearchRequestor requestor= new MethodCollector(typesFound, style);
100         new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
101             
102         return (IType[]) typesFound.toArray(new IType[typesFound.size()]);
103     }
104     
105     
106     
107     /**
108      * Searches for all main methods in the given scope.
109      * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and
110      * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
111      */

112     public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final int style) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
113         int allFlags= IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS | IJavaElementSearchConstants.CONSIDER_BINARIES;
114         Assert.isTrue((style | allFlags) == allFlags);
115         
116         final IType[][] res= new IType[1][];
117         
118         IRunnableWithProgress runnable= new IRunnableWithProgress() {
119             public void run(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
120                 try {
121                     res[0]= searchMainMethods(pm, scope, style);
122                 } catch (CoreException e) {
123                     throw new InvocationTargetException JavaDoc(e);
124                 }
125             }
126         };
127         context.run(true, true, runnable);
128         
129         return res[0];
130     }
131             
132 }
133
Popular Tags