diff --git a/src/main/java/xyz/snaker/hiss/sneaky/Sneaky.java b/src/main/java/xyz/snaker/hiss/sneaky/Sneaky.java index 74f3c85..558adb6 100644 --- a/src/main/java/xyz/snaker/hiss/sneaky/Sneaky.java +++ b/src/main/java/xyz/snaker/hiss/sneaky/Sneaky.java @@ -2,7 +2,7 @@ import org.jetbrains.annotations.NotNull; import sun.misc.Unsafe; -import xyz.snaker.hiss.thread.UncaughtExceptionThread; +import xyz.snaker.hiss.thread.PeripheralException; import java.lang.reflect.Field; import java.nio.ByteOrder; @@ -56,7 +56,7 @@ public long right(long value, int bytes) field.setAccessible(true); theUnsafe = (Unsafe) field.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { - UncaughtExceptionThread.createAndRun("Failed to get theUnsafe", e); + PeripheralException.invoke("Failed to get theUnsafe", e); } } diff --git a/src/main/java/xyz/snaker/hiss/thread/PeripheralException.java b/src/main/java/xyz/snaker/hiss/thread/PeripheralException.java new file mode 100644 index 0000000..658eb24 --- /dev/null +++ b/src/main/java/xyz/snaker/hiss/thread/PeripheralException.java @@ -0,0 +1,79 @@ +package xyz.snaker.hiss.thread; + +import xyz.snaker.hiss.logger.Logger; +import xyz.snaker.hiss.logger.Loggers; + +import java.util.function.Function; + +/** + * Created by SnakerBone on 19/08/2023 + **/ +public class PeripheralException extends Thread +{ + private static final Logger LOGGER = Loggers.getLogger(); + private static final Function ERROR_TEMPLATE = "Peripheral Exception Thrown: %s"::formatted; + private static final String NO_ERROR_MESSAGE = ERROR_TEMPLATE.apply("No message specified"); + + private final String message; + private final UncaughtExceptionHandler handler; + private final Exception cause; + + private PeripheralException(String message, Exception cause) + { + this.message = message; + this.handler = (t, e) -> LOGGER.errorf(ERROR_TEMPLATE.apply(message)); + this.cause = cause; + } + + private PeripheralException(Exception cause) + { + this(cause.getMessage(), cause); + } + + private PeripheralException(String message) + { + this(message, new Exception()); + } + + private PeripheralException() + { + this(NO_ERROR_MESSAGE, new Exception()); + } + + public static void invoke(String message, Exception cause) + { + new PeripheralException(message, cause).start(); + } + + public static void invoke(Exception cause) + { + new PeripheralException(cause).start(); + } + + public static void invoke(String message) + { + new PeripheralException(message).start(); + } + + public static void invoke() + { + new PeripheralException().start(); + } + + @Override + @SuppressWarnings({"ConstantValue", "CallToPrintStackTrace"}) + public void run() + { + cause.printStackTrace(); + + if (true) { + throw new RuntimeException(message); + } + } + + @Override + public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) + { + super.setUncaughtExceptionHandler(handler); + } +} diff --git a/src/main/java/xyz/snaker/hiss/thread/UncaughtExceptionThread.java b/src/main/java/xyz/snaker/hiss/thread/UncaughtExceptionThread.java deleted file mode 100644 index eab1f75..0000000 --- a/src/main/java/xyz/snaker/hiss/thread/UncaughtExceptionThread.java +++ /dev/null @@ -1,71 +0,0 @@ -package xyz.snaker.hiss.thread; - -import xyz.snaker.hiss.logger.Logger; -import xyz.snaker.hiss.logger.Loggers; - -/** - * Created by SnakerBone on 19/08/2023 - **/ -public class UncaughtExceptionThread extends Thread -{ - private static final Logger LOGGER = Loggers.getLogger(); - - private final UncaughtExceptionHandler handler; - private final String message; - private final boolean breakpoint; - - public UncaughtExceptionThread(Exception cause) - { - this.message = cause.getMessage(); - this.handler = (thread, throwable) -> LOGGER.errorf("[]: []", cause.getClass().getName(), message); - this.breakpoint = false; - } - - public UncaughtExceptionThread(Exception cause, boolean breakpoint) - { - this.message = cause.getMessage(); - this.handler = (thread, throwable) -> LOGGER.errorf("[]: []", cause.getClass().getName(), message); - this.breakpoint = breakpoint; - } - - public UncaughtExceptionThread(String message, Exception cause) - { - this.message = message; - this.handler = (thread, throwable) -> LOGGER.errorf("[]: []", cause.getClass().getName(), message); - this.breakpoint = false; - } - - public UncaughtExceptionThread(String message, Exception cause, boolean breakpoint) - { - this.message = message; - this.handler = (thread, throwable) -> LOGGER.errorf("[]: []", cause.getClass().getName(), message); - this.breakpoint = breakpoint; - } - - public static void createAndRun(Exception cause) - { - UncaughtExceptionThread thread = new UncaughtExceptionThread(cause); - thread.start(); - } - - public static void createAndRun(String message, Exception cause) - { - UncaughtExceptionThread thread = new UncaughtExceptionThread(message, cause); - thread.start(); - } - - @Override - @SuppressWarnings("ConstantValue") - public void run() - { - if (true) { - throw new RuntimeException(message); - } - } - - @Override - public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) - { - super.setUncaughtExceptionHandler(handler); - } -} diff --git a/src/main/java/xyz/snaker/hiss/utility/Strings.java b/src/main/java/xyz/snaker/hiss/utility/Strings.java index e8ff2fa..8e8a411 100644 --- a/src/main/java/xyz/snaker/hiss/utility/Strings.java +++ b/src/main/java/xyz/snaker/hiss/utility/Strings.java @@ -2,6 +2,7 @@ import xyz.snaker.hiss.logger.Logger; import xyz.snaker.hiss.logger.Loggers; +import xyz.snaker.hiss.thread.PeripheralException; import java.util.Arrays; import java.util.Iterator; @@ -21,13 +22,41 @@ public class Strings public static final String EMPTY = ""; public static final String LINE_SEPARATOR = System.lineSeparator(); + /** + * Removes an extension from a string + *

+ * For example these strings: + *

+     *     object.json
+     *     image.png
+     *     text.txt
+     * 
+ * Would be converted to: + *
+     *     object
+     *     image
+     *     text
+     * 
+ * + * @param string The string to strip the extension from + * @return The stripped string + */ + public static String stripExtension(String string) + { + if (!string.contains(".")) { + PeripheralException.invoke(new RuntimeException("String '%s' does not seem to contain an extension")); + } + + return string.substring(0, string.lastIndexOf('.')); + } + /** * Gives a string consisting of the elements of a given array of strings, each separated by a given separator - * string. + * string * - * @param pieces the strings to join - * @param separator the separator - * @return the joined string + * @param pieces The strings to join + * @param separator The separator + * @return The joined string */ public static String join(String[] pieces, String separator) { @@ -36,11 +65,11 @@ public static String join(String[] pieces, String separator) /** * Gives a string consisting of the string representations of the elements of a given array of objects, - * each separated by a given separator string. + * each separated by a given separator string * - * @param pieces the elements whose string representations are to be joined - * @param separator the separator - * @return the joined string + * @param pieces The elements whose string representations are to be joined + * @param separator The separator + * @return The joined string */ public static String join(Iterable pieces, String separator) {