Uploaded image for project: 'Clover'
  1. Clover
  2. CLOV-1984

Instrumentation error for generic array constructor reference

    XMLWordPrintable

Details

    • Severity 2 - Major

    Description

      Clover fails to instrument source code in case when a java8 method reference is used to call a generic array constructor, such as:

      Class<?>[]::new

      Example:

      public class A {
        public void a(Class<?>[] interfaces) { 
          java.util.Arrays.stream(interfaces).toArray(Class<?>[]::new); 
        }
      }
      

      Stack trace:

      com.atlassian.clover.api.CloverException: /tmp/A.java:4:5:unexpected token: java
      at com.atlassian.clover.instr.java.Instrumenter.instrument(Instrumenter.java:161)
      at com.atlassian.clover.CloverInstr.execute(CloverInstr.java:76)
      at com.atlassian.clover.CloverInstr.mainImpl(CloverInstr.java:54)
      at com.atlassian.clover.CloverInstr.main(CloverInstr.java:39)
      Caused by: line 4:5: unexpected token: java
      at com.atlassian.clover.instr.java.JavaRecognizer.conditionalExpression(JavaRecognizer.java:4581)
      at com.atlassian.clover.instr.java.JavaRecognizer.assignmentExpression(JavaRecognizer.java:4448)
      at com.atlassian.clover.instr.java.JavaRecognizer.expression(JavaRecognizer.java:3515)
      at com.atlassian.clover.instr.java.JavaRecognizer.statement(JavaRecognizer.java:3038)
      at com.atlassian.clover.instr.java.JavaRecognizer.outerCompoundStmt(JavaRecognizer.java:2825)
      at com.atlassian.clover.instr.java.JavaRecognizer.field(JavaRecognizer.java:2394)
      at com.atlassian.clover.instr.java.JavaRecognizer.classBlock(JavaRecognizer.java:1857)
      at com.atlassian.clover.instr.java.JavaRecognizer.classDefinition(JavaRecognizer.java:865)
      at com.atlassian.clover.instr.java.JavaRecognizer.typeDefinition2(JavaRecognizer.java:795)
      at com.atlassian.clover.instr.java.JavaRecognizer.typeDefinition(JavaRecognizer.java:648)
      at com.atlassian.clover.instr.java.JavaRecognizer.compilationUnit(JavaRecognizer.java:566)
      at com.atlassian.clover.instr.java.Instrumenter.instrument(Instrumenter.java:213)
      at com.atlassian.clover.instr.java.Instrumenter.instrument(Instrumenter.java:126)
      ... 3 more
      

      Workaround:

      1) Do not write a generic array constructor reference. Sic! Java, because of type erasure, don't have generic arrays.

      2) Change method reference to a lambda, e.g.:

      value -> new Class<?>[value]
      

      3) Or exclude source file from instrumentation.

      Expected fix:

      Extend Clover's ANTLR grammar to handle generic array constructor references:

      A<B>[]::new
      

      Note: Clover already handles:

      A::new
      A[]::new
      A<B>::new
      A<B>::<B>new
      (A)B::new
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            mparfianowicz Marek Parfianowicz
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: