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

Non-existent (invisible?) static analysis block shows 75% code coverage #1087

Closed
linusjf opened this issue Aug 25, 2020 · 11 comments
Closed
Assignees
Labels
declined: duplicate ❌ This issue or pull request already exists

Comments

@linusjf
Copy link

linusjf commented Aug 25, 2020

From StackOverflow Question

Steps to reproduce

  • JaCoCo version: 0.85
  • Operating system: Linux
  • Tool integration: Maven
  • Complete executable reproducer: https://github.com/Fernal73/DSAlgos
  • Steps: Run Jacoco on project and check report for BrickSortParallel class.

Expected behaviour

No static block or incomplete coverage for class expected.

Actual behaviour

50% code coverage displayed for static block (not visible).

@linusjf linusjf added the type: bug 🐛 Something isn't working label Aug 25, 2020
@marchof
Copy link
Member

marchof commented Aug 25, 2020

Your code uses the assert keyword which results in a static initializer:

static {};
  descriptor: ()V
  flags: (0x0008) ACC_STATIC
  Code:
    stack=1, locals=0, args_size=0
       0: ldc           #72                 // class ds/BrickSortParallel
       2: invokevirtual #73                 // Method java/lang/Class.desiredAssertionStatus:()Z
       5: ifne          12
       8: iconst_1
       9: goto          13
      12: iconst_0
      13: putstatic     #31                 // Field $assertionsDisabled:Z
      16: return
    LineNumberTable:
      line 19: 0
    StackMapTable: number_of_entries = 2
      frame_type = 12 /* same */
      frame_type = 64 /* same_locals_1_stack_item */
        stack = [ int ]

This is a know limitation. The workaround is to not use assert.

@marchof marchof closed this as completed Aug 25, 2020
@marchof
Copy link
Member

marchof commented Aug 25, 2020

Duplicate #613.

@marchof marchof self-assigned this Aug 25, 2020
@marchof marchof added declined: duplicate ❌ This issue or pull request already exists and removed type: bug 🐛 Something isn't working labels Aug 25, 2020
@linusjf
Copy link
Author

linusjf commented Aug 25, 2020

I've flagged the method that uses assert with a @Generated annotation. Doesn't that filter that out? I'm aware that assert is not filtered out by Jacoco. It's on the things-to-do list. So the @Generated annotation doesn't help?

@linusjf
Copy link
Author

linusjf commented Aug 25, 2020

Another question wrt to the Google Group:
https://groups.google.com/g/jacoco/c/k7yvGHGExvg

I can't seem to create a new thread online. Is the only way to do it is email the group, instead?

@marchof
Copy link
Member

marchof commented Aug 25, 2020

@Fernal73 The method as such will be excluded due to the @Generated annotation. But the Java compiler creates additional infrastructure for assertions in the static initializer.

@marchof
Copy link
Member

marchof commented Aug 25, 2020

@Fernal73 Does the "New conversation" button nut work for you?

image

@linusjf
Copy link
Author

linusjf commented Aug 25, 2020

No.
Screenshot_2020-08-25-20-35-36-771_com android chrome
Screenshot_2020-08-25-20-35-23-546_com android chrome

@linusjf
Copy link
Author

linusjf commented Aug 25, 2020

@Fernal73 The method as such will be excluded due to the @Generated annotation. But the Java compiler creates additional infrastructure for assertions in the static initializer.

Thanks for clearing that up. I wasn't aware that it's a static initialiser. Evidently, I'm not familiar with java byte code. I had removed the assert statement earlier but then felt it's a good idea to push it into a @Generated method. I guess I didn't notice that the code coverage earlier was incomplete because of the static block it generates. It's a shame , really, since assertions can be useful without having to use an external API.

@marchof
Copy link
Member

marchof commented Aug 25, 2020

@Fernal73 Not true, JDK nowadays provides APIs for this, for example java.util.Objects.nonNull()

@linusjf
Copy link
Author

linusjf commented Aug 25, 2020

@Fernal73 Does the "New conversation" button nut work for you?

image

Why nut? Is the email group only for the crazies over there?

@linusjf
Copy link
Author

linusjf commented Aug 26, 2020

@Fernal73 Not true, JDK nowadays provides APIs for this, for example java.util.Objects.nonNull()

I discovered this excellent explanation on StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
declined: duplicate ❌ This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants