Compilation error with Clover enabled - explicit constructor invocation not allowed here

Symptoms

Clover-instrumented code fails to compile with the following error:

[ERROR] target/clover/src/com/acme/Producer.java:[25,43] explicit constructor invocation not allowed here

The error points at a constructor which contains a super(...) or this(...) call. The original, non-instrumented source compiles without any problem.

Earlier in the build log you will find a line like this:

[INFO] Processing files at JAVA_25 source level.

while the compiler is configured for a much lower level, for example:

[INFO] Compiling 1 source file with javac [debug target 1.8] to target/clover/classes

Cause

Java 25 introduced flexible constructor bodies (JEP 513), which allow statements to appear before the super(...) / this(...) call. OpenClover takes advantage of this: when instrumenting at source level 25 or higher, it places its coverage recorder calls in the constructor prologue, so that a constructor is recorded as executed even if the superclass constructor throws an exception.

The instrumented constructor therefore looks roughly like this:

public Producer(String name) {
    __CLR4_1_0.R.inc(25); __CLR4_1_0.R.inc(26);
    super(name);
    ...
}

This is valid Java 25, but it is not valid Java 24 or earlier - and javac rejects it with "explicit constructor invocation not allowed here".

The mismatch happens because OpenClover and your compiler disagree about the language level. If no source level is configured for Clover, it instruments at the highest language level it supports (25 in OpenClover 5.1.x), regardless of what maven.compiler.source, <javac source="..."> or your IDE is set to. Sources are then instrumented as Java 25 and compiled as, say, Java 8.

Solution

Make both sides agree. Either tell Clover which language level to use (recommended), or raise the compiler level.

1) Set the language level for Clover

Clover for Maven - set the jdk parameter to the same value your compiler uses:

<plugin>
    <groupId>org.openclover</groupId>
    <artifactId>clover-maven-plugin</artifactId>
    <version>5.1.0</version>
    <configuration>
        <jdk>8</jdk>
    </configuration>
</plugin>

or from the command line:

mvn -Dmaven.clover.jdk=8 clover:setup verify clover:clover

Clover for Ant - use the source attribute of the <clover-setup> task:

<clover-setup initstring="target/clover/clover.db" source="8"/>

Command line tools - use the --source option of CloverInstr:

java org.openclover.core.CloverInstr --source 8 -i clover.db -s src -d target/clover/src
2) Or raise the compiler level

If your project can be compiled with Java 25, set the compiler to that level instead - for example in Maven:

<maven.compiler.release>25</maven.compiler.release>

and make sure the build runs on JDK 25.

Tip: the "Processing files at JAVA_xx source level" message in the build log always tells you which level Clover actually used. Compare it with the level reported by the compiler - if they differ, this article applies. The same mismatch can also produce other errors for code which is valid in one Java version but not in another.

See also: Supported Platforms, Which Java and Groovy versions are supported?