Skip to content

Conversation

@Godin
Copy link
Member

@Godin Godin commented Jun 5, 2023

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

before before

and after this change

after after

Fixes #771

@Godin Godin force-pushed the ExhaustiveSwitchFilter branch from ae0663f to 749871b Compare June 5, 2023 09:55
@Godin Godin changed the title Add filter for exhaustive switch-expression Add filter for exhaustive switch expression Jun 5, 2023
@Godin Godin force-pushed the ExhaustiveSwitchFilter branch from 6a99e78 to f26e464 Compare June 5, 2023 10:51
@Godin Godin marked this pull request as ready for review June 6, 2023 11:31
@Godin Godin requested a review from marchof June 6, 2023 11:31
Copy link
Member

@marchof marchof left a 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.

@marchof marchof merged commit 8271afb into jacoco:master Jun 14, 2023
@Godin Godin deleted the ExhaustiveSwitchFilter branch June 14, 2023 21:25
@nioncode
Copy link

@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);
    }
}

@Godin
Copy link
Member Author

Godin commented Apr 29, 2024

@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

Screenshot 2024-04-29 at 11 38 05 Screenshot 2024-04-29 at 11 37 46

@nioncode
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add filter for exhaustive switch-expression

3 participants