1 11 package org.eclipse.jdt.internal.debug.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 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 fResult; 43 44 public MethodCollector() { 45 fResult = new ArrayList (200); 46 } 47 48 public List getResult() { 49 return fResult; 50 } 51 52 55 public void acceptSearchMatch(SearchMatch match) throws CoreException { 56 Object enclosingElement = match.getElement(); 57 if (enclosingElement instanceof IMethod) { 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 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); 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 result = collector.getResult(); 98 if (includeSubtypes) { 99 IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, 75); 100 subtypesMonitor.beginTask(LauncherMessages.MainMethodSearchEngine_2, result.size()); 101 Set 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 114 private Set addSubtypes(List types, IProgressMonitor monitor, IJavaSearchScope scope) { 115 Iterator iterator = types.iterator(); 116 Set result = new HashSet (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 144 public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) { 145 return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 146 } 147 148 155 public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope, final boolean includeSubtypes) throws InvocationTargetException , InterruptedException { 156 final IType[][] res= new IType[1][]; 157 158 IRunnableWithProgress runnable= new IRunnableWithProgress() { 159 public void run(IProgressMonitor pm) throws InvocationTargetException { 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 |