Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import static jdk.jpackage.internal.model.MacPackage.RUNTIME_BUNDLE_LAYOUT;
import static jdk.jpackage.internal.model.StandardPackageType.MAC_DMG;
import static jdk.jpackage.internal.model.StandardPackageType.MAC_PKG;
import static jdk.jpackage.internal.util.function.ExceptionBox.rethrowUnchecked;
import static jdk.jpackage.internal.util.function.ExceptionBox.toUnchecked;

import java.nio.file.Path;
import java.util.Objects;
Expand Down Expand Up @@ -153,11 +153,11 @@ static MacPkgPackage createMacPkgPackage(Options options) {
} catch (RuntimeException ex) {
// Some error occurred trying to configure the signing config for the package.
// Ignore it, bail out with the first error.
rethrowUnchecked(expiredAppCertException);
throw toUnchecked(expiredAppCertException);
}

Log.error(pkgSignConfigResult.firstError().orElseThrow().getMessage());
rethrowUnchecked(expiredAppCertException);
throw toUnchecked(expiredAppCertException);
}
}

Expand Down Expand Up @@ -303,7 +303,7 @@ private static void rethrowIfNotExpiredCertificateException(Result<?> result) {
}
}

rethrowUnchecked(ex);
throw toUnchecked(ex);
}

private static SigningIdentityBuilder createSigningIdentityBuilder(Options options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

final class TempKeychain implements Closeable {

static void withKeychains(ThrowingConsumer<List<Keychain>> keychainConsumer, List<Keychain> keychains) throws Throwable {
static void withKeychains(ThrowingConsumer<List<Keychain>, ? extends Exception> keychainConsumer, List<Keychain> keychains) throws Exception {
keychains.forEach(Objects::requireNonNull);
if (keychains.isEmpty() || OSVersion.current().compareTo(new OSVersion(10, 12)) < 0) {
keychainConsumer.accept(keychains);
Expand All @@ -47,7 +47,7 @@ static void withKeychains(ThrowingConsumer<List<Keychain>> keychainConsumer, Lis
}
}

static void withKeychain(ThrowingConsumer<Keychain> keychainConsumer, Keychain keychain) throws Throwable {
static void withKeychain(ThrowingConsumer<Keychain, ? extends Exception> keychainConsumer, Keychain keychain) throws Exception {
Objects.requireNonNull(keychainConsumer);
withKeychains(keychains -> {
keychainConsumer.accept(keychains.getFirst());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static ExternalApplication load(ApplicationLayout appLayout, OperatingSystem os)

} catch (XPathExpressionException ex) {
// This should never happen as XPath expressions should be correct
throw ExceptionBox.rethrowUnchecked(ex);
throw ExceptionBox.toUnchecked(ex);
} catch (SAXException ex) {
// Malformed input XML
throw new JPackageException(I18N.format("error.malformed-app-image-file", relativeAppImageFilePath, appImageDir), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ private void execute(TaskContext context) {
try {
builder.create().call();
} catch (Exception ex) {
throw ExceptionBox.rethrowUnchecked(ex);
throw ExceptionBox.toUnchecked(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static int run(PrintWriter out, PrintWriter err, String... args) {
Log.fatalError(I18N.format("ERR_CannotParseOptions", ex.getMessage()));
return 1;
} catch (IOException ex) {
throw ExceptionBox.rethrowUnchecked(ex);
throw ExceptionBox.toUnchecked(ex);
}

final var bundlingEnv = ServiceLoader.load(CliBundlingEnvironment.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.List;
import java.util.Optional;
import jdk.internal.util.OperatingSystem;
import jdk.jpackage.internal.util.function.ExceptionBox;
import jdk.jpackage.internal.util.function.ThrowingConsumer;

public final class FileUtils {
Expand Down Expand Up @@ -170,15 +169,13 @@ private static void adjustAttributes(Path path) throws IOException {
}
}

private void runActionOnPath(ThrowingConsumer<Path> action, Path path) {
private void runActionOnPath(ThrowingConsumer<Path, IOException> action, Path path) {
try {
action.accept(path);
} catch (IOException ex) {
if (this.ex == null) {
this.ex = ex;
}
} catch (Throwable t) {
throw ExceptionBox.rethrowUnchecked(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package jdk.jpackage.internal.util;

import static jdk.jpackage.internal.util.function.ExceptionBox.rethrowUnchecked;
import static jdk.jpackage.internal.util.function.ExceptionBox.toUnchecked;

import java.util.Collection;
import java.util.List;
Expand All @@ -50,7 +50,7 @@ public record Result<T>(Optional<T> value, Collection<? extends Exception> error

public T orElseThrow() {
firstError().ifPresent(ex -> {
rethrowUnchecked(ex);
throw toUnchecked(ex);
});
return value.orElseThrow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
package jdk.jpackage.internal.util;

import static jdk.jpackage.internal.util.function.ExceptionBox.rethrowUnchecked;
import static jdk.jpackage.internal.util.function.ExceptionBox.toUnchecked;

import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -80,7 +80,7 @@ public static void createXml(Path dstFile, XmlConsumer xmlConsumer) throws IOExc
xml.flush();
xml.close();
} catch (XMLStreamException ex) {
throw rethrowUnchecked(ex);
throw toUnchecked(ex);
}
}

Expand All @@ -101,7 +101,7 @@ public static void createXml(DOMResult dom, XmlConsumer xmlConsumer) throws IOEx
xml.flush();
xml.close();
} catch (XMLStreamException ex) {
throw rethrowUnchecked(ex);
throw toUnchecked(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,23 +30,55 @@ public class ExceptionBox extends RuntimeException {

private static final long serialVersionUID = 1L;

public static RuntimeException rethrowUnchecked(Throwable throwable) {
if (throwable instanceof RuntimeException err) {
throw err;
}

if (throwable instanceof Error err) {
throw err;
public static RuntimeException toUnchecked(Exception ex) {
switch (ex) {
case RuntimeException rex -> {
return rex;
}
case InvocationTargetException itex -> {
var t = itex.getCause();
if (t instanceof Exception cause) {
return toUnchecked(cause);
} else {
throw (Error)t;
}
}
case InterruptedException _ -> {
Thread.currentThread().interrupt();
return new ExceptionBox(ex);
}
default -> {
return new ExceptionBox(ex);
}
}
}

if (throwable instanceof InvocationTargetException err) {
throw rethrowUnchecked(err.getCause());
public static Exception unbox(Throwable t) {
switch (t) {
case ExceptionBox ex -> {
return unbox(ex.getCause());
}
case InvocationTargetException ex -> {
return unbox(ex.getCause());
}
case Exception ex -> {
return ex;
}
case Error err -> {
throw err;
}
default -> {
// Unreachable
throw reachedUnreachable();
}
}
}

throw new ExceptionBox(throwable);
public static Error reachedUnreachable() {
return new AssertionError("Reached unreachable!");
}

private ExceptionBox(Throwable throwable) {
super(throwable);
private ExceptionBox(Exception ex) {
super(ex);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,17 +27,17 @@
import java.util.function.BiConsumer;

@FunctionalInterface
public interface ThrowingBiConsumer<T, U> {
public interface ThrowingBiConsumer<T, U, E extends Exception> {

void accept(T t, U u) throws Throwable;
void accept(T t, U u) throws E;

public static <T, U> BiConsumer<T, U> toBiConsumer(
ThrowingBiConsumer<T, U> v) {
ThrowingBiConsumer<T, U, ? extends Exception> v) {
return (t, u) -> {
try {
v.accept(t, u);
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,17 +27,17 @@
import java.util.function.BiFunction;

@FunctionalInterface
public interface ThrowingBiFunction<T, U, R> {
public interface ThrowingBiFunction<T, U, R, E extends Exception> {

R apply(T t, U u) throws Throwable;
R apply(T t, U u) throws E;

public static <T, U, R> BiFunction<T, U, R> toBiFunction(
ThrowingBiFunction<T, U, R> v) {
ThrowingBiFunction<T, U, R, ? extends Exception> v) {
return (t, u) -> {
try {
return v.apply(t, u);
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,16 +27,16 @@
import java.util.function.Consumer;

@FunctionalInterface
public interface ThrowingConsumer<T> {
public interface ThrowingConsumer<T, E extends Exception> {

void accept(T t) throws Throwable;
void accept(T t) throws E;

public static <T> Consumer<T> toConsumer(ThrowingConsumer<T> v) {
public static <T> Consumer<T> toConsumer(ThrowingConsumer<T, ? extends Exception> v) {
return o -> {
try {
v.accept(o);
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,16 +27,16 @@
import java.util.function.Function;

@FunctionalInterface
public interface ThrowingFunction<T, R> {
public interface ThrowingFunction<T, R, E extends Exception> {

R apply(T t) throws Throwable;
R apply(T t) throws E;

public static <T, R> Function<T, R> toFunction(ThrowingFunction<T, R> v) {
public static <T, R> Function<T, R> toFunction(ThrowingFunction<T, R, ? extends Exception> v) {
return t -> {
try {
return v.apply(t);
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,16 +25,16 @@
package jdk.jpackage.internal.util.function;

@FunctionalInterface
public interface ThrowingRunnable {
public interface ThrowingRunnable<E extends Exception> {

void run() throws Throwable;
void run() throws E;

public static Runnable toRunnable(ThrowingRunnable v) {
public static Runnable toRunnable(ThrowingRunnable<? extends Exception> v) {
return () -> {
try {
v.run();
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,16 +27,16 @@
import java.util.function.Supplier;

@FunctionalInterface
public interface ThrowingSupplier<T> {
public interface ThrowingSupplier<T, E extends Exception> {

T get() throws Throwable;
T get() throws E;

public static <T> Supplier<T> toSupplier(ThrowingSupplier<T> v) {
public static <T> Supplier<T> toSupplier(ThrowingSupplier<T, ? extends Exception> v) {
return () -> {
try {
return v.get();
} catch (Throwable ex) {
throw ExceptionBox.rethrowUnchecked(ex);
} catch (Exception ex) {
throw ExceptionBox.toUnchecked(ex);
}
};
}
Expand Down
Loading