Skip to content

Ignore Immutables generated code. #656

Closed

Description

@matt-isaman

Steps to reproduce

JaCoCo version: the jacoco gradle plugin version 0.7.9 and whatever is used by Eclipse via EclEmma version 3.0.1
Operating system: MacOS
Tool integration: Gradle and Eclipse (via EclEmma version 3.0.1)

I have attached a super simple eclipse project which can repro the issue.

Steps I used to make the attached project:

  • Use the Immutables library
  • Write tests for the Immutables based classes
  • Run those tests and coverage reporting

The generated classes are annotated with @javax.annotation.Generated, in case that is something you can use to filter on.

Expected behaviour

Immutables generated code is ignored in the coverage reporting. (Doctored up image of expectations)
foo-coverage-expected

Actual behaviour

Missed coverage in Immutables generated code is counted against overall coverage.
foo-coverage-actual

Activity

  1. Godin commented on Mar 15, 2018

    @Godin
    Member

    @matt-isaman @javax.annotation.Generated has @Retention(SOURCE) -
    see https://docs.oracle.com/javase/7/docs/api/javax/annotation/Generated.html , so not presented in bytecode. Because of this Lombok for example has own annotation @lombok.Generated that has @Retention(CLASS), similarly Groovy recently introduced @groovy.transform.Generated. JaCoCo performs analysis of bytecode and can automatically filter methods marked by the last two. But there is nothing what we can do as long as there is no way to distinguish methods/classes based on bytecode of compiled source files generated by Immutables library. Is there a way to instruct Immutables library to generate another annotation? JaCoCo provides ability to exclude packages/classes by name pattern, maybe this is already sufficient?

  2. added
    feedback pending
    on Mar 15, 2018
  3. matt-isaman commented on Mar 15, 2018

    @matt-isaman
    Author

    Filtering by package/class name wouldn't be a general solution as the name of the generated classes can be customized via configuration and may vary depending on the purpose of the generated classes. So you would have to duplicate this configuration in the JaCoCo filtering configuration and wouldn't be a durable solution if anything in the Immutables configuration changed.

    I have created an issue in their github repository to consider adding an annotation with class retention as Lombok and Groovy have.

  4. cal101 commented on May 22, 2018

    @cal101

    It looks like immutables library will include such an annotation in it's 2.6 release.
    I understood that recognizing this annotation will require changes to the jacoco code.

    So why not go a small step further.

    What about providing an annotation "@JacocoIgnore" that can be used as an annotation and as a meta annotation (https://www.intertech.com/Blog/spring-4-meta-annotations/) so that one can write custom annotations "@PlainGetterSetter" where the annotation itself is annoted with "@JacocoIgnore" and so serves two purposes:

    1. marks plain getters and setters (can be checked by other tool if custom conditions hold for this)
    2. Make jacoco ignore them for coverage calculation

    Can anyone give a hint in which class/package this would be implemented?

  5. Serranya commented on May 28, 2018

    @Serranya

    A runtime annotation is now added to the generated classes from the immutables project. Based on that annotation it should be possible to exclude that class from coverage analysis.

    Any hints what I can do to do that? Or is there a plan to include that functionality in jacoco.

  6. marchof commented on May 29, 2018

    @marchof
    Member

    @Serranya Can you please provide a reference or documentation link?

  7. Serranya commented on May 29, 2018

    @Serranya

    @marchof

    The relevant issue is this one: immutables/immutables#756
    The annotation used is this: https://github.com/immutables/immutables/blob/edc79cf0e713e173dc7a74d2cd017fceea45faf0/value/src/org/immutables/value/Generated.java

    I did not find the feature mentioned in the docs, but my generated class looks like this:

    @SuppressWarnings({"all"})
    @ParametersAreNonnullByDefault
    @Generated("org.immutables.processor.ProxyProcessor")
    @org.immutables.value.Generated(from = "Pair", generator = "Immutables")
    @Immutable
    @CheckReturnValue
    public final class ImmutablePair<F, S> implements Pair<F, S> {
    
  8. marchof commented on May 29, 2018

    @marchof
    Member

    @Godin What do you think: Should we change our method filters to ignore a method when there is any *.Generated annotation? This would be a similar aproach as proposed in #675

  9. added and removed
    feedback pending
    on May 29, 2018
  10. gedl commented on May 29, 2018

    @gedl

    👍 for this feature
    👍 👍 if annotations can be configurable on the pom or equivalent

  11. cdir commented on Jun 5, 2018

    @cdir

    +1
    Is there any possibility to configure the qualified name of the annotation that marks these generated methods?

  12. self-assigned this
    on Jun 6, 2018
  13. Godin commented on Jun 13, 2018

    @Godin
    Member

    While immutables/immutables#756 was marked as resolved and with latest version 2.6.1 some generated classes are annotated by org.immutables.value.Generated, seems that this is not the case for Builders - created immutables/immutables#792

    What do you think: Should we change our method filters to ignore a method when there is any *.Generated annotation?

    @marchof my memory tells me that we discussed this in past or at least was joking about that and for some reason decided to be concrete/precise, but I can't find any reference. Did you change your mind or this was in my dreams? 😆

    Note that this case is different from previous ones - class receives annotation, not methods. Thanks to changes for #689 we now have easy access to class annotations from filters 😋

    I was about to propose to implement this ticket as-is as we already do for others, and already prepared changes. And after that when this concrete feature and all test cases will be in place maybe reorganize into generic filter *.Generated. But now due to the above issue of missing annotations this concrete feature seems incomplete 😞 However we can still do this partially, leaving remaining cases in responsibility of library authors? Or directly do generic filter?

    This would be a similar aproach as proposed in #675

    "similar" but not exactly - #675 is less about generated code and more about manual tagging. And not sure that I like idea of manual tagging in general, especially with such a big granularity as method - today you manually tag simple method, tomorrow someone will update it up to complex one and this update will be completely missed. I prefer seeing low coverage for modified code as indication to do more careful review, than seeing 100% coverage because everything was excluded. Many times come across missing refactorings and unnecessary complexity because developers were trying to trick coverage. And even low coverage for whole application/modules rather than exclusions. Maybe we should think more/deeper about source-level annotations and enforcements around them? On the other hand already possible to abuse lombok.Generated and place it manually, however officially this is abuse rather than our statement - "yes, go ahead just exclude code instead of refactoring it into more testable and hence more maintainable". Or maybe this is out of our control and more of a philosophical/educational question.

    What I for sure don't like in #675 - is usage of regex in general and the way of usage in particular. IMO this is an overkill and performance degradation should be easily observable.

  14. matt-isaman commented on Jun 13, 2018

    @matt-isaman
    Author

    @Godin Would it be possible to update the HTML report to alleviate some of the concerns over people tricking coverage or abusing these annotations? Something along the lines of adding a new HTML file, or a new section to the current file, where all the excluded classes/methods are listed along with the reason why they were excluded. This would allow developers to ignore coverage for generated classes/methods but not lose track of the fact that they are being ignored.

    This would also allow for tooling around changes in the amount of code being excluded in the same way tools are used to display changes in coverage percentage between commits. If there was a suspiciously large bump in excluded code it would cause conscientious developers to double check why that happened and resolve any incorrect exclusions.

    One risk, depending on how this is done, would be that certain tools which depend on and parse the output from jacoco may have issues.

  15. philieu commented on Jun 28, 2018

    @philieu

    @Godin #675 was created with the following scenario in mind:

    • A build would fail if the code coverage is not 100%
    • There is an ability to exclude certain parts of the code via annotations or comments
    • What is being excluded is clear and right there in pull requests
    • The team can then discuss the merit and reason why certain code is being excluded

    The issue with percentage-based code coverage quality gate is that it is often not clear where the non-covered code is (unless one go through the report in details) and people can often trick coverage (e.g. write tests for the trivial parts but ignore the important bit).
    As you mentioned, we can already "cheat" using the lombok.Generated annotation. It's just not nice having to use that for the above purpose.

    Regarding the use of regex, if performance is a concern, I'm happy to update it in order to move the PR ahead.

  16. Godin commented on Jun 28, 2018

    @Godin
    Member

    @philieu First of all, I'm guilty for starting, but would prefer to stop hijacking this thread, which initially is about completely different subject - filtering of a code generated by Immutables library, and not at all about manual tagging.

    Please also keep in mind that development and maintenance of JaCoCo library is not the only one of our activities, we will get back to story of manual tagging and to your PR in particular as soon as will be possible to have spare time to work on it properly.

    Thank you for your understanding.

  17. philieu commented on Jun 28, 2018

    @philieu

    @Godin Sure, no worries. Was not meant to hijack the thread.

  18. added this to the 0.8.2 milestone on Aug 16, 2018
  19. Godin commented on Aug 16, 2018

    @Godin
    Member

    While immutables/immutables#792 wasn't yet resolved, I'm closing this ticket, because on our side exclusion of classes and methods having Generated annotation with retention policy CLASS or RUNTIME from any package was implemented in #731.

  20. locked as resolved and limited conversation to collaborators on Oct 8, 2018
  21. moved this to Done in Filteringon Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions