1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 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 fResult; 47 private int fStyle; 48 49 public MethodCollector(int style) { 50 fResult = new ArrayList (200); 51 fStyle= style; 52 } 53 54 public List 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 69 public void acceptSearchMatch(SearchMatch match) throws CoreException { 70 Object enclosingElement = match.getElement(); 71 if (enclosingElement instanceof IMethod) { 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 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); 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 result = collector.getResult(); 122 if (includeSubtypes) { 123 IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, 75); 124 subtypesMonitor.beginTask(PDEUIMessages.MainMethodSearchEngine_search, result.size()); 125 Set 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 addSubtypes(List types, IProgressMonitor monitor, IJavaSearchScope scope) { 132 Iterator iterator = types.iterator(); 133 Set result = new HashSet (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 161 public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) { 162 return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 163 } 164 165 172 public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final int style, final boolean includeSubtypes) throws InvocationTargetException , InterruptedException { 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 { 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 |