Improve classes in the "jdk.jpackage.internal.util.function" package

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Affects Version/s: 17, 21, 25, 26, 27
    • Component/s: tools
    • None
    • Fix Understood
    • generic
    • generic

      1. Functional interfaces in the "jdk.jpackage.internal.util.function" package have hardcoded "throws Throwable" in their abstract method signatures. This is overly vague and doesn't allow preserving the exception type of a wrapped functional object, resulting in foolish exception handlers:

      class FileContentCreator {

          static void createFile(Path location) throws IOException {
              createContent(location, Files::createFile);
          }

          static void createDirectory(Path location) throws IOException {
              createContent(location, Files::createDirectory);
          }

          private static void createContent(Path location, ThrowingConsumer<Path> contentCreator) throws IOException {
              try {
                  contentCreator.accept(location);
              } catch (IOException ex) {
                  throw ex;
              } catch (Throwable t) {
                  // This is dead code. It will never get here under normal conditions.
                  throw ExceptionBox.rethrowUnchecked(t);
              }
          }
      }

      Adding an exception type that functional interfaces can throw will address this issue. createContent() method from the example above will become as simple as:

      private static void createContent(Path location, ThrowingConsumer<Path, IOException> contentCreator) throws IOException {
          contentCreator.accept(location);
      }

      2. ExceptionBox.rethrowUnchecked() method is handy, but causes coverage gaps. Adding tests for functional interfaces in the "jdk.jpackage.internal.util.function" package looked simple and promised 100% coverage, but the coverage was 76%. All because ExceptionBox.rethrowUnchecked() always throws an exception and never returns. Changing its implementation to return an exception instead of throwing it, and replacing all "ExceptionBox.rethrowUnchecked()" calls with "throw ExceptionBox.rethrowUnchecked()" statements, boosted coverage to the expected 100%. However, with this change, the function name becomes misleading. Suggested change: rename it to "toUnchecked", keeping the signature and behavior.

      3. ExceptionBox.rethrowUnchecked() method doesn't handle InterruptedException. It treats it as any other unchecked exception and merely rethrows it wrapped in an unchecked exception [1]. If ExceptionBox.rethrowUnchecked() method handles InterruptedException, it should call `Thread.currentThread().interrupt()` at some point as [2], [3], and [4] suggest.

      [1] https://github.com/openjdk/jdk/blob/23c39757ecdc834c631f98f4487cfea21c9b948b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/util/function/ExceptionBox.java#L46
      [2] https://mail.openjdk.org/pipermail/core-libs-dev/2025-November/154573.html
      [3] https://github.com/openjdk/jdk/blob/23c39757ecdc834c631f98f4487cfea21c9b948b/src/java.base/share/classes/java/lang/Thread.java#L183
      [4] https://github.com/openjdk/jdk/blob/23c39757ecdc834c631f98f4487cfea21c9b948b/src/java.base/share/classes/java/lang/InterruptedException.java#L34

            Assignee:
            Alexey Semenyuk
            Reporter:
            Alexey Semenyuk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: