Jacoco API to filter report

48 views
Skip to first unread message

Rodrigo Amaro

unread,
Nov 23, 2018, 12:22:12 PM11/23/18
to JaCoCo and EclEmma Users
Hi everyone.

I know that Jacoco offers some options in order to filter which classes should be considered in the coverage report, but i sometimes fell that they are not enough (as many people that i've talked to)

So i was wondering if it's possible to use Jacoco API to open the .ec/.exec files and filter some files in order to get a more accurate value given my filtering needs.

I've been playing with the API and i was able to load the .ec file filter some stuff and generate a new HTML report. The problem is that i expected that the coverage would increase since i'm no longer considering some classes on the coverage report.

My code (Kotlin) is based on the examples provided on the documentation:

class ExecDump(private val out: PrintStream) {

private var execFileLoader: ExecFileLoader? = null
/**
* Run this example with the given parameters.
*
* @param args
* command line parameters
* @throws IOException
* in case of error reading a input file
*/
@Throws(IOException::class)
fun execute(args: Array<String>) {
for (file in args) {
dump(file)
}
}

private fun dump(file: String) {
execFileLoader = ExecFileLoader()
execFileLoader!!.load(File(file))

execFileLoader!!.executionDataStore.contents
.filter { it.name.contains("$") }
.onEach { execFileLoader!!.executionDataStore.contents.remove(it) }

val bundleCoverage = analyzeStructure()

bundleCoverage.packages.map { it.classes }
.onEach { pack ->
pack.filter { it.name.contains("$") }
.onEach { out.println(it.name) }
.onEach { pack.remove(it) }
}

val htmlFormatter = HTMLFormatter()
val visitor = htmlFormatter
.createVisitor(FileMultiReportOutput(File(".")))

// Initialize the report with all of the execution and session
visitor.visitInfo(execFileLoader?.sessionInfoStore?.infos,
execFileLoader?.executionDataStore?.contents)

visitor.visitBundle(bundleCoverage, DirectorySourceFileLocator(
File("path_to_source"), "utf-8", 4))

visitor.visitEnd()
}

@Throws(IOException::class)
private fun analyzeStructure(): IBundleCoverage {
val coverageBuilder = CoverageBuilder()

val analyzer = Analyzer(execFileLoader?.executionDataStore, coverageBuilder)

analyzer.analyzeAll(File("path_to_classes"))

return coverageBuilder.getBundle("Report JaCoCos")
}


companion object {

@Throws(IOException::class)
@JvmStatic
fun main(args: Array<String>) {
ExecDump(System.out).execute(arrayOf("path_to_ec_file"))
}
}
}

Resuming:
- Is it possible to use a .ec file and rebuild it excluding some classes?
- If yes, what should i change in the code above need to achieve it?

My goal, assuming this is possible, is to build a complementary gradle plugin to help filtering these classes

Marc Hoffmann

unread,
Nov 26, 2018, 4:30:40 AM11/26/18
to jac...@googlegroups.com
Hi Rodrigo,

the scope of the coverage report is defined by the classes you provide
to the analyzer, not by the content of the exec file. If you filter
classes from the exec file these classes simply show as not covered.

Instead of calling analyzer.analzeAll() on the root folder of your
classes iterate through the classses and only supply classes which are
you want to show in the report.

Regards,
-marc

ram...@uolinc.com

unread,
Nov 26, 2018, 7:18:31 AM11/26/18
to JaCoCo and EclEmma Users
Hey Marc, thanks for your reply.

I was able to achieve what i intended to by build a custom CoverageBuilder class and overriding the visitCoverage method.

Building a report with the same code i've submitted (but using this custom class) generates a Report without the classes filtered by the custom class and considering only the classes i want on the coverage.

Right now i'm trying to organize this code to provide a gradle plugin that would help configuring these filters

Regards,
Rodrigo

John A

unread,
Nov 26, 2018, 10:16:23 AM11/26/18
to jac...@googlegroups.com
Rodrigo,

Would you be willing to share your code? I'm looking for something similar and your code would be an easy and quick starting point.

John

--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/42f214f9-9d5b-4d4d-8f20-238fcd4e2c0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marc Hoffmann

unread,
Nov 26, 2018, 11:15:49 AM11/26/18
to jac...@googlegroups.com

Hi Rodrigo,

you do know that the gradle plugin already supports filtering on class level? With the includes and excludes property you can specify patters for the classfiles to include or exclude.

Regards,

-marc

ram...@uolinc.com

unread,
Nov 26, 2018, 11:44:08 AM11/26/18
to JaCoCo and EclEmma Users

ram...@uolinc.com

unread,
Nov 26, 2018, 11:53:29 AM11/26/18
to JaCoCo and EclEmma Users
Hey Marc, yeah i'm aware of this. 

Actually the major point what i intended to was to filter some Kotlin generated code. I later discovered that version 0.8.2 does this by default, but i was struggling to configure it on my Android build.

After some Googling i've found out that i need to set this on Gradle

android.jacoco.version = '0.8.2'

instead (or along, don't know if its also needed)

jacoco.toolVersion = '0.8.2'

With this, all the cases i wanted to filter were covered. Still, i think that there's room to improve filtering with some minor tweaks on current gradle plugin or with a new plugin

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