Off by behavior for source code lines with kotlin?

79 views
Skip to first unread message

Mirko Friedenhagen

unread,
Jan 25, 2017, 6:32:27 PM1/25/17
to jac...@googlegroups.com
Hello,

I am playing with Kotlin and tried to do a POC for a SonarQube plugin[0]
While doing this I encountered a strange behavior (at least for me)
SonarQube iterates on iSourceFileCoverage and checks that the number
of lines in iSourceFileCoverage do not exceed the number of lines of
the source code.

I developed a small sample project which should show the behavior[1]
If you look at the output the line count:

XXX de/friedenhagen/jacocokotlinsample/App: iClassCoverage.lastLine=6,
iSourceFileCoverage.lastLine=18
XXX de/friedenhagen/jacocokotlinsample/App: lines=17 lastLine=18

lastLine is 18 but only 17 lines are found in the source[2].

The jacoco-maven-plugin is able to render the coverage correctly[3].

Note that kotlin creates a synthetic class AppKt file for methods not
found directly in the class, i.e. the companion object and the outer
method defined in [2].

Best Regards Mirko

[0] https://groups.google.com/d/topic/sonarqube/eyJa_y_-aoU/discussion
[1] https://gitlab.com/mfriedenhagen/jacoco-kotlin-sample/builds/9289284
[2] https://gitlab.com/mfriedenhagen/jacoco-kotlin-sample/blob/e1dacb838191920ddf9eeeffc3dfb20d622e3bd1/src/main/kotlin/de/friedenhagen/jacocokotlinsample/App.kt
[3] https://mfriedenhagen.gitlab.io/jacoco-kotlin-sample/
--
http://illegalstateexception.blogspot.com/
https://github.com/mfriedenhagen/ (http://osrc.dfm.io/mfriedenhagen)
https://bitbucket.org/mfriedenhagen/

Marc R. Hoffmann

unread,
Jan 26, 2017, 11:16:18 AM1/26/17
to jac...@googlegroups.com
Hi Mirko,

first of all: Nice to see you back here :-)

This seems to be an issue with the Kotlin compiler. The compiled class file AppKt has a reference to line 18 in method outer(). See decmpiled version below. JaCoCo simply takes this debug line information from the class file.

Cheers,
-marc




Classfile /Users/marc/git/jacoco-kotlin-sample/target/classes/de/friedenhagen/jacocokotlinsample/AppKt.class
  Last modified 26.01.2017; size 1107 bytes
  MD5 checksum 5f18406d76db819836b94e2d5a6193df
  Compiled from "App.kt"
public final class de.friedenhagen.jacocokotlinsample.AppKt
  SourceFile: "App.kt"
  SourceDebugExtension:
    SMAP
    App.kt
    Kotlin
    *S Kotlin
    *F
    + 1 App.kt
    de/friedenhagen/jacocokotlinsample/AppKt
    + 2 IntrinsicArrayConstructors.kt
    org/jetbrains/kotlin/codegen/intrinsics/IntrinsicArrayConstructorsKt
    *L
    1#1,17:1
    39#2:18
    *E
    *S KotlinDebug
    *F
    + 1 App.kt
    de/friedenhagen/jacocokotlinsample/AppKt
    *L
    16#1:18
    *E
  RuntimeVisibleAnnotations:
    0: #26(#27=[I#28,I#28,I#28],#29=[I#28,I#30,I#30],#31=I#32,#33=[s#34],#35=[s#5,s#36,s#37])
  minor version: 0
  major version: 50
  flags: ACC_PUBLIC, ACC_FINAL, ACC_SUPER
Constant pool:
   #1 = Utf8               de/friedenhagen/jacocokotlinsample/AppKt
   #2 = Class              #1             //  de/friedenhagen/jacocokotlinsample/AppKt
   #3 = Utf8               java/lang/Object
   #4 = Class              #3             //  java/lang/Object
   #5 = Utf8               outer
   #6 = Utf8               ()V
   #7 = Utf8               de/friedenhagen/jacocokotlinsample/App
   #8 = Class              #7             //  de/friedenhagen/jacocokotlinsample/App
   #9 = Utf8               Companion
  #10 = Utf8               Lde/friedenhagen/jacocokotlinsample/App$Companion;
  #11 = NameAndType        #9:#10         //  Companion:Lde/friedenhagen/jacocokotlinsample/App$Companion;
  #12 = Fieldref           #8.#11         //  de/friedenhagen/jacocokotlinsample/App.Companion:Lde/friedenhagen/jacocokotlinsample/App$Companion;
  #13 = Utf8               java/lang/String
  #14 = Class              #13            //  java/lang/String
  #15 = Utf8               [Ljava/lang/Object;
  #16 = Class              #15            //  "[Ljava/lang/Object;"
  #17 = Utf8               [Ljava/lang/String;
  #18 = Class              #17            //  "[Ljava/lang/String;"
  #19 = Utf8               de/friedenhagen/jacocokotlinsample/App$Companion
  #20 = Class              #19            //  de/friedenhagen/jacocokotlinsample/App$Companion
  #21 = Utf8               main
  #22 = Utf8               ([Ljava/lang/String;)V
  #23 = NameAndType        #21:#22        //  main:([Ljava/lang/String;)V
  #24 = Methodref          #20.#23        //  de/friedenhagen/jacocokotlinsample/App$Companion.main:([Ljava/lang/String;)V
  #25 = Utf8               elements$iv
  #26 = Utf8               Lkotlin/Metadata;
  #27 = Utf8               mv
  #28 = Integer            1
  #29 = Utf8               bv
  #30 = Integer            0
  #31 = Utf8               k
  #32 = Integer            2
  #33 = Utf8               d1
  #34 = Utf8              

Mirko Friedenhagen

unread,
Jan 26, 2017, 3:53:22 PM1/26/17
to jac...@googlegroups.com
Hello Marc,

hello as well :-). Thanks for the analysis. I will open a bug at Kotlin, then.

Regards
Mirko

Regards Mirko
> --
> 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 jacoco+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jacoco/13e7fb57-0c08-4800-d173-7947387f7453%40mountainminds.com.
> For more options, visit https://groups.google.com/d/optout.

Mirko Friedenhagen

unread,
Jan 26, 2017, 4:21:36 PM1/26/17
to jac...@googlegroups.com
Hello,

just for the record: But in the kotlin-tracker
https://youtrack.jetbrains.com/issue/KT-16002

Regards
Mirko
Regards Mirko
On Thu, Jan 26, 2017 at 5:16 PM, Marc R. Hoffmann
<hoff...@mountainminds.com> wrote:
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages