我使用新的cobertura (2.0.2..)获得以下异常。我想这与新块中的新对象创建有关。
WARN instrumentClass, Unable to instrument file c:\apps\ijprojects\TrickyInstrument\out\production\TrickyInstrument\InstrumentationFailsOnFirstNewClassInTryBlock.class
java.lang.RuntimeException: java.lang.ClassNotFoundException: DataAccess
at org.objectweb.asm.ClassWriter.getCommonSuperClass(Unknown Source)
at org.objectweb.asm.ClassWriter.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.Frame.a(Unknown Source)
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitMaxs(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitMaxs(Unknown Source)
at org.objectweb.asm.commons.LocalVariablesSorter.visitMaxs(Unknown Source)
at org.objectweb.asm.tree.MethodNode.accept(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter$1.visitEnd(Unknown Source)
at org.objectweb.asm.MethodVisitor.visitEnd(Unknown Source)
at org.objectweb.asm.util.CheckMethodAdapter.visitEnd(Unknown Source)
at org.objectweb.asm.ClassReader.b(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at org.objectweb.asm.ClassReader.accept(Unknown Source)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.instrumentClass(CoberturaInstrumenter.java:204)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.instrumentClass(CoberturaInstrumenter.java:121)
at net.sourceforge.cobertura.instrument.CoberturaInstrumenter.addInstrumentationToSingleClass(CoberturaInstrumenter.java:233)
at net.sourceforge.cobertura.instrument.Main.addInstrumentationToSingleClass(Main.java:274)
at net.sourceforge.cobertura.instrument.Main.addInstrumentation(Main.java:283)
at net.sourceforge.cobertura.instrument.Main.addInstrumentation(Main.java:292)
at net.sourceforge.cobertura.instrument.Main.parseArguments(Main.java:373)
at net.sourceforge.cobertura.instrument.Main.main(Main.java:395)
8 Jul, 2013 2:05:07 PM net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler saveCoverageData
INFO: Cobertura: Saved information on 2 classes.
以下是与上述异常相关的代码。
public class InstrumentationFailsOnFirstNewClassInTryBlock {
public void saveToDatabase() {
//
try {
// boolean b=false;
// if ( b) {
// System.out.println("no action");
// }
DataAccess da = new DataAccess();
System.out.println("nothing");
} catch (Exception e) {
}
}
}
class DataAccess {
public DataAccess() {
//To change body of created methods use File | Settings | File Templates.
}
}
如果我取消注释代码阻止一些虚拟语句,那么检测工作正常。有谁见过这个?任何可能的修复?
编辑:java6和java7发生错误。
答案 0 :(得分:2)
原始问题是由于Cobertura缺陷造成的。它不是固定的。 Cobertura现在支持auxillary classpath.的另一个论点。这将用于解决检测所需的任何类。
cobertura-ant task documentation
添加auxClasspath
auxClasspath参数旨在删除ClassNotFoundException 仪器仪表期间。看到 https://github.com/cobertura/cobertura/wiki/FAQ#classnotfoundexception-during-instrumentation 有关此论点的更多信息
答案 1 :(得分:1)
我有类似的问题,可能是个错误,请参阅:https://github.com/cobertura/cobertura/issues/49
您的测试用例可能对调试问题很有用......
答案 2 :(得分:1)
来自https://github.com/cobertura/cobertura/wiki/FAQ#classnotfoundexception-during-instrumentation:
"这是因为在cobertura 2.0中的检测期间,我们使用ASM来重建.class文件。我们重建了stackmap,这是与java 7以及之后的任何东西兼容的要求。这并不意味着我们重新编译代码,但ASM要求我们提供其他类的二进制文件,以防它需要查找任何超级方法。为了解决这个问题,我们使用了一个名为auxClasspath的参数。"
将以下代码添加到ant文件(build.xml)应解决此问题。
<path id="cobertura.auxpath">
<pathelement location="${bin}"/>
</path>
<target name="instrument_coverage" depends="init_coverage"
description="Instruments source code for coverage measurement">
<cobertura-instrument datafile="${coverage.datafile}">
<fileset refid="coverage-files"/>
<auxClasspath>
<path refid="cobertura.auxpath" />
</auxClasspath>
</cobertura-instrument>
</target>
这对我有用。