From 794f8bb471b16b3a2c3c3101aca62774dc936ebf Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 31 Aug 2024 19:32:00 +0200 Subject: [PATCH] Nuke StackTraceParser --- .../placeholder/crashhandler/Stacktrace.java | 10 +-- .../crashhandler/StacktraceAll.java | 2 +- .../sosengine/base/utility/Miscellaneous.java | 26 ++++++- .../base/utility/StackTraceParser.java | 78 ------------------- .../information/EngineInformation.java | 5 +- .../utility/StackTraceParserTest.java | 61 --------------- .../sosengine/testapp/Main.java | 11 +-- 7 files changed, 35 insertions(+), 158 deletions(-) delete mode 100644 base/src/main/java/de/staropensource/sosengine/base/utility/StackTraceParser.java delete mode 100644 base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/Stacktrace.java b/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/Stacktrace.java index d33bde7..12d2082 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/Stacktrace.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/Stacktrace.java @@ -19,9 +19,9 @@ package de.staropensource.sosengine.base.internal.implementation.placeholder.crashhandler; -import de.staropensource.sosengine.base.implementable.Placeholder; import de.staropensource.sosengine.base.exception.UnexpectedThrowableException; -import de.staropensource.sosengine.base.utility.StackTraceParser; +import de.staropensource.sosengine.base.implementable.Placeholder; +import de.staropensource.sosengine.base.utility.Miscellaneous; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -85,12 +85,10 @@ public final class Stacktrace implements Placeholder { stacktrace.append("\n"); // Append stack trace - StackTraceParser parser = new StackTraceParser(throwable); - stacktrace - .append(parser.getHeader()) + .append(Miscellaneous.getStackTraceHeader(throwable)) .append("\n") - .append(parser.getStackTrace()); + .append(Miscellaneous.getStackTraceAsString(throwable, true)); // Handle throwables which contain other throwables if (throwable instanceof UnexpectedThrowableException unexpectedThrowableException) diff --git a/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/StacktraceAll.java b/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/StacktraceAll.java index b6ee281..d897ae8 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/StacktraceAll.java +++ b/base/src/main/java/de/staropensource/sosengine/base/internal/implementation/placeholder/crashhandler/StacktraceAll.java @@ -63,7 +63,7 @@ public final class StacktraceAll implements Placeholder { .append(thread.isDaemon()) .append(")") .append("\n") - .append(Miscellaneous.stringifyStackTrace(stacktraces.get(thread), false)); + .append(Miscellaneous.getStackTraceAsString(stacktraces.get(thread), false)); } return text.replace("%stacktrace_all%", output.toString().replace("<", "\\<")); diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java index d445db0..e46d7f2 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java @@ -206,15 +206,25 @@ public final class Miscellaneous { return Thread.currentThread().threadId() == 1; } + /** + * Returns the {@code Caused by} message usually found when the JVM prints a throwable. + * + * @return stack trace header + * @since v1-alpha4 + */ + public static @NotNull String getStackTraceHeader(@NotNull Throwable throwable) { + return "Caused by: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage()); + } + /** * Converts an array of {@link StackTraceElement}s into a regular string. * * @param stacktrace array of {@link StackTraceElement}s to convert * @param indent if all lines should be indented with a single {@code \t} character, like in regular stack traces * @return converted stacktrace string - * @since v1-alpha2 + * @since v1-alpha4 */ - public static @NotNull String stringifyStackTrace(@NotNull StackTraceElement[] stacktrace, boolean indent) { + public static @NotNull String getStackTraceAsString(@NotNull StackTraceElement[] stacktrace, boolean indent) { StringBuilder output = new StringBuilder(); for (StackTraceElement element : stacktrace) { @@ -228,4 +238,16 @@ public final class Miscellaneous { return output.toString(); } + + /** + * Converts the stacktrace of a throwable into a regular string. + * + * @param throwable throwable to derive the stacktrace from + * @param indent if all lines should be indented with a single {@code \t} character, like in regular stack traces + * @return converted stacktrace string + * @since v1-alpha4 + */ + public static @NotNull String getStackTraceAsString(@NotNull Throwable throwable, boolean indent) { + return getStackTraceAsString(throwable.getStackTrace(), indent); + } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/StackTraceParser.java b/base/src/main/java/de/staropensource/sosengine/base/utility/StackTraceParser.java deleted file mode 100644 index f5a79de..0000000 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/StackTraceParser.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * STAROPENSOURCE ENGINE SOURCE FILE - * Copyright (c) 2024 The StarOpenSource Engine Authors - * Licensed under the GNU Affero General Public License v3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.staropensource.sosengine.base.utility; - -import lombok.AccessLevel; -import lombok.Getter; -import org.jetbrains.annotations.NotNull; - -/** - * Parses the stacktrace of a {@link Throwable} for use in {@link String}s. - * - * @see Throwable - * @since v1-alpha0 - */ -// TODO deprecate """parser""" and move getHeader() to Miscellaneous class -public final class StackTraceParser { - /** - * Contains the {@link Throwable} to parse. - * - * @see Throwable - * @since v1-alpha0 - * -- GETTER -- - * Returns the {@link Throwable} to parse. - * - * @see Throwable - * @since v1-alpha0 - */ - @Getter(AccessLevel.PROTECTED) - private final @NotNull Throwable throwable; - - /** - * Constructs this class. - * - * @param throwable throwable to parse - * @since v1-alpha0 - */ - public StackTraceParser(@NotNull Throwable throwable) { - this.throwable = throwable; - } - - /** - * Returns the stack trace header. - * It looks like this: {@code Caused by java.lang.NullPointerThrowable: This application can crash!} - * - * @return stack trace header - * @since v1-alpha0 - */ - public @NotNull String getHeader() { - return "Caused by: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage()); - } - - /** - * Returns the stack trace. - * - * @return stack trace - * @since v1-alpha0 - */ - public @NotNull String getStackTrace() { - return Miscellaneous.stringifyStackTrace(throwable.getStackTrace(), true); - } -} diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/information/EngineInformation.java b/base/src/main/java/de/staropensource/sosengine/base/utility/information/EngineInformation.java index c731c10..5516220 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/information/EngineInformation.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/information/EngineInformation.java @@ -22,8 +22,8 @@ package de.staropensource.sosengine.base.utility.information; import de.staropensource.sosengine.base.Engine; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.type.VersionType; +import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.base.utility.PropertiesReader; -import de.staropensource.sosengine.base.utility.StackTraceParser; import lombok.Getter; import org.jetbrains.annotations.NotNull; @@ -427,9 +427,8 @@ public final class EngineInformation { calendar.setTime(date); gitCommitTime = calendar.toZonedDateTime(); } catch (ParseException exception) { - StackTraceParser parser = new StackTraceParser(exception); System.out.println("Unable to load build information: Can't parse \"" + gitParser.getString("git.commit.time") + "\" using format \"yyyy-MM-dd'T'HH:mmZ\""); - System.out.println(parser.getHeader() + "\n" + parser.getStackTrace()); + System.out.println(Miscellaneous.getStackTraceHeader(exception) + "\n" + Miscellaneous.getStackTraceAsString(exception, true)); Engine.getInstance().shutdown(69); return; } diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java deleted file mode 100644 index acf5b0d..0000000 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * STAROPENSOURCE ENGINE SOURCE FILE - * Copyright (c) 2024 The StarOpenSource Engine Authors - * Licensed under the GNU Affero General Public License v3 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.staropensource.sosengine.base.srctests.utility; - -import de.staropensource.sosengine.base.srctests.TestBase; -import de.staropensource.sosengine.base.utility.StackTraceParser; -import org.jetbrains.annotations.NotNull; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Tests the class {@link StackTraceParser}. - */ -@DisplayName("StackTraceParser") -class StackTraceParserTest extends TestBase { - /** - * Tests the method {@code getHeader}. - */ - @ParameterizedTest - @DisplayName("getHeader") - @ValueSource(strings = { - "", - "This is an example message", - "Some description here..." - }) - void testGetHeader(@NotNull String message) { - if (checkCondition()) return; - getLogger().testCall("testGetHeader", message); - - String output; - if (message.isEmpty()) - output = new StackTraceParser(new NumberFormatException()).getHeader(); - else - output = new StackTraceParser(new NumberFormatException(message)).getHeader(); - - String expected = "Caused by: " + NumberFormatException.class.getName(); - if (!message.isEmpty()) - expected += ": " + message; - assertEquals(expected, output, "Output \"" + output + "\" does not match expected output \"" + expected + "\""); - } -} diff --git a/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java b/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java index 974aad7..305dbe3 100644 --- a/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java +++ b/testapp/src/main/java/de/staropensource/sosengine/testapp/Main.java @@ -23,7 +23,7 @@ import de.staropensource.sosengine.base.Engine; import de.staropensource.sosengine.base.annotation.EventListener; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.type.vector.Vec2i; -import de.staropensource.sosengine.base.utility.StackTraceParser; +import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.windowing.WindowingSubsystem; import de.staropensource.sosengine.windowing.event.InputEvent; import de.staropensource.sosengine.windowing.implementable.Window; @@ -148,18 +148,15 @@ public final class Main { StringBuilder message = new StringBuilder(); message.append("Render loop failed on some windows:\n"); - for (Window windowFailed : renderLoopFailures.keySet()) { - StackTraceParser parser = new StackTraceParser(renderLoopFailures.get(windowFailed)); - + for (Window windowFailed : renderLoopFailures.keySet()) message .append("-> ") .append(window) .append(": ") - .append(parser.getHeader()) + .append(Miscellaneous.getStackTraceHeader(renderLoopFailures.get(windowFailed))) .append("\n") - .append(parser.getStackTrace()) + .append(Miscellaneous.getStackTraceAsString(renderLoopFailures.get(windowFailed), true)) .append("\n"); - } logger.crash(message.toString()); } catch (Exception exception) {