Skip to content

Refactor Sets/Maps/Lists MethodHandle dispatch for full branch coverage on any JDK#259

Merged
mercyblitz merged 1 commit intodevfrom
copilot/enhance-code-coverage-java-tests
Mar 19, 2026
Merged

Refactor Sets/Maps/Lists MethodHandle dispatch for full branch coverage on any JDK#259
mercyblitz merged 1 commit intodevfrom
copilot/enhance-code-coverage-java-tests

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

On JDK 8, the Set/Map/List.of(...) MethodHandles are null at class load time, making the invokeExact branch unreachable in tests. On JDK 9+, the null-guard branch is never hit. Neither branch achieves full coverage under a single JDK version.

Approach

Extract the MethodHandle dispatch logic from each public factory method into a package-private helper that accepts the handle as an explicit parameter. The public method becomes a one-liner delegate.

Before:

public static <E> Set<E> ofSet() {
    if (of0MethodHandle == null) {
        return emptySet();
    }
    try {
        return (Set<E>) of0MethodHandle.invokeExact();
    } catch (Throwable e) {
        return emptySet();
    }
}

After:

public static <E> Set<E> ofSet() {
    return ofSet0(of0MethodHandle);
}

static <E> Set<E> ofSet0(MethodHandle methodHandle) {
    if (methodHandle == null) {
        return emptySet();
    }
    try {
        return (Set<E>) methodHandle.invokeExact();
    } catch (Throwable e) {
        return emptySet();
    }
}

Tests can now exercise both branches unconditionally by injecting null or a type-incompatible handle:

@Test
void testOfSet0WithNull() {
    assertEquals(emptySet(), ofSet0(null));
}

@Test
void testOfSet0WithWrongMethodHandle() {
    MethodHandle wrongMethodHandle = findPublicStatic(Collections.class, "emptySet");
    assertEquals(emptySet(), ofSet0(wrongMethodHandle));
}

Changes

  • Sets.java — extracted ofSet0ofSet10 and ofSetElements helpers
  • Maps.java — extracted ofMap0ofMap10 and ofMapEntries helpers
  • Lists.java — extracted ofList0ofList10 and ofListElements helpers
  • SetsTest / MapsTest / ListsTest — added null + wrong-handle tests for every helper (114 tests total)

…anch coverage

Co-authored-by: mercyblitz <533114+mercyblitz@users.noreply.github.com>
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 98.14815% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../src/main/java/io/microsphere/collection/Sets.java 94.44% 2 Missing ⚠️
Files with missing lines Coverage Δ Complexity Δ
...src/main/java/io/microsphere/collection/Lists.java 97.67% <100.00%> (+40.91%) 38.00 <35.00> (+24.00)
.../src/main/java/io/microsphere/collection/Maps.java 97.67% <100.00%> (+43.62%) 38.00 <35.00> (+24.00)
.../src/main/java/io/microsphere/collection/Sets.java 94.18% <94.44%> (+46.88%) 37.00 <35.00> (+24.00)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mercyblitz mercyblitz marked this pull request as ready for review March 19, 2026 13:24
@mercyblitz mercyblitz merged commit 71d6064 into dev Mar 19, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants