/
LaunchInstrumenter.scala
112 lines (92 loc) · 4.17 KB
/
LaunchInstrumenter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* This file is part of eCobertura.
*
* Copyright (c) 2009, 2010 Joachim Hofer
* All rights reserved.
*
* eCobertura is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eCobertura is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with eCobertura. If not, see <http://www.gnu.org/licenses/>.
*/
package ecobertura.core.launching
import java.io.File
import java.util.logging.Logger
import org.eclipse.core.runtime.CoreException
import org.eclipse.debug.core.ILaunchConfiguration
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy
import org.eclipse.jdt.launching._
import _root_.ecobertura.core.CorePlugin;
import _root_.ecobertura.core.cobertura.CoberturaWrapper;
object LaunchInstrumenter {
val COBERTURA_DATAFILE_PROPERTY = "net.sourceforge.cobertura.datafile"
private val logger = Logger.getLogger("ecobertura.core.launching")
def instrumentClassesFor(configuration: ILaunchConfiguration) =
new LaunchInstrumenter(configuration)
}
class LaunchInstrumenter(configuration: ILaunchConfiguration) {
import LaunchInstrumenter._
private val configWC = configuration.getWorkingCopy
CoberturaWrapper.get.resetProjectData
instrumentClasspath
CoberturaWrapper.get.saveProjectDataToDefaultFile
addCoberturaAndCoveredClassesToClasspath
addDatafileSystemProperty
private def instrumentClasspath = {
def resolvedClasspathEntries = {
val unresolvedClasspathEntries =
JavaRuntime.computeUnresolvedRuntimeClasspath(configuration)
val resolvedClasspathEntries =
JavaRuntime.resolveRuntimeClasspath(unresolvedClasspathEntries, configuration)
resolvedClasspathEntries
}
resolvedClasspathEntries foreach (classpathEntry => {
def containsUserClassesFromProject(entry: IRuntimeClasspathEntry) = {
entry.getClasspathProperty == IRuntimeClasspathEntry.USER_CLASSES &&
entry.getType == IRuntimeClasspathEntry.PROJECT
}
def instrumentFilesWithin(file: File) : Unit = {
def instrumentClassFile(file: File) = {
logger.fine(String.format("instrumenting %s", file.getPath()))
CoberturaWrapper.get.instrumentClassFile(file)
}
if (file.isDirectory) {
file.listFiles foreach (instrumentFilesWithin(_))
} else instrumentClassFile(file)
}
if (containsUserClassesFromProject(classpathEntry)) {
val userClasspath = classpathEntry.getLocation
logger.fine(String format ("instrumenting classes within %s", userClasspath))
CorePlugin.instance.pluginState.copyClassesFrom(new File(userClasspath))
instrumentFilesWithin(CorePlugin.instance.pluginState.instrumentedClassesDirectory)
} else logger.fine(String.format("skipping %s", classpathEntry.getLocation))
})
}
private def addCoberturaAndCoveredClassesToClasspath = {
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER,
CoverageClasspathProvider.ID)
CoverageClasspathProvider.wrap(JavaRuntime.getClasspathProvider(configuration))
}
private def addDatafileSystemProperty = {
def requiresCoberturaVMArgument(currentVMArguments: String, coberturaVMArgument: String) =
(currentVMArguments.indexOf(coberturaVMArgument)) == -1
val coberturaFile = new File(CorePlugin.instance.pluginState.instrumentationDataDirectory,
CoberturaWrapper.DEFAULT_COBERTURA_FILENAME)
val coberturaVMArgument = String.format("-D%s=%s ", COBERTURA_DATAFILE_PROPERTY,
coberturaFile.getAbsolutePath)
val currentVMArguments = configWC.getAttribute(
IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "")
if (requiresCoberturaVMArgument(currentVMArguments, coberturaVMArgument))
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
String.format("%s %s ", currentVMArguments, coberturaVMArgument))
}
def getUpdatedLaunchConfiguration = configWC
}