From 94d34faffda078a7f34b232f5ec9c0c93bdfb548 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 18 Oct 2023 10:54:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20BaseRuntimeException=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=9F=BA=E7=A1=80=E8=BF=90=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=8E=9F=20BaseException=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=9F=BA=E7=A1=80=E6=A3=80=E6=9F=A5=E5=9E=8B?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/exception/BaseException.java | 2 +- .../exception/BaseRuntimeException.java | 58 +++++++++++++++++++ .../zhouxy/plusone/commons/SerialTests.java | 23 ++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java index 3b13195..7e15ed4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java @@ -27,7 +27,7 @@ import java.util.Objects; * @author ZhouXY */ public abstract class BaseException - extends RuntimeException + extends Exception implements IWithCode { private static final long serialVersionUID = -2546365325001947203L; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java new file mode 100644 index 0000000..3212d6a --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package xyz.zhouxy.plusone.commons.exception; + +import xyz.zhouxy.plusone.commons.base.IWithCode; + +import javax.annotation.Nonnull; +import java.util.Objects; + +/** + * 带错误码的异常。 + * + * @author ZhouXY + */ +public abstract class BaseRuntimeException + extends RuntimeException + implements IWithCode { + + private static final long serialVersionUID = -6345888403567792664L; + + @Nonnull + private final String code; + + protected BaseRuntimeException(String code, String msg) { + super(msg); + this.code = Objects.requireNonNull(code); + } + + protected BaseRuntimeException(String code, Throwable cause) { + super(cause); + this.code = Objects.requireNonNull(code); + } + + protected BaseRuntimeException(String code, String msg, Throwable cause) { + super(msg, cause); + this.code = Objects.requireNonNull(code); + } + + @Nonnull + @Override + public final String getCode() { + return this.code; + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java new file mode 100644 index 0000000..c556af6 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java @@ -0,0 +1,23 @@ +package xyz.zhouxy.plusone.commons; + +import java.io.ObjectStreamClass; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException; + +@Slf4j +class SerialTests { + + @Test + void testSerialVersionUID() { + long uid = getSerialVersionUID(BaseRuntimeException.class); + log.info("\n private static final long serialVersionUID = {}L;", uid); + } + + private long getSerialVersionUID(Class cl) { + ObjectStreamClass c = ObjectStreamClass.lookup(cl); + return c.getSerialVersionUID(); + } +}