From 847cfdcfa8079b33da36f78124d8f5bef11430d2 Mon Sep 17 00:00:00 2001 From: trydofor Date: Tue, 19 Mar 2024 15:08:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20CodeException=20tweak=20stack=20by?= =?UTF-8?q?=20code=20#29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fessional/mirana/pain/CodeException.java | 61 ++++++++++++++++--- .../mirana/pain/CodeExceptionTest.java | 17 ++++++ 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/main/java/pro/fessional/mirana/pain/CodeException.java b/src/main/java/pro/fessional/mirana/pain/CodeException.java index a3353f5..850ec7d 100644 --- a/src/main/java/pro/fessional/mirana/pain/CodeException.java +++ b/src/main/java/pro/fessional/mirana/pain/CodeException.java @@ -2,21 +2,26 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import pro.fessional.mirana.data.CodeEnum; import pro.fessional.mirana.data.Null; import pro.fessional.mirana.evil.TweakingContext; import pro.fessional.mirana.i18n.I18nAware; import pro.fessional.mirana.i18n.I18nString; +import java.util.concurrent.ConcurrentHashMap; + /** - * Readability and performance first, with support for stackless exceptions. + * Readability and performance first exception. + * always enable suppression and support tweak stacktrace, + * ClassStack(1) has higher priority than TweakStack(2) * * @author trydofor * @since 2019-05-29 */ public class CodeException extends RuntimeException implements I18nAware { private static final long serialVersionUID = 19791023L; - public static final TweakingContext TweakStack = new TweakingContext<>(false); + public static final TweakingCodeException TweakStack = new TweakingCodeException(false); private final String code; @@ -27,28 +32,28 @@ public class CodeException extends RuntimeException implements I18nAware { * Constructs a stacked or unstacked exception depending on the Global or Thread setting. */ public CodeException(String code) { - this(TweakStack.current(true), code, null); + this(TweakStack.current(code), code, null); } /** * Constructs a stacked or unstacked exception depending on the Global or Thread setting. */ public CodeException(String code, String message) { - this(TweakStack.current(true), code, message); + this(TweakStack.current(code), code, message); } /** * Constructs a stacked or unstacked exception depending on the Global or Thread setting. */ public CodeException(CodeEnum code) { - this(TweakStack.current(true), code, Null.Objects); + this(TweakStack.current(code), code, Null.Objects); } /** * Constructs a stacked or unstacked exception depending on the Global or Thread setting. */ public CodeException(CodeEnum code, Object... args) { - this(TweakStack.current(true), code, args); + this(TweakStack.current(code), code, args); } /** @@ -62,7 +67,7 @@ public CodeException(boolean stack, String code) { * Constructs a stacked or unstacked exception directly. */ public CodeException(boolean stack, String code, String message) { - super(message == null ? Null.notNull(code) : message, null, stack, stack); + super(message == null ? Null.notNull(code) : message, null, true, stack); if (code == null) { this.code = ""; this.i18nCode = null; @@ -157,4 +162,46 @@ public String getI18nCode() { public Object[] getI18nArgs() { return i18nArgs; } + + /** + * tweaking stacktrace by code and thread + */ + public static class TweakingCodeException extends TweakingContext { + + public final ConcurrentHashMap tweakCode = new ConcurrentHashMap<>(); + + public TweakingCodeException(boolean initDefault) { + super(initDefault); + } + + public boolean current(@Nullable String code) { + if (code == null || code.isEmpty()) { + return TweakStack.current(true); + } + else { + Boolean b = tweakCode.get(code); + return b != null ? b : TweakStack.current(true); + } + } + + public boolean current(@Nullable CodeEnum code) { + return current(code == null ? null : code.getCode()); + } + + public void tweakCode(@NotNull String code, boolean stack) { + tweakCode.put(code, stack); + } + + public void tweakCode(@NotNull CodeEnum code, boolean stack) { + tweakCode.put(code.getCode(), stack); + } + + public void resetCode(@NotNull String code) { + tweakCode.remove(code); + } + + public void resetCode(@NotNull CodeEnum code) { + tweakCode.remove(code.getCode()); + } + } } diff --git a/src/test/java/pro/fessional/mirana/pain/CodeExceptionTest.java b/src/test/java/pro/fessional/mirana/pain/CodeExceptionTest.java index a53bf31..679da4a 100644 --- a/src/test/java/pro/fessional/mirana/pain/CodeExceptionTest.java +++ b/src/test/java/pro/fessional/mirana/pain/CodeExceptionTest.java @@ -1,5 +1,6 @@ package pro.fessional.mirana.pain; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import pro.fessional.mirana.SystemOut; import pro.fessional.mirana.data.Null; @@ -40,4 +41,20 @@ void testNull() { assertEquals(0, noc.getStackTrace().length); assertEquals(0, noc.getSuppressed().length); } + + @Test + void testCode() { + String stack = "stack"; + CodeException.TweakStack.tweakCode(stack, true); + final CodeException st1 = new CodeException(stack); + Assertions.assertTrue(st1.getStackTrace().length > 0); + + CodeException.TweakStack.tweakCode(stack, false); + final CodeException st2 = new CodeException(stack); + Assertions.assertEquals(0, st2.getStackTrace().length); + + CodeException.TweakStack.resetCode(stack); + final CodeException st3 = new CodeException(stack); + Assertions.assertEquals(0, st3.getStackTrace().length); + } }