JAVAC.TRYWITH filter not working?

44 views
Skip to first unread message

dragon...@gmail.com

unread,
Jul 2, 2019, 9:12:52 AM7/2/19
to JaCoCo and EclEmma Users
Hi all.

I'm using try-with-resources and JaCoCo reports a large number of missed branches. I'm using JaCoCo version 0.8.2, which includes the JAVAC.TRYWITH filter. I understand that this filter is unconditionally on, but it doesn't seem to be working.

A little research into the filter seems to indicate that this is for Java 11, and I'm using Java 8.

Is this the reason I'm seeing the missed branches?

Thanks.

-Mark

Evgeny Mandrikov

unread,
Jul 2, 2019, 9:25:10 AM7/2/19
to JaCoCo and EclEmma Users

dragon...@gmail.com

unread,
Jul 3, 2019, 7:59:10 AM7/3/19
to JaCoCo and EclEmma Users
Interesting. Well, I'm not doing anything very surprising.

Here's a brief example (showing function only):

// The URL is to a file located on the classpath, and was obtained using:
// Thread.currentThread().getContextClassLoader().getResource(fileName);
//
private String loadResource(URL url) throws IOException {
StringBuilder builder = new StringBuilder();

try (Reader reader = new InputStreamReader(url.openStream())) {
int numRead;
char[] buf = new char[2048];
while((numRead = reader.read(buf)) != -1) {
builder.append(buf, 0, numRead);
}
}

return builder.toString();
}

The "try" line is highlighted yellow in the report, and the closing brace is also highlighted yellow with a yellow diamond. Hovering over it with the mouse reveals that "6 of 8 branches were missed".

Is this supposed to be ignored by the filter that is available for Java 7/8? It seems to fall into the area of "code generated by compiler".

Also, I found it interesting that the lazy initialization via ResourceHolder pattern shows lack of coverage. Here's a short example:

public class Foo {

private static class ResourceHolder {
private static final Foo INSTANCE = new Foo();
}

public static Foo getInstance() {
return ResourceHolder.INSTANCE;
}

// Remaining code not shown.
}

The line defining the class "ResourceHolder" is RED in the JaCoCo report. Perhaps because there is a default constructor that does not get called?

Thanks. I hope this helps.

-Mark

Evgeny Mandrikov

unread,
Jul 3, 2019, 8:40:17 AM7/3/19
to JaCoCo and EclEmma Users

example.png

Yes it is - execution of following commands using JDK 8u212

javac Example.java
java -javaagent:jacoco-0.8.2/lib/jacocoagent.jar=append=false Example
java -jar jacoco-0.8.2/lib/jacococli.jar report jacoco.exec --classfiles Example.class --sourcefiles . --html report

leads to the below report that doesn't contain any missing branches.

So let me repeat - please provide corresponding class file.


example.png

 
Also, I found it interesting that the lazy initialization via ResourceHolder pattern shows lack of coverage.  Here's a short example:

public class Foo {

   private static class ResourceHolder {
      private static final Foo INSTANCE = new Foo();
   }

   public static Foo getInstance() {
      return ResourceHolder.INSTANCE;
   }

   // Remaining code not shown.
}

The line defining the class "ResourceHolder" is RED in the JaCoCo report.  Perhaps because there is a default constructor that does not get called?

Yes this is because of implicit default constructor that is never called. You can add explicit private no-arg constructor and it will be filtered-out.

dragon...@gmail.com

unread,
Jul 3, 2019, 8:51:07 AM7/3/19
to JaCoCo and EclEmma Users
> Also, I found it interesting that the lazy initialization via ResourceHolder pattern shows lack of coverage.  Here's a short example:
>
>
>
> public class Foo {
>
>
>
>    private static class ResourceHolder {
>
>       private static final Foo INSTANCE = new Foo();
>
>    }
>
>
>
>    public static Foo getInstance() {
>
>       return ResourceHolder.INSTANCE;
>
>    }
>
>
>
>    // Remaining code not shown.
>
> }
>
>
>
> The line defining the class "ResourceHolder" is RED in the JaCoCo report.  Perhaps because there is a default constructor that does not get called?
>
>
>
>
> Yes this is because of implicit default constructor that is never called. You can add explicit private no-arg constructor and it will be filtered-out.

Thanks for the response!

Two things:
1. I will try adding the private no-args constructor.
2. Your image revealed something I had not seen before - the JaCoCo version in the lower right corner of the page. I was not seeing this in my browser, probably because the window is so small. The version it is showing is 0.7.9. I'm using an SBT plugin, and I was TOLD it used 0.8.2 for JaCoCo, so apparently something isn't configured properly, or that information was inaccurate.

Sorry to take up your time with something so trivial, but I appreciate your response.

Have a great remainder of the week!

-Mark

Evgeny Mandrikov

unread,
Jul 3, 2019, 9:19:54 AM7/3/19
to JaCoCo and EclEmma Users
You're welcome.
 

Two things:
1. I will try adding the private no-args constructor.

This will work only if you use JaCoCo version 0.8.0 or above - see https://www.jacoco.org/jacoco/trunk/doc/changes.html
 
2. Your image revealed something I had not seen before - the JaCoCo version in the lower right corner of the page.  I was not seeing this in my browser, probably because the window is so small.  The version it is showing is 0.7.9.

Good catch!
 
 I'm using an SBT plugin, and I was TOLD it used 0.8.2 for JaCoCo, so apparently something isn't configured properly, or that information was inaccurate.

If you use https://github.com/sbt/sbt-jacoco , then AFAIK its latest release 3.1.0 still uses/provides JaCoCo version 0.7.9 :

Commits_·_sbt_sbt-jacoco.png



As an option I believe that you can use JaCoCo command line interface directly from sbt without any plugin.

 
Sorry to take up your time with something so trivial, but I appreciate your response.

Have a great remainder of the week!

Glad that the root cause was found! Good luck with versions updates.

Have a nice week too.



Regards,
Evgeny

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages