diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2012-06-30 00:15:43 +0000 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2012-06-30 00:15:43 +0000 |
commit | cf41fc18ccb6773f10077dfb460d2c6b84f78858 (patch) | |
tree | d0c7b1bb4f989fbc2bb4a3a763dd532c255b3e7a /org.jacoco.agent.rt | |
parent | 0509ad32ba6a30654149b34d4dbd8e4a3bc6aae2 (diff) | |
download | jacoco-cf41fc18ccb6773f10077dfb460d2c6b84f78858.tar.gz |
Trac #208: New option classdumpdir for JaCoCo agent
Diffstat (limited to 'org.jacoco.agent.rt')
-rw-r--r-- | org.jacoco.agent.rt/src/org/jacoco/agent/rt/ClassFileDumper.java | 72 | ||||
-rw-r--r-- | org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java | 4 |
2 files changed, 76 insertions, 0 deletions
diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ClassFileDumper.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ClassFileDumper.java new file mode 100644 index 00000000..9749ae1d --- /dev/null +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/ClassFileDumper.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * Internal dumper for class files. + */ +class ClassFileDumper { + + private final File location; + + /** + * Create a new dumper for the given location. + * + * @param location + * relative path to dump directory. <code>null</code> if no dumps + * should be written + */ + ClassFileDumper(final String location) { + if (location == null) { + this.location = null; + } else { + this.location = new File(location); + } + } + + /** + * Dumps the given binary content under the given name if a non- + * <code>null</code> location has been specified. + * + * @param name + * qualified class name in VM notation + * @param contents + * binary contents + * @throws IOException + * in case of problems while dumping the file + */ + void dump(final String name, final byte[] contents) throws IOException { + if (location != null) { + final File outputdir; + final String localname; + final int pkgpos = name.lastIndexOf('/'); + if (pkgpos != -1) { + outputdir = new File(location, name.substring(0, pkgpos)); + localname = name.substring(pkgpos + 1); + } else { + outputdir = location; + localname = name; + } + outputdir.mkdirs(); + final File file = new File(outputdir, localname + ".class"); + final OutputStream out = new FileOutputStream(file); + out.write(contents); + out.close(); + } + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java index 01cb69f8..a0b32907 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/CoverageTransformer.java @@ -46,6 +46,8 @@ public class CoverageTransformer implements ClassFileTransformer { private final WildcardMatcher exclClassloader;
+ private final ClassFileDumper classFileDumper;
+
/**
* New transformer with the given delegates.
*
@@ -68,6 +70,7 @@ public class CoverageTransformer implements ClassFileTransformer { toWildcard(toVMName(options.getExcludes())));
exclClassloader = new WildcardMatcher(
toWildcard(options.getExclClassloader()));
+ classFileDumper = new ClassFileDumper(options.getClassDumpDir());
}
public byte[] transform(final ClassLoader loader, final String classname,
@@ -80,6 +83,7 @@ public class CoverageTransformer implements ClassFileTransformer { }
try {
+ classFileDumper.dump(classname, classfileBuffer);
if (classBeingRedefined != null) {
// For redefined classes we must clear the execution data
// reference as probes might have changed.
|