1 8 package com.mountainminds.eclemma.internal.core.launching; 9 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 import java.util.List ; 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 35 public class InstrumentedClasspathProvider implements IRuntimeClasspathProvider { 36 37 public static final String ID = "com.mountainminds.eclemma.core.instrumentedClasspathProvider"; 39 private static final ITracer TRACER = DebugOptions.LAUNCHINGTRACER; 40 41 private static final ThreadLocal originalClasspathProvider = new ThreadLocal (); 42 43 private static final ThreadLocal launchInfo = new ThreadLocal (); 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 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 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 76 public IRuntimeClasspathEntry[] computeUnresolvedClasspath( 77 ILaunchConfiguration configuration) throws CoreException { 78 IRuntimeClasspathEntry[] entries = getOriginalClasspathProvider().computeUnresolvedClasspath( 79 configuration); 80 TRACER.trace("computeUnresolvedClasspath() -> {0}", Arrays.asList(entries)); return entries; 82 } 83 84 public IRuntimeClasspathEntry[] resolveClasspath( 85 IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) 86 throws CoreException { 87 TRACER.trace("resolveClasspath()"); ICoverageLaunchInfo info = getLaunchInfo(); 89 entries = getOriginalClasspathProvider().resolveClasspath(entries, configuration); 90 List newentries = new ArrayList (); 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()); IInstrumentation instr = info.getInstrumentation(entries[i].getLocation()); 96 if (instr != null) { 97 TRACER.trace("Found instrumented classes for {0}", entries[i].getLocation()); 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 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 |