GeezerVison is showing github.com — Add filter for exhaustive switch expression by Godin · Pull Request #1472 · jacoco/jacoco
Conversation
Godin
commented
Jun 5, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Before this change for the following Example.java
enum Example {
A, B;
static int example(Example e) {
return switch (e) {
case A -> 1;
case B -> 2;
};
}
public static void main(String[] args) {
example(A);
example(B);
}
}
using
openjdk version "17.0.2" 2022-01-18 LTS
OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)
execution of
javac Example.java -d classes
java -javaagent:jacocoagent.jar -cp classes Example
java -jar jacococli.jar report jacoco.exec --classfiles classes --sourcefiles . --html report
produces
and after this change
Fixes #771
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ice1000, roman-svystun, and lolo101 reacted with thumbs up emoji
ice1000, KirillSmirnow, roman-svystun, and lolo101 reacted with hooray emoji
ice1000, bcersows, and roman-svystun reacted with heart emoji
ice1000, KirillSmirnow, and roman-svystun reacted with rocket emoji
All reactions
-
👍
3 reactions
-
🎉
4 reactions
-
❤️
3 reactions
-
🚀
3 reactions
Godin
changed the title
Add filter for exhaustive switch-expression
Add filter for exhaustive switch expression
Jun 5, 2023
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work again! I left some formal comments you might want to consider.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@Godin should this already handle multi-line switch expressions? I still get missed branch warnings in my code, where I have something like:
enum Example {
A, B;
static int example(Example e) {
return switch (e) {
case A -> {
if (false) {
throw new InvalidStateException();
}
yield 1
};
case B -> {
if (false) {
throw new InvalidStateException();
}
yield 2;
};
}
public static void main(String[] args) {
example(A);
example(B);
}
}
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@nioncode for the following Example.java which is the same example as yours after fixing compilation errors
enum Example {
A, B;
static int example(Example e) {
return switch (e) {
case A -> {
if (false) {
throw new IllegalStateException();
}
yield 1;
}
case B -> {
if (false) {
throw new IllegalStateException();
}
yield 2;
}
};
}
public static void main(String[] args) {
example(A);
example(B);
}
}
using
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)
execution of
javac Example.java -d classes
java -javaagent:jacoco-0.8.12/lib/jacocoagent.jar -cp classes Example
java -jar jacoco-0.8.12/lib/jacococli.jar report jacoco.exec --classfiles classes --sourcefiles . --html report
produces the following report
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This is true. Sorry, I ran this through gradle and it seems it does not pick up the latest version by default.
Just for others, if you use the jacoco plugin in gradle, you can force a specific version via:
plugins {
id "jacoco" // note: don't specify a version here, specify it in the jacoco block below
}
jacoco {
toolVersion = "0.8.12"
}
Everything works fine as is, sorry for the noise.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
odd-contributor Bot
pushed a commit
to opendatadiscovery/odd-platform
that referenced
this pull request
Sep 11, 2026
…nthetic default is no longer counted
javac emits a default arm for an exhaustive enum `switch` expression that throws if the enum ever gains a
constant — 4 instructions and 1 branch no test can reach. JaCoCo 0.8.9 (Gradle 8.5's default) counts them;
0.8.11+ filters them (jacoco/jacoco#1472). On a 157-instruction class that arm alone is 2.55%, so a small new
class with every reachable line covered still reads below the 98% changed-files floor. odd-platform-api has 18
such `return switch (…)` sites in measured code; their numbers move up, nothing else changes.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01D6rCJ3zvUAsBZ5H9XYpfmG
Reviewers
marchof
marchof approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|