Jacoco: Workaround for runtime modified classes

2,155 views
Skip to first unread message

bprakash...@gmail.com

unread,
Jan 30, 2017, 8:50:49 PM1/30/17
to JaCoCo and EclEmma Users
What workarounds exist to deal with runtime-modified classes?

From jacoco (0.7.8) doc " If classes get modified at runtime in your setup there are some workarounds to make JaCoCo work anyways: If you use another Java agent make sure the JaCoCo agent is specified at first in the command line. This way the JaCoCo agent should see the original class files.
Specify the classdumpdir option of the JaCoCo agent and use the dumped classes at report generation. Note that only loaded classes will be dumped, i.e. classes not executed at all will not show-up in your report as not covered. Use offline instrumentation before you run your tests. This way classes get instrumented by JaCoCo before any runtime modification can take place. Note that in this case the report has to be generated with the original classes, not with instrumented ones."

I am using ""Specify the classdumpdir option... " How to use dumped classes for report?
Three problems in doing this (1) 'classDumpDir'/'classDumpDir' of prepare-agent does include this in javaagent, but report goal is unaware of this and (2) report goal has no way of configuring this (3) I know using default might be the best bet, but whats the default location for dumped classes where report looks?

Evgeny Mandrikov

unread,
Jan 30, 2017, 9:33:29 PM1/30/17
to JaCoCo and EclEmma Users, bprakash...@gmail.com, Marc R. Hoffmann
Hi,

"report" goal always looks into "${project.build.testOutputDirectory}" i.e. "target/classes" independently from "classDumpDir" of agent. To be honest I'm surprised that we recommend to use "classDumpDir" in case when classes modified at runtime, I even doubt that it might work due to the way how mapping of classes back to source files is performed. (CC Marc, since you created this page - maybe you can shed more light on this?)

IMO the best simplest workaround - is to use offline instrumentation.

BTW may I ask - what (which tool?) performs this runtime modification? The reason for this question: widely known (might even say that the only one) example - is PowerMock, and even it recommends to use offline instrumentation - see https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo

Hope this helps.

P.S. glad that there are users that carefully read documentation

bprakash...@gmail.com

unread,
Jan 31, 2017, 4:09:30 PM1/31/17
to JaCoCo and EclEmma Users, bprakash...@gmail.com, hoff...@mountainminds.com
Hi Evgeny, firstof thanks so much for looking into this and responding so quickly!.
I am using powermock which does runtime modification. I had considered the offline option as well, and here's the problem I have with it.

I instrument offline and run the testes by amending pom.xml, all runs fine and report shows up. But if I try to do the same through commandline (really helpful for massive projects wehre pom structures are huge and complex) it fails, I run the same phases/goals howerever. Following are mvn commands I am trying and their outcome.

clean org.jacoco:jacoco-maven-plugin:prepare-agent test org.jacoco:jacoco-maven-plugin:report -- Coverage for classes using powermock doesnt showup obviously, class file looked at during report generation are different. If the concept of classdumpdir works, we should be able to use this command itself without the need for offline instrumentation (I guess).

org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:instrument test org.jacoco:jacoco-maven-plugin:restore-instrumented-classes org.jacoco:jacoco-maven-plugin:report --- Again in principle this should work, but I get the same error as above that the class execution data doesnt match?

mvn clean -Djacoco.classDumpDir=${project.build.directory}/classDumpDir org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:instrument test org.jacoco:jacoco-maven-plugin:restore-instrumented-classes org.jacoco:jacoco-maven-plugin:report -- I am trying to combine both by instrumenting and using the instrumented class before test for report, not sure how to instruct report to look at them. Its not possible to copy classes from classdumpdir into target/classes because the former doesnt has class files of the original classes (ie the classes under test).

Please let me know if you are aware of any other means of getting this to work. Thanks again!

bprakash...@gmail.com

unread,
Feb 1, 2017, 3:35:53 PM2/1/17
to JaCoCo and EclEmma Users, bprakash...@gmail.com, hoff...@mountainminds.com
I got it working with the following change in phase executions.

clean compile org.jacoco:jacoco-maven-plugin:instrument test org.jacoco:jacoco-maven-plugin:restore-instrumented-classes org.jacoco:jacoco-maven-plugin:report

'Instrument' is executed as part of maven 'process-classes' which comes after 'compile', but for some reason code was not getting compiled and hence 'classes' folder was not found so obviously jacoco was not generating report. Now when I manually compile, it is working fine. Down side is it tries to compile twice, once before instrument and once after instrument, but since the source is already compiled, the second compile doesnt do anyting, it just posts tht 'no change in class'.

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages