KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > core > CoverageTools


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: CoverageTools.java 370 2007-07-18 11:14:27Z mtnminds $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.core;
9
10 import java.io.IOException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22
23 import com.mountainminds.eclemma.core.analysis.IJavaCoverageListener;
24 import com.mountainminds.eclemma.core.analysis.IJavaElementCoverage;
25 import com.mountainminds.eclemma.core.analysis.IJavaModelCoverage;
26 import com.mountainminds.eclemma.core.analysis.ILineCoverage;
27 import com.mountainminds.eclemma.core.launching.ICoverageLaunchConfigurationConstants;
28 import com.mountainminds.eclemma.core.launching.ICoverageLaunchInfo;
29 import com.mountainminds.eclemma.core.launching.ICoverageLauncher;
30 import com.mountainminds.eclemma.internal.core.CoverageSession;
31 import com.mountainminds.eclemma.internal.core.EclEmmaCorePlugin;
32 import com.mountainminds.eclemma.internal.core.SessionExporter;
33 import com.mountainminds.eclemma.internal.core.SessionImporter;
34 import com.mountainminds.eclemma.internal.core.launching.CoverageLaunchInfo;
35
36 /**
37  * For central access to the tools provided by the coverage core plugin this
38  * class offers several static methods.
39  *
40  * @author Marc R. Hoffmann
41  * @version $Revision: 370 $
42  */

43 public final class CoverageTools {
44   
45   /**
46    * The launch mode used for coverage sessions.
47    */

48   public static final String JavaDoc LAUNCH_MODE = "coverage"; //$NON-NLS-1$
49

50   public static final int EXPORT_HTML = 0;
51   public static final int EXPORT_XML = 1;
52   public static final int EXPORT_TEXT = 2;
53   public static final int EXPORT_ES = 3;
54
55   /**
56    * Returns the global session manager.
57    *
58    * @return global session manager
59    */

60   public static ISessionManager getSessionManager() {
61     return EclEmmaCorePlugin.getInstance().getSessionManager();
62   }
63
64   /**
65    * Convenience method that tries to adapt the given object to
66    * IJavaElementCoverage, i.e. find coverage information from the active
67    * session.
68    *
69    * @param object
70    * Object to adapt
71    * @return adapter or <code>null</code>
72    */

73   public static IJavaElementCoverage getCoverageInfo(Object JavaDoc object) {
74     if (object instanceof IAdaptable) {
75       return (IJavaElementCoverage) ((IAdaptable) object)
76           .getAdapter(IJavaElementCoverage.class);
77     } else {
78       return null;
79     }
80   }
81
82   /**
83    * Convenience method that tries to adapt the given object to ILineCoverage,
84    * i.e. find line coverage information from the active session.
85    *
86    * @param object
87    * Object to adapt
88    * @return adapter or <code>null</code>
89    */

90   public static ILineCoverage getLineCoverage(Object JavaDoc object) {
91     if (object instanceof IAdaptable) {
92       return (ILineCoverage) ((IAdaptable) object)
93           .getAdapter(ILineCoverage.class);
94     } else {
95       return null;
96     }
97   }
98
99   /**
100    * Returns the coverage launch info that is assoziated with the given launch.
101    * If no info object is assoziated with the given launch <code>null</code>
102    * is returned, i.e. the launch was probably not in coverage mode.
103    *
104    * @param launch
105    * the launch object we need coverage data for
106    * @return the info object or <code>null</code>
107    */

108   public static ICoverageLaunchInfo getLaunchInfo(ILaunch launch) {
109     return CoverageLaunchInfo.getInfo(launch);
110   }
111
112   /**
113    * Returns a local path to the emma.jar runtime archive.
114    *
115    * @return local path to emma.jar
116    * @throws CoreException
117    * if no local version can be created
118    */

119   public static IPath getEmmaJar() throws CoreException {
120     URL JavaDoc url = EclEmmaCorePlugin.getInstance().find(EclEmmaCorePlugin.EMMA_JAR);
121     try {
122       url = Platform.asLocalURL(url);
123     } catch (IOException JavaDoc e) {
124       throw new CoreException(EclEmmaStatus.NO_LOCAL_EMMAJAR_ERROR.getStatus(e));
125     }
126     return new Path(url.getPath());
127   }
128
129   /**
130    * Returns descriptors for all class files in the workspace.
131    *
132    * @return descriptors for all class files in the workspace
133    *
134    * @throws CoreException
135    */

136   public static IClassFiles[] getClassFiles()
137       throws CoreException {
138     List JavaDoc l = new ArrayList JavaDoc(EclEmmaCorePlugin.getInstance()
139         .getClassFiles().values());
140     IClassFiles[] arr = new IClassFiles[l.size()];
141     return (IClassFiles[]) l.toArray(arr);
142   }
143
144   /**
145    * Returns descriptors for class files for the given launch configuration.
146    *
147    * @param configuration
148    * launch configuration to look for class files
149    * @param includebinaries
150    * flag whether binary classpath entries should be included
151    *
152    * @return descriptors for all class files
153    *
154    * @throws CoreException
155    */

156   public static IClassFiles[] getClassFiles(ILaunchConfiguration configuration, boolean includebinaries) throws CoreException {
157     ICoverageLauncher launcher = (ICoverageLauncher)
158         configuration.getType().getDelegate(LAUNCH_MODE);
159     return launcher.getClassFiles(configuration, includebinaries);
160   }
161
162   /**
163    * Returns descriptors for class files for instrumentation as configured by
164    * the given launch configuration.
165    *
166    * @param configuration
167    * launch configuration to look for class files
168    * @param inplace
169    * flag whether instrumentation will happen inplace. In this case
170    * binary libraries will be excluded
171    *
172    * @return descriptors for all class for instrumentation
173    *
174    * @throws CoreException
175    */

176   public static IClassFiles[] getClassFilesForInstrumentation(ILaunchConfiguration configuration, boolean inplace) throws CoreException {
177     IClassFiles[] all = getClassFiles(configuration, !inplace);
178     List JavaDoc filtered = new ArrayList JavaDoc();
179     List JavaDoc selection =
180       configuration.getAttribute(ICoverageLaunchConfigurationConstants.ATTR_INSTRUMENTATION_PATHS, (List JavaDoc) null);
181     for (int i = 0; i < all.length; i++) {
182       if (selection == null) {
183         // by default we select all source based class files
184
if (!all[i].isBinary()) {
185           filtered.add(all[i]);
186         }
187       } else {
188         if (selection.contains(all[i].getLocation().toString())) {
189           filtered.add(all[i]);
190         }
191       }
192     }
193     return (IClassFiles[]) filtered.toArray(new IClassFiles[filtered.size()]);
194   }
195   
196   public static ICoverageSession createCoverageSession(String JavaDoc description,
197       IInstrumentation[] instrumentations, IPath[] coveragedatafiles,
198       ILaunchConfiguration launchconfiguration) {
199     return new CoverageSession(description, instrumentations, coveragedatafiles,
200         launchconfiguration);
201   }
202
203   public static IJavaModelCoverage getJavaModelCoverage() {
204     return EclEmmaCorePlugin.getInstance().getJavaCoverageLoader()
205         .getJavaModelCoverage();
206   }
207   
208   public static void addJavaCoverageListener(IJavaCoverageListener l) {
209     EclEmmaCorePlugin.getInstance().getJavaCoverageLoader().addJavaCoverageListener(l);
210   }
211
212   public static void removeJavaCoverageListener(IJavaCoverageListener l) {
213     EclEmmaCorePlugin.getInstance().getJavaCoverageLoader().removeJavaCoverageListener(l);
214   }
215   
216   public static ISessionExporter getExporter(ICoverageSession session) {
217     return new SessionExporter(session);
218   }
219   
220   public static ISessionImporter getImporter() {
221     return new SessionImporter();
222   }
223   
224   /**
225    * Sets a {@link ICorePreferences} instance which will be used by the EclEmma
226    * core to query preference settings if required.
227    *
228    * @param preferences callback object for preference settings
229    */

230   public static void setPreferences(ICorePreferences preferences) {
231     EclEmmaCorePlugin.getInstance().setPreferences(preferences);
232   }
233   
234 }
235
Popular Tags