Skip to content

Generated annotation filter detection does not work when used with parameters #831

Closed

Description

@tasomaniac

Steps to reproduce

JaCoCo version: 0.8.2
Operating system: MacOS
Tool integration: Gradle

Expected behaviour

Class generated with @Generated annotation with parameters should also be ignored from code coverage report.

It is pretty easily reproducable when used by Google's Dagger annotation processor. They put something like below on top of generated classes

@Generated(
    value = "dagger.internal.codegen.ComponentProcessor",
    comments = "https://google.github.io/dagger"
)
public final class SomeGeneratedClass

Actual behaviour

Not just simple usage of @Generated annotation but all versions should be ignored.

Activity

  1. self-assigned this
    on Jan 17, 2019
  2. Godin commented on Jan 17, 2019

    @Godin
    Member

    Hi,

    This has nothing to do with parameters, because as you can see Google's Dagger annotation processor uses either javax.annotation.Generated or javax.annotation.processing.Generated and unfortunately both have retention policy SOURCE (aka discarded by compiler). This has been already discussed many times ( #685 (comment) , #656 (comment) ) and even mentioned in changelog - JaCoCo performs analysis of bytecode and hence annotation must have retention policy RUNTIME (aka "runtime visible") or CLASS (aka "runtime invisible" yet retained in class files).

    Annotations with parameters having correct retention policy are processed correctly:

    given src/org/example/Example.java

    package org.example;
    
    class Example {
    
      public void m1() {
      }
    
      @javax.annotation.Generated("")
      public void m2() {
      }
    
      @Generated(message = "")
      public void m3() {
      }
    
    }
    
    @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
    @interface Generated {
      String message();
    }
    

    execution of

    mkdir bin
    javac -d bin src/org/example/Example.java
    java -jar jacoco-0.8.2/lib/jacococli.jar report --classfiles bin --sourcefiles src --html report
    

    produces

    example

    as expected.

  3. removed their assignment
    on Jan 17, 2019
  4. tasomaniac commented on Jan 17, 2019

    @tasomaniac
    Author

    Thanks for the detailed reply. I actually searched before posting but couldn't find it.

    Here is 2 feedback:

    • the docs says Generated annotation is detected if it is visible or invisible. That is kind of confusing. I thought SOURCE retention would mean visible :)

    • The source files are available to Jacoco. Would it be possible to read sources to pick up the annotation?

  5. Godin commented on Jan 17, 2019

    @Godin
    Member

    the docs says Generated annotation is detected if it is visible or invisible. That is kind of confusing. I thought SOURCE retention would mean visible :)

    Noted, will improve.

    The source files are available to Jacoco. Would it be possible to read sources to pick up the annotation?

    No - source code parsing is out of scope of project, not even recalling/considering that JaCoCo supports Kotlin and Groovy.

  6. tasomaniac commented on Jan 17, 2019

    @tasomaniac
    Author

    That makes sense. 😊 Thanks a lot.

  7. locked as resolved and limited conversation to collaborators on May 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions