On Wednesday, July 3, 2019 at 2:51:07 PM UTC+2, [email protected] 
wrote:
>
> On Wednesday, July 3, 2019 at 8:40:17 AM UTC-4, Evgeny Mandrikov wrote: 
> > On Wednesday, July 3, 2019 at 1:59:10 PM UTC+2, [email protected] 
> wrote:On Tuesday, July 2, 2019 at 9:25:10 AM UTC-4, Evgeny Mandrikov wrote: 
> > 
> > > Hi, 
> > 
> > > 
> > 
> > > On Tuesday, July 2, 2019 at 3:12:52 PM UTC+2, [email protected] 
> wrote: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? 
> > 
> > > 
> > 
> > > 
> > 
> > > No, this can't be the reason, because 
> > 
> > > for javac versions from 7 to 10 we have 
> https://github.com/jacoco/jacoco/blob/v0.8.2/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/TryWithResourcesJavacFilter.java
>  
> > 
> > > 
> > 
> > > for javac versions starting from 11 we have 
> https://github.com/jacoco/jacoco/blob/v0.8.2/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/TryWithResourcesJavac11Filter.java
>  
> > 
> > > for ecj we have 
> https://github.com/jacoco/jacoco/blob/v0.8.2/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/TryWithResourcesEcjFilter.java
>   
>
> > 
> > > 
> > 
> > > 
> > 
> > > 
> > 
> > > In order to investigate please provide complete reproducer, 
> > 
> > > or at least source file and corresponding class file. 
> > 
> > >   
> > 
> > > Thanks. 
> > 
> > > -Mark 
> > 
> > 
> > 
> > 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". 
> > 
> > 
> > 
> > 
> > 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. 
> > 
> > 
> > 
> > 
> > 
> > 
> >   
> > 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! 
>

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 :

[image: 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

-- 
You received this message because you are subscribed to the Google Groups 
"JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jacoco/18c1b704-9e59-423c-bbd9-b787991fa483%40googlegroups.com.

Reply via email to