In my case, some testing frameworks based bytecode like Robolectric, need to keep compatible with JaCoCo. And it handles carefully by detecting JaCoCo instrumented.
https://github.com/robolectric/robolectric/issues/8001
Is there any ways to check if a class was instrumented by JaCoCo?
I came up with a possible solution based on JaCoCo docs and implementation. Like:
Checking jacoco instrument by if there are two members in class with being mared as synthetic:
- private static field with naming $jacocoData
- private static method with naming $jacocoInit
According to the JaCoCo docs:
> To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.
See
https://www.eclemma.org/jacoco/trunk/doc/faq.htmlAnd both JaCoCo on-the-fly and offline instrument are sharing the same options, and they instrument code in class granularity. It means that once a class is to be instrumented, all methods of this class are instrumented.
https://www.jacoco.org/jacoco/trunk/doc/agent.html
IMO, are there any better ways to check?
Like, adding a field or flag in class like naming with `$jacocoInstrumented = true`.