| 1 | |
package org.webslinger.instrumentation; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.util.Collections; |
| 6 | |
|
| 7 | |
import org.objectweb.asm.ClassReader; |
| 8 | |
import org.objectweb.asm.ClassWriter; |
| 9 | |
|
| 10 | |
import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler; |
| 11 | |
import net.sourceforge.cobertura.coveragedata.ProjectData; |
| 12 | |
import net.sourceforge.cobertura.instrument.ClassInstrumenter; |
| 13 | |
|
| 14 | |
import org.webslinger.launcher.Instrumenter; |
| 15 | |
|
| 16 | 27 | public final class CoberturaInstrumenter implements Instrumenter { |
| 17 | |
protected File dataFile; |
| 18 | |
protected ProjectData projectData; |
| 19 | |
protected boolean forInstrumenting; |
| 20 | |
|
| 21 | |
public File getDefaultFile() throws IOException { |
| 22 | 0 | return CoverageDataFileHandler.getDefaultDataFile(); |
| 23 | |
} |
| 24 | |
|
| 25 | |
public void open(File dataFile, boolean forInstrumenting) throws IOException { |
| 26 | 1 | System.setProperty("net.sourceforge.cobertura.datafile", dataFile.toString()); |
| 27 | 1 | this.forInstrumenting = forInstrumenting; |
| 28 | 1 | this.dataFile = dataFile; |
| 29 | 1 | if (forInstrumenting) { |
| 30 | 0 | if (dataFile.exists()) { |
| 31 | 0 | projectData = CoverageDataFileHandler.loadCoverageData(dataFile); |
| 32 | |
} else { |
| 33 | 0 | projectData = new ProjectData(); |
| 34 | |
} |
| 35 | |
} |
| 36 | 1 | } |
| 37 | |
|
| 38 | |
public void close() throws IOException { |
| 39 | 0 | if (forInstrumenting) { |
| 40 | 0 | CoverageDataFileHandler.saveCoverageData(projectData, dataFile); |
| 41 | |
} |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
public byte[] instrumentClass(byte[] bytes) throws IOException { |
| 45 | 60 | ClassReader cr = new ClassReader(bytes); |
| 46 | 60 | ClassWriter cw = new ClassWriter(true); |
| 47 | 60 | ClassInstrumenter ci = new ClassInstrumenter(projectData != null ? projectData : ProjectData.getGlobalProjectData(), cw, Collections.EMPTY_LIST); |
| 48 | 60 | cr.accept(ci, false); |
| 49 | 60 | if (ci.isInstrumented()) return cw.toByteArray(); |
| 50 | 0 | return bytes; |
| 51 | |
} |
| 52 | |
} |