How to ignore inner static classes in Jacoco when

2020-04-30 23:31发布

问题:

I know how to ignore classes defined in their own .java files, but not aware of how to ignore inner classes.

For example, I have class A with nested class B:

class A {
    ...

    static class B {
        ...
    }
}

jacocoTestReport keeps checking the coverage when I want to ignore them in jacoco.gradle file with this syntax(learned from this post: How to ignore inner/nested classes with JaCoCo?): (setFrom part is for later versions of Gradle, where classDirectories = files() is deprecated)

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.8.3"
}

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: [
                            "com/example/xxx/*",
                            "com/example/xxx/A\$.*B*"
                    ])
        }))
    }
}

($ must be escaped, while in the post there is no need because he uses Maven when I use Gradle)

So, how can I ignore this inner class?

回答1:

At last I found the answer with several trial-and-failure. Seems that the naming pattern follows compiled Java classes naming convention, as mentioned in the other post, and will not require the . between the outer class and the inner class. So, it should be like A$B. And, there may be some .class interfering(my guess), so I added A$B*(for other normal classes, last * is not needed).

So it becomes:

"com/example/xxx/A\$B*"

I hope there be some documentation about this pattern of exclusion. There is not, yet.