1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 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 fResult; 46 private int fStyle; 47 48 public MethodCollector(List 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 65 public void acceptSearchMatch(SearchMatch match) throws CoreException { 66 Object enclosingElement= match.getElement(); 67 if (enclosingElement instanceof IMethod) { 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 94 public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException { 95 List typesFound= new ArrayList (200); 96 97 SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", 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 112 public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final int style) throws InvocationTargetException , InterruptedException { 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 { 120 try { 121 res[0]= searchMainMethods(pm, scope, style); 122 } catch (CoreException e) { 123 throw new InvocationTargetException (e); 124 } 125 } 126 }; 127 context.run(true, true, runnable); 128 129 return res[0]; 130 } 131 132 } 133 | Popular Tags |