Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tasomaniac opened this issue Jan 17, 2019 · 4 comments

Comments

@tasomaniac
Copy link

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.

@Godin Godin self-assigned this Jan 17, 2019
@Godin
Copy link
Member

Godin commented Jan 17, 2019

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.

@Godin Godin removed their assignment Jan 17, 2019
@Godin Godin closed this as completed Jan 17, 2019
@tasomaniac
Copy link
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?

@Godin
Copy link
Member

Godin commented Jan 17, 2019

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.

@Godin Godin mentioned this issue Jan 17, 2019
@tasomaniac
Copy link
Author

That makes sense. 😊 Thanks a lot.

@jacoco jacoco locked as resolved and limited conversation to collaborators May 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants