From f1ef4e1927af43eb17ec0abb2a09237e4f7dea2a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 11 Mar 2023 12:16:09 +0100 Subject: [PATCH] Makes exception types generic in ThrowableUtil --- .../pandacube/lib/reflect/ReflectField.java | 2 +- .../pandacube/lib/util/LazyOrException.java | 13 ++++++------ .../fr/pandacube/lib/util/ThrowableUtil.java | 21 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pandalib-reflect/src/main/java/fr/pandacube/lib/reflect/ReflectField.java b/pandalib-reflect/src/main/java/fr/pandacube/lib/reflect/ReflectField.java index 5be4842..509ab81 100644 --- a/pandalib-reflect/src/main/java/fr/pandacube/lib/reflect/ReflectField.java +++ b/pandalib-reflect/src/main/java/fr/pandacube/lib/reflect/ReflectField.java @@ -10,7 +10,7 @@ import java.lang.reflect.Modifier; public final class ReflectField extends ReflectMember { /* Those fields are used to modify the value of a static variable. Depending of the current Java version, - * one of them whill be used for this purpose. + * one of them will be used for this purpose. */ private static sun.misc.Unsafe sunMiscUnsafeInstance; private static Field modifiersFieldInFieldClass; diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/LazyOrException.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/LazyOrException.java index 32ea60b..c3b5063 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/LazyOrException.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/LazyOrException.java @@ -6,35 +6,36 @@ import fr.pandacube.lib.util.ThrowableUtil.SupplierException; /** * Represents a lazy loaded value. - * + *

* The value will be computed using the Supplier provided in the * constructor, only the first time the {@link #get()} method is * called. * * @param the type of the enclosed value. + * @param the exception type */ -public class LazyOrException implements SupplierException { +public class LazyOrException implements SupplierException { private T cachedValue; - private final SupplierException supplier; + private final SupplierException supplier; private boolean cached = false; /** * Create a lazy value loader that will call the provided supplier to get the value. * @param s the supplier from which the value is fetched. */ - public LazyOrException(SupplierException s) { + public LazyOrException(SupplierException s) { supplier = Objects.requireNonNull(s); } /** * Get the wrapped value, from cache or from the provider if it is not yet cached. - * + *

* If the provider throws an exception, it will be redirected to the caller as is, and no value will be cached * (the next call to this method will execute the supplier again). */ @Override - public synchronized T get() throws Exception { + public synchronized T get() throws E { if (!cached) set(supplier.get()); return cachedValue; diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java index d539b4b..53f0ba6 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java @@ -5,7 +5,6 @@ import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; -import java.util.function.BiConsumer; /** * Utility class to easily manipulate {@link Throwable}s. @@ -43,7 +42,7 @@ public class ThrowableUtil { * @throws RuntimeException if the provided {@link SupplierException} throws a checked exception. * @param the type of the returned object */ - public static T wrapEx(SupplierException supp) { + public static T wrapEx(SupplierException supp) { try { return supp.get(); } catch (Exception e) { @@ -56,7 +55,7 @@ public class ThrowableUtil { * @param run the {@link RunnableException} to run. * @throws RuntimeException if the provided {@link RunnableException} throws a checked exception. */ - public static void wrapEx(RunnableException run) { + public static void wrapEx(RunnableException run) { try { run.run(); } catch (Exception e) { @@ -74,7 +73,7 @@ public class ThrowableUtil { * @throws RuntimeException if the provided {@link SupplierException} throws a checked exception. * @param the type of the returned object */ - public static T wrapReflectEx(SupplierException supp) { + public static T wrapReflectEx(SupplierException supp) { try { return supp.get(); } catch (Exception e) { @@ -88,7 +87,7 @@ public class ThrowableUtil { * @param run the {@link RunnableException} to run. * @throws RuntimeException if the provided {@link RunnableException} throws a checked exception. */ - public static void wrapReflectEx(RunnableException run) { + public static void wrapReflectEx(RunnableException run) { try { run.run(); } catch (Exception e) { @@ -152,13 +151,13 @@ public class ThrowableUtil { * A supplier that can possibly throw a checked exception. */ @FunctionalInterface - public interface SupplierException { // TODO make exception type generic + public interface SupplierException { /** * Gets a result. * @return a result. - * @throws Exception if implementation failed to run. + * @throws E if implementation failed to run. */ - T get() throws Exception; + T get() throws E; } @@ -167,12 +166,12 @@ public class ThrowableUtil { * A runnable that can possibly throw a checked exception. */ @FunctionalInterface - public interface RunnableException { // TODO make exception type generic + public interface RunnableException { /** * Run any code implemented. - * @throws Exception if implementation failed to run. + * @throws E if implementation failed to run. */ - void run() throws Exception; + void run() throws E; }