KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > core > launching > InstrumentedClasspathProvider


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: InstrumentedClasspathProvider.java 15 2006-08-28 20:31:45Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.core.launching;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
18 import org.eclipse.jdt.launching.IRuntimeClasspathProvider;
19 import org.eclipse.jdt.launching.JavaRuntime;
20
21 import com.mountainminds.eclemma.core.CoverageTools;
22 import com.mountainminds.eclemma.core.EclEmmaStatus;
23 import com.mountainminds.eclemma.core.IInstrumentation;
24 import com.mountainminds.eclemma.core.launching.ICoverageLaunchInfo;
25 import com.mountainminds.eclemma.internal.core.DebugOptions;
26 import com.mountainminds.eclemma.internal.core.DebugOptions.ITracer;
27
28 /**
29  * Class path provider used internally to inject instrumented classes and the
30  * Emma runtime.
31  *
32  * @author Marc R. Hoffmann
33  * @version $Revision: 15 $
34  */

35 public class InstrumentedClasspathProvider implements IRuntimeClasspathProvider {
36
37   public static final String JavaDoc ID = "com.mountainminds.eclemma.core.instrumentedClasspathProvider"; //$NON-NLS-1$
38

39   private static final ITracer TRACER = DebugOptions.LAUNCHINGTRACER;
40
41   private static final ThreadLocal JavaDoc originalClasspathProvider = new ThreadLocal JavaDoc();
42
43   private static final ThreadLocal JavaDoc launchInfo = new ThreadLocal JavaDoc();
44
45   public static void enable(
46       IRuntimeClasspathProvider originalClasspathProvider,
47       ICoverageLaunchInfo launchInfo) {
48     InstrumentedClasspathProvider.originalClasspathProvider.set(originalClasspathProvider);
49     InstrumentedClasspathProvider.launchInfo.set(launchInfo);
50   }
51
52   public static void disable() {
53     InstrumentedClasspathProvider.originalClasspathProvider.set(null);
54     InstrumentedClasspathProvider.launchInfo.set(null);
55   }
56
57   private static IRuntimeClasspathProvider getOriginalClasspathProvider()
58       throws CoreException {
59     Object JavaDoc obj = originalClasspathProvider.get();
60     if (obj == null) {
61       throw new CoreException(EclEmmaStatus.INVALID_CLASSPATH_PROVIDER_CONTEXT_ERROR.getStatus(null));
62     }
63     return (IRuntimeClasspathProvider) obj;
64   }
65
66   private static ICoverageLaunchInfo getLaunchInfo() throws CoreException {
67     Object JavaDoc obj = launchInfo.get();
68     if (obj == null) {
69       throw new CoreException(EclEmmaStatus.INVALID_CLASSPATH_PROVIDER_CONTEXT_ERROR.getStatus(null));
70     }
71     return (ICoverageLaunchInfo) obj;
72   }
73
74   // IRuntimeClasspathProvider implementation
75

76   public IRuntimeClasspathEntry[] computeUnresolvedClasspath(
77       ILaunchConfiguration configuration) throws CoreException {
78     IRuntimeClasspathEntry[] entries = getOriginalClasspathProvider().computeUnresolvedClasspath(
79         configuration);
80     TRACER.trace("computeUnresolvedClasspath() -> {0}", Arrays.asList(entries)); //$NON-NLS-1$
81
return entries;
82   }
83
84   public IRuntimeClasspathEntry[] resolveClasspath(
85       IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration)
86       throws CoreException {
87     TRACER.trace("resolveClasspath()"); //$NON-NLS-1$
88
ICoverageLaunchInfo info = getLaunchInfo();
89     entries = getOriginalClasspathProvider().resolveClasspath(entries, configuration);
90     List JavaDoc newentries = new ArrayList JavaDoc();
91     boolean emmartinserted = false;
92     for (int i = 0; i < entries.length; i++) {
93       if (entries[i].getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) {
94         TRACER.trace("Resolved classpath entry: {0}", entries[i].getLocation()); //$NON-NLS-1$
95
IInstrumentation instr = info.getInstrumentation(entries[i].getLocation());
96         if (instr != null) {
97           TRACER.trace("Found instrumented classes for {0}", entries[i].getLocation()); //$NON-NLS-1$
98
if (!emmartinserted) {
99             addEmmaRuntime(info, newentries);
100             emmartinserted = true;
101           }
102           if (!instr.isInplace()) {
103             newentries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(instr.getOutputLocation()));
104           }
105         }
106       }
107       newentries.add(entries[i]);
108     }
109     IRuntimeClasspathEntry[] arr = new IRuntimeClasspathEntry[newentries.size()];
110     return (IRuntimeClasspathEntry[]) newentries.toArray(arr);
111   }
112   
113   protected void addEmmaRuntime(ICoverageLaunchInfo info, List JavaDoc entries) throws CoreException {
114     IPath propertiesjarpath = info.getPropertiesJARFile();
115     entries.add(0, JavaRuntime.newArchiveRuntimeClasspathEntry(propertiesjarpath));
116     entries.add(0, JavaRuntime.newArchiveRuntimeClasspathEntry(CoverageTools.getEmmaJar()));
117   }
118
119 }
120
Popular Tags