Package: AcceleoMainMethodSearchEngine$1

AcceleoMainMethodSearchEngine$1

nameinstructionbranchcomplexitylinemethod
run(IProgressMonitor)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

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: *         Obeo - Tweaking for the needs of Acceleo
11: *******************************************************************************/
12: package org.eclipse.acceleo.internal.ide.ui.launching;
13:
14: import java.lang.reflect.InvocationTargetException;
15: import java.util.ArrayList;
16: import java.util.Iterator;
17: import java.util.List;
18: import java.util.Set;
19:
20: import org.eclipse.acceleo.common.utils.CompactHashSet;
21: import org.eclipse.acceleo.ide.ui.AcceleoUIActivator;
22: import org.eclipse.acceleo.internal.ide.ui.AcceleoUIMessages;
23: import org.eclipse.core.runtime.CoreException;
24: import org.eclipse.core.runtime.IProgressMonitor;
25: import org.eclipse.core.runtime.SubProgressMonitor;
26: import org.eclipse.jdt.core.IJavaElement;
27: import org.eclipse.jdt.core.IMethod;
28: import org.eclipse.jdt.core.IPackageFragmentRoot;
29: import org.eclipse.jdt.core.IType;
30: import org.eclipse.jdt.core.ITypeHierarchy;
31: import org.eclipse.jdt.core.JavaModelException;
32: import org.eclipse.jdt.core.search.IJavaSearchConstants;
33: import org.eclipse.jdt.core.search.IJavaSearchScope;
34: import org.eclipse.jdt.core.search.SearchEngine;
35: import org.eclipse.jdt.core.search.SearchMatch;
36: import org.eclipse.jdt.core.search.SearchParticipant;
37: import org.eclipse.jdt.core.search.SearchPattern;
38: import org.eclipse.jdt.core.search.SearchRequestor;
39: import org.eclipse.jface.operation.IRunnableContext;
40: import org.eclipse.jface.operation.IRunnableWithProgress;
41:
42: /**
43: * Gets the main method of the Java class representing the main template. This is inspired from the
44: * 'MainMethodSearchEngine.java' file of the JDT.
45: *
46: * @author <a href="mailto:jonathan.musset@obeo.fr">Jonathan Musset</a>
47: */
48: public class AcceleoMainMethodSearchEngine {
49:
50:         /**
51:          * Collects the methods from a search engine query.
52:          *
53:          * @author <a href="mailto:jonathan.musset@obeo.fr">Jonathan Musset</a>
54:          */
55:         private class MethodCollector extends SearchRequestor {
56:
57:                 /**
58:                  * The result.
59:                  */
60:                 private List<IType> fResult;
61:
62:                 /**
63:                  * Constructor.
64:                  */
65:                 public MethodCollector() {
66:                         final int size = 200;
67:                         fResult = new ArrayList<IType>(size);
68:                 }
69:
70:                 /**
71:                  * Gets the result.
72:                  *
73:                  * @return the result
74:                  */
75:                 public List<IType> getResult() {
76:                         return fResult;
77:                 }
78:
79:                 /**
80:                  * {@inheritDoc}
81:                  *
82:                  * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
83:                  */
84:                 @Override
85:                 public void acceptSearchMatch(SearchMatch match) throws CoreException {
86:                         Object enclosingElement = match.getElement();
87:                         if (enclosingElement instanceof IMethod) { // defensive code
88:                                 try {
89:                                         IMethod curr = (IMethod)enclosingElement;
90:                                         if (curr.isMainMethod()) {
91:                                                 IType declaringType = curr.getDeclaringType();
92:                                                 fResult.add(declaringType);
93:                                         }
94:                                 } catch (JavaModelException e) {
95:                                         AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
96:                                 }
97:                         }
98:                 }
99:         }
100:
101:         /**
102:          * Searches for all main methods in the given scope. Valid styles are
103:          * IJavaElementSearchConstants.CONSIDER_BINARIES and IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
104:          *
105:          * @param pm
106:          * progress monitor
107:          * @param scope
108:          * search scope
109:          * @param includeSubtypes
110:          * whether to consider types that inherit a main method
111:          * @return main methods types
112:          */
113:         public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, boolean includeSubtypes) {
114:                 final int v100 = 100;
115:                 final int v25 = 25;
116:                 final int v75 = 75;
117:                 pm.beginTask(AcceleoUIMessages.getString("AcceleoMainMethodSearchEngine.SearchingForMainTypes"), 100); //$NON-NLS-1$
118:                 int searchTicks = v100;
119:                 if (includeSubtypes) {
120:                         searchTicks = v25;
121:                 }
122:
123:                 SearchPattern pattern = SearchPattern
124:                                 .createPattern(
125:                                                 "main(String[]) void", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); //$NON-NLS-1$
126:                 SearchParticipant[] participants = new SearchParticipant[] {SearchEngine
127:                                 .getDefaultSearchParticipant(), };
128:                 MethodCollector collector = new MethodCollector();
129:                 IProgressMonitor searchMonitor = new SubProgressMonitor(pm, searchTicks);
130:                 try {
131:                         new SearchEngine().search(pattern, participants, scope, collector, searchMonitor);
132:                 } catch (CoreException e) {
133:                         AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
134:                 }
135:
136:                 List<IType> result = collector.getResult();
137:                 if (includeSubtypes) {
138:                         IProgressMonitor subtypesMonitor = new SubProgressMonitor(pm, v75);
139:                         subtypesMonitor.beginTask(AcceleoUIMessages
140:                                         .getString("AcceleoMainMethodSearchEngine.SearchingForMainTypes"), result.size()); //$NON-NLS-1$
141:                         Set<IType> set = addSubtypes(result, subtypesMonitor, scope);
142:                         return set.toArray(new IType[set.size()]);
143:                 }
144:                 return result.toArray(new IType[result.size()]);
145:         }
146:
147:         /**
148:          * Adds sub-types and enclosed types to the listing of 'found' types.
149:          *
150:          * @param types
151:          * the list of found types thus far
152:          * @param monitor
153:          * progress monitor
154:          * @param scope
155:          * the scope of elements
156:          * @return as set of all types to consider
157:          */
158:         private Set<IType> addSubtypes(List<IType> types, IProgressMonitor monitor, IJavaSearchScope scope) {
159:                 Iterator<IType> iterator = types.iterator();
160:                 Set<IType> result = new CompactHashSet<IType>(types.size());
161:                 IType type = null;
162:                 ITypeHierarchy hierarchy = null;
163:                 IType[] subtypes = null;
164:                 while (iterator.hasNext()) {
165:                         type = iterator.next();
166:                         if (result.add(type)) {
167:                                 try {
168:                                         hierarchy = type.newTypeHierarchy(monitor);
169:                                         subtypes = hierarchy.getAllSubtypes(type);
170:                                         for (int i = 0; i < subtypes.length; i++) {
171:                                                 if (scope.encloses(subtypes[i])) {
172:                                                         result.add(subtypes[i]);
173:                                                 }
174:                                         }
175:                                 } catch (JavaModelException e) {
176:                                         AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
177:                                 }
178:                         }
179:                         monitor.worked(1);
180:                 }
181:                 return result;
182:         }
183:
184:         /**
185:          * Returns the package fragment root of <code>IJavaElement</code>. If the given element is already a
186:          * package fragment root, the element itself is returned.
187:          *
188:          * @param element
189:          * is the Java element
190:          * @return the package fragment root
191:          */
192:         public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) {
193:                 return (IPackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
194:         }
195:
196:         /**
197:          * Searches for all main methods in the given scope. Valid styles are
198:          * IJavaElementSearchConstants.CONSIDER_BINARIES and IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS
199:          *
200:          * @param context
201:          * is the context
202:          * @param scope
203:          * the scope of elements
204:          * @param includeSubtypes
205:          * whether to consider types that inherit a main method
206:          * @return all main methods
207:          * @throws InvocationTargetException
208:          * invocation target exception
209:          * @throws InterruptedException
210:          * interrupted exception
211:          */
212:         public IType[] searchMainMethods(IRunnableContext context, final IJavaSearchScope scope,
213:                         final boolean includeSubtypes) throws InvocationTargetException, InterruptedException {
214:                 final IType[][] res = new IType[1][];
215:                 IRunnableWithProgress runnable = new IRunnableWithProgress() {
216:                         public void run(IProgressMonitor pm) throws InvocationTargetException {
217:                                 res[0] = searchMainMethods(pm, scope, includeSubtypes);
218:                         }
219:                 };
220:                 context.run(true, true, runnable);
221:                 return res[0];
222:         }
223:
224: }