Skip to content

Commit

Permalink
1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
SnakerBone committed Apr 29, 2024
1 parent 57da6b7 commit c04d4a9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/main/java/xyz/snaker/hiss/sneaky/Sneaky.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
79 changes: 79 additions & 0 deletions src/main/java/xyz/snaker/hiss/thread/PeripheralException.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}
}
71 changes: 0 additions & 71 deletions src/main/java/xyz/snaker/hiss/thread/UncaughtExceptionThread.java

This file was deleted.

45 changes: 37 additions & 8 deletions src/main/java/xyz/snaker/hiss/utility/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
* <p>
* For example these strings:
* <pre>
* object.json
* image.png
* text.txt
* </pre>
* Would be converted to:
* <pre>
* object
* image
* text
* </pre>
*
* @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)
{
Expand All @@ -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<String> pieces, String separator)
{
Expand Down

0 comments on commit c04d4a9

Please sign in to comment.