diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java index 26969336e..afdfa3a26 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java @@ -18,6 +18,7 @@ import org.dromara.hutool.core.collection.set.SetUtil; import org.dromara.hutool.core.collection.set.UniqueKeySet; import org.dromara.hutool.core.comparator.CompareUtil; import org.dromara.hutool.core.convert.Convert; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.map.MapUtil; @@ -1156,7 +1157,7 @@ public class ArrayUtil extends PrimitiveArrayUtil { return (Object[]) obj; } } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } throw new HutoolException(StrUtil.format("[{}] is not Array!", obj.getClass())); diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/classloader/JarClassLoader.java b/hutool-core/src/main/java/org/dromara/hutool/core/classloader/JarClassLoader.java index acc66747b..28d18e5e7 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/classloader/JarClassLoader.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/classloader/JarClassLoader.java @@ -13,6 +13,7 @@ package org.dromara.hutool.core.classloader; import org.dromara.hutool.core.exception.HutoolException; +import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.net.url.URLUtil; import org.dromara.hutool.core.reflect.method.MethodUtil; @@ -73,7 +74,7 @@ public class JarClassLoader extends URLClassLoader { } } } catch (final IOException e) { - throw new HutoolException(e); + throw new IORuntimeException(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java index ce68556cb..1862f8ff2 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/collection/CollUtil.java @@ -26,6 +26,7 @@ import org.dromara.hutool.core.comparator.PinyinComparator; import org.dromara.hutool.core.comparator.PropertyComparator; import org.dromara.hutool.core.convert.CompositeConverter; import org.dromara.hutool.core.convert.Convert; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.func.SerBiConsumer; import org.dromara.hutool.core.func.SerConsumer3; @@ -764,7 +765,7 @@ public class CollUtil { if (null != superclass && collectionType != superclass) { return create(superclass); } - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } return list; diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/DateConverter.java b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/DateConverter.java index 76b028725..8d419ced0 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/DateConverter.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/convert/impl/DateConverter.java @@ -16,6 +16,7 @@ import org.dromara.hutool.core.convert.AbstractConverter; import org.dromara.hutool.core.convert.ConvertException; import org.dromara.hutool.core.date.DateTime; import org.dromara.hutool.core.date.DateUtil; +import org.dromara.hutool.core.date.SqlDateUtil; import org.dromara.hutool.core.text.StrUtil; import java.time.temporal.TemporalAccessor; @@ -113,13 +114,13 @@ public class DateConverter extends AbstractConverter { return date; } if (java.sql.Date.class == targetClass) { - return date.toSqlDate(); + return SqlDateUtil.date(date); } if (java.sql.Time.class == targetClass) { - return new java.sql.Time(date.getTime()); + return SqlDateUtil.time(date); } if (java.sql.Timestamp.class == targetClass) { - return date.toTimestamp(); + return SqlDateUtil.timestamp(date); } throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", targetClass.getName())); diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/DateTime.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/DateTime.java index 6653f1315..9b8ee01f9 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/DateTime.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/DateTime.java @@ -22,7 +22,6 @@ import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.SystemUtil; -import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.Instant; @@ -724,24 +723,6 @@ public class DateTime extends Date { return new Date(this.getTime()); } - /** - * 转为{@link Timestamp} - * - * @return {@link Timestamp} - */ - public Timestamp toTimestamp() { - return new Timestamp(this.getTime()); - } - - /** - * 转为 {@link java.sql.Date} - * - * @return {@link java.sql.Date} - */ - public java.sql.Date toSqlDate() { - return new java.sql.Date(getTime()); - } - /** * 转换为 {@link LocalDateTime} * diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/SqlDateUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/SqlDateUtil.java new file mode 100755 index 000000000..1b87731da --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/SqlDateUtil.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.core.date; + +import org.dromara.hutool.core.lang.Assert; + +import java.sql.Timestamp; +import java.util.Date; + +/** + * {@code java.sql.*}日期时间相关封装
+ * 考虑到JDK9+模块化后,java.sql并非默认引入模块,因此将相关内容单独封装为工具,避免类找不到问题。 + * + * @author looly + * @since 6.0.0 + */ +public class SqlDateUtil { + + /** + * 转为{@link Timestamp} + * + * @param date 日期时间,非空 + * @return {@link Timestamp} + */ + public static Timestamp timestamp(final Date date) { + Assert.notNull(date); + return new Timestamp(date.getTime()); + } + + /** + * /** + * 转为{@link java.sql.Date} + * + * @param date 日期时间,非空 + * @return {@link java.sql.Date} + */ + public static java.sql.Date date(final Date date) { + Assert.notNull(date); + return new java.sql.Date(date.getTime()); + } + + /** + * /** + * 转为{@link java.sql.Time} + * + * @param date 日期时间,非空 + * @return {@link java.sql.Time} + */ + public static java.sql.Time time(final Date date) { + Assert.notNull(date); + return new java.sql.Time(date.getTime()); + } + +} diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java index f8fad0f9c..13dd4093f 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/exception/ExceptionUtil.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.exception; +import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.stream.FastByteArrayOutputStream; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.reflect.ConstructorUtil; @@ -19,6 +20,7 @@ import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.text.CharUtil; +import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.UndeclaredThrowableException; @@ -66,10 +68,13 @@ public class ExceptionUtil { * @return 运行时异常 */ public static RuntimeException wrapRuntime(final Throwable throwable) { + if(throwable instanceof IOException){ + return new IORuntimeException(throwable); + } if (throwable instanceof RuntimeException) { return (RuntimeException) throwable; } - return new RuntimeException(throwable); + return new HutoolException(throwable); } /** diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiConsumer.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiConsumer.java index 21f73a7fa..3d7087323 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiConsumer.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiConsumer.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -62,7 +63,7 @@ public interface SerBiConsumer extends BiConsumer, Serializable { try { accepting(t, u); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiFunction.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiFunction.java index 43f383a3e..6f48bb2a0 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiFunction.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiFunction.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -52,7 +53,7 @@ public interface SerBiFunction extends BiFunction, Serializabl try { return this.applying(t, u); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiPredicate.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiPredicate.java index 60649d442..e2e264327 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiPredicate.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBiPredicate.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -54,7 +55,7 @@ public interface SerBiPredicate extends BiPredicate, Serializable { try { return testing(t, u); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBinaryOperator.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBinaryOperator.java index ad542642e..1089560a3 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBinaryOperator.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerBinaryOperator.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -51,7 +52,7 @@ public interface SerBinaryOperator extends BinaryOperator, Serializable { try { return this.applying(t, u); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer.java index 3018d34b4..f42c787c3 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -47,7 +48,7 @@ public interface SerConsumer extends Consumer, Serializable { try { accepting(t); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer3.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer3.java index 2643e0e59..13cf3aeeb 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer3.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerConsumer3.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -50,7 +51,7 @@ public interface SerConsumer3 extends Serializable { try { accepting(p1, p2, p3); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerFunction.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerFunction.java index 9a779a4f7..38695a2f8 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerFunction.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerFunction.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -48,7 +49,7 @@ public interface SerFunction extends Function, Serializable { try { return applying(t); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerPredicate.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerPredicate.java index 7dbbe2021..4aed6b38b 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerPredicate.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerPredicate.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -51,7 +52,7 @@ public interface SerPredicate extends Predicate, Serializable { try { return testing(t); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerRunnable.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerRunnable.java index 6d25ac96c..45f5cf17a 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerRunnable.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerRunnable.java @@ -13,6 +13,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -57,7 +58,7 @@ public interface SerRunnable extends Runnable, Serializable { try { running(); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerSupplier.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerSupplier.java index ec210a525..f745a41c4 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerSupplier.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerSupplier.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -46,7 +47,7 @@ public interface SerSupplier extends Supplier, Serializable { try { return getting(); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerUnaryOperator.java b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerUnaryOperator.java index 3d581cdc4..07704650a 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/func/SerUnaryOperator.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/func/SerUnaryOperator.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.func; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import java.io.Serializable; @@ -48,7 +49,7 @@ public interface SerUnaryOperator extends UnaryOperator, Serializable { try { return applying(t); } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/LineReader.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/LineReader.java index 5e305d371..e28de009c 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/LineReader.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/LineReader.java @@ -119,7 +119,7 @@ public class LineReader extends ReaderWrapper implements Iterable { try { return readLine(); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } } }; diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java index 778033837..c049fd18f 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.io.file; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.func.SerConsumer; import org.dromara.hutool.core.func.SerFunction; @@ -31,34 +32,37 @@ import java.util.function.Predicate; * 文件读取器 * * @author Looly - * */ public class FileReader extends FileWrapper { private static final long serialVersionUID = 1L; /** * 创建 FileReader - * @param file 文件 + * + * @param file 文件 * @param charset 编码,使用 {@link CharsetUtil} * @return FileReader */ - public static FileReader of(final File file, final Charset charset){ + public static FileReader of(final File file, final Charset charset) { return new FileReader(file, charset); } /** * 创建 FileReader, 编码:{@link FileWrapper#DEFAULT_CHARSET} + * * @param file 文件 * @return FileReader */ - public static FileReader of(final File file){ + public static FileReader of(final File file) { return new FileReader(file, DEFAULT_CHARSET); } // ------------------------------------------------------- Constructor start + /** * 构造 - * @param file 文件 + * + * @param file 文件 * @param charset 编码,使用 {@link CharsetUtil} */ public FileReader(final File file, final Charset charset) { @@ -88,7 +92,7 @@ public class FileReader extends FileWrapper { * @return 内容 * @throws IORuntimeException IO异常 */ - public String readString() throws IORuntimeException{ + public String readString() throws IORuntimeException { // JDK11+不再推荐使用这种方式,推荐使用Files.readString return new String(readBytes(), this.charset); } @@ -96,7 +100,7 @@ public class FileReader extends FileWrapper { /** * 从文件中读取每一行数据 * - * @param 集合类型 + * @param 集合类型 * @param collection 集合 * @return 文件中的每行内容的集合 * @throws IORuntimeException IO异常 @@ -108,15 +112,15 @@ public class FileReader extends FileWrapper { /** * 从文件中读取每一行数据 * - * @param 集合类型 + * @param 集合类型 * @param collection 集合 - * @param predicate 断言,断言为真的加入到提供的集合中 + * @param predicate 断言,断言为真的加入到提供的集合中 * @return 文件中的每行内容的集合 * @throws IORuntimeException IO异常 */ public > T readLines(final T collection, final Predicate predicate) throws IORuntimeException { readLines((SerConsumer) s -> { - if(null == predicate || predicate.test(s)){ + if (null == predicate || predicate.test(s)) { collection.add(s); } }); @@ -130,7 +134,7 @@ public class FileReader extends FileWrapper { * @throws IORuntimeException IO异常 * @since 3.0.9 */ - public void readLines(final SerConsumer lineHandler) throws IORuntimeException{ + public void readLines(final SerConsumer lineHandler) throws IORuntimeException { BufferedReader reader = null; try { reader = FileUtil.getReader(file, charset); @@ -153,7 +157,7 @@ public class FileReader extends FileWrapper { /** * 按照给定的readerHandler读取文件中的数据 * - * @param 读取的结果对象类型 + * @param 读取的结果对象类型 * @param readerHandler Reader处理类 * @return 从文件中read出的数据 * @throws IORuntimeException IO异常 @@ -165,13 +169,7 @@ public class FileReader extends FileWrapper { reader = FileUtil.getReader(this.file, charset); result = readerHandler.applying(reader); } catch (final Exception e) { - if(e instanceof IOException){ - throw new IORuntimeException(e); - } else if(e instanceof RuntimeException){ - throw (RuntimeException)e; - } else{ - throw new HutoolException(e); - } + throw ExceptionUtil.wrapRuntime(e); } finally { IoUtil.closeQuietly(reader); } @@ -216,19 +214,19 @@ public class FileReader extends FileWrapper { /** * 将文件写入流中 * - * @param out 流 + * @param out 流 * @param isCloseOut 是否关闭输出流 * @return 写出的流byte数 * @throws IORuntimeException IO异常 * @since 5.5.2 */ public long writeToStream(final OutputStream out, final boolean isCloseOut) throws IORuntimeException { - try (final FileInputStream in = new FileInputStream(this.file)){ + try (final FileInputStream in = new FileInputStream(this.file)) { return IoUtil.copy(in, out); - }catch (final IOException e) { + } catch (final IOException e) { throw new IORuntimeException(e); - } finally{ - if(isCloseOut){ + } finally { + if (isCloseOut) { IoUtil.closeQuietly(out); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java index 268b21958..daa035df8 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java @@ -271,7 +271,7 @@ public class FileTypeUtil { try { return IoUtil.readHex(in, Math.min(8192, in.available()), false); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/PathDeleter.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/PathDeleter.java index 600a81dd4..48e633176 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/PathDeleter.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/PathDeleter.java @@ -76,7 +76,7 @@ public class PathDeleter { try (final Stream list = Files.list(this.path)){ list.forEach(PathUtil::del); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } } @@ -89,7 +89,7 @@ public class PathDeleter { try { Files.walkFileTree(path, DelVisitor.INSTANCE); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/net/NetUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/net/NetUtil.java index d5d2109f2..9595fad92 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/net/NetUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/net/NetUtil.java @@ -17,6 +17,7 @@ import org.dromara.hutool.core.collection.iter.EnumerationIter; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IoUtil; +import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.split.SplitUtil; import org.dromara.hutool.core.text.CharUtil; @@ -367,9 +368,7 @@ public class NetUtil { throw new HutoolException(e); } - if (networkInterfaces == null) { - throw new HutoolException("Get network interface error!"); - } + Assert.notNull(networkInterfaces, ()-> new HutoolException("Get network interface error!")); final LinkedHashSet ipSet = new LinkedHashSet<>(); diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassScanner.java b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassScanner.java index 68e2a4170..aa7fdad35 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassScanner.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/ClassScanner.java @@ -15,6 +15,8 @@ package org.dromara.hutool.core.reflect; import org.dromara.hutool.core.classloader.ClassLoaderUtil; import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.iter.EnumerationIter; +import org.dromara.hutool.core.exception.ExceptionUtil; +import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.file.FileNameUtil; import org.dromara.hutool.core.io.resource.ResourceUtil; @@ -408,7 +410,7 @@ public class ClassScanner implements Serializable { classesOfLoadError.add(className); } catch (final Throwable e){ if(!this.ignoreLoadError) { - throw new RuntimeException(e); + throw ExceptionUtil.wrapRuntime(e); }else{ classesOfLoadError.add(className); } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodHandleUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodHandleUtil.java index ea783f7c3..1d2a3aed2 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodHandleUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodHandleUtil.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.reflect.method; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.reflect.lookup.LookupUtil; @@ -51,7 +52,7 @@ public class MethodHandleUtil { try { return (T) methodHandle.invokeWithArguments(args); } catch (final Throwable e) { - throw new RuntimeException(e); + throw ExceptionUtil.wrapRuntime(e); } } @@ -114,10 +115,7 @@ public class MethodHandleUtil { } return (T) handle.invokeWithArguments(args); } catch (final Throwable e) { - if(e instanceof RuntimeException){ - throw (RuntimeException)e; - } - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodUtil.java index 65df30852..ee114bef6 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/reflect/method/MethodUtil.java @@ -18,6 +18,7 @@ import org.dromara.hutool.core.classloader.ClassLoaderUtil; import org.dromara.hutool.core.collection.set.SetUtil; import org.dromara.hutool.core.collection.set.UniqueKeySet; import org.dromara.hutool.core.convert.Convert; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.Singleton; @@ -753,7 +754,7 @@ public class MethodUtil { return invoke(isSingleton ? Singleton.get(clazz) : ConstructorUtil.newInstance(clazz), method, args); } } catch (final Exception e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/thread/AsyncUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/thread/AsyncUtil.java index 1ab283d99..6fbe1c6a2 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/thread/AsyncUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/thread/AsyncUtil.java @@ -12,6 +12,7 @@ package org.dromara.hutool.core.thread; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.stream.StreamUtil; @@ -211,7 +212,7 @@ public class AsyncUtil { if (eHandler != null) { return eHandler.apply(ex); } else { - throw new RuntimeException(ex); + throw ExceptionUtil.wrapRuntime(ex); } } }) diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/func/LambdaFactoryTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/func/LambdaFactoryTest.java index 2bea04470..833f00f1f 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/func/LambdaFactoryTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/func/LambdaFactoryTest.java @@ -17,6 +17,7 @@ import lombok.Getter; import lombok.Setter; import lombok.SneakyThrows; import org.dromara.hutool.core.collection.ListUtil; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.reflect.ConstructorUtil; import org.dromara.hutool.core.reflect.lookup.LookupUtil; import org.junit.jupiter.api.Assertions; @@ -322,7 +323,7 @@ public class LambdaFactoryTest { try { return get0(); } catch (final Throwable e) { - throw new RuntimeException(e); + throw ExceptionUtil.wrapRuntime(e); } } } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java index f21030d19..4848aeb24 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java @@ -12,6 +12,7 @@ package org.dromara.hutool.http.client.engine.jdk; +import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.net.url.UrlBuilder; import org.dromara.hutool.core.text.StrUtil; @@ -79,7 +80,7 @@ public class JdkClientEngine implements ClientEngine { } catch (final IOException e) { // 出错后关闭连接 IoUtil.closeQuietly(this); - throw new RuntimeException(e); + throw new IORuntimeException(e); } return sendRedirectIfPossible(message, isAsync); diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/JakartaServletUtil.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/JakartaServletUtil.java index 10570b2c1..86306d090 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/JakartaServletUtil.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/JakartaServletUtil.java @@ -557,7 +557,7 @@ public class JakartaServletUtil { writer.write(text); writer.flush(); } catch (final IOException e) { - throw new HutoolException(e); + throw new IORuntimeException(e); } finally { IoUtil.closeQuietly(writer); } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/ServletUtil.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/ServletUtil.java index 55ced26d1..00fec5926 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/ServletUtil.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/servlet/ServletUtil.java @@ -568,7 +568,7 @@ public class ServletUtil { writer.write(text); writer.flush(); } catch (final IOException e) { - throw new HutoolException(e); + throw new IORuntimeException(e); } finally { IoUtil.closeQuietly(writer); } diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/IssueI6MBS5Test.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/IssueI6MBS5Test.java index 3ba626d68..e608b46bf 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/IssueI6MBS5Test.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/IssueI6MBS5Test.java @@ -1,5 +1,6 @@ package org.dromara.hutool.poi.excel; +import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.poi.excel.cell.CellUtil; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -42,7 +43,7 @@ public class IssueI6MBS5Test { CellUtil.setComment(cell, "commonText", "ascend", null); workbook.write(Files.newOutputStream(file.toPath())); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } } } diff --git a/hutool-swing/src/main/java/org/dromara/hutool/swing/clipboard/ClipboardUtil.java b/hutool-swing/src/main/java/org/dromara/hutool/swing/clipboard/ClipboardUtil.java index 37f745395..84b9c4d20 100644 --- a/hutool-swing/src/main/java/org/dromara/hutool/swing/clipboard/ClipboardUtil.java +++ b/hutool-swing/src/main/java/org/dromara/hutool/swing/clipboard/ClipboardUtil.java @@ -12,16 +12,11 @@ package org.dromara.hutool.swing.clipboard; -import org.dromara.hutool.core.exception.HutoolException; +import org.dromara.hutool.core.exception.ExceptionUtil; import java.awt.Image; import java.awt.Toolkit; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.ClipboardOwner; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.Transferable; -import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.datatransfer.*; import java.io.IOException; /** @@ -82,7 +77,7 @@ public class ClipboardUtil { try { return content.getTransferData(flavor); } catch (final UnsupportedFlavorException | IOException e) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } return null; diff --git a/hutool-swing/src/main/java/org/dromara/hutool/swing/img/FontUtil.java b/hutool-swing/src/main/java/org/dromara/hutool/swing/img/FontUtil.java index 965ced669..8be05d278 100644 --- a/hutool-swing/src/main/java/org/dromara/hutool/swing/img/FontUtil.java +++ b/hutool-swing/src/main/java/org/dromara/hutool/swing/img/FontUtil.java @@ -12,6 +12,7 @@ package org.dromara.hutool.swing.img; +import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.io.IORuntimeException; @@ -76,7 +77,7 @@ public class FontUtil { try { return Font.createFont(Font.TYPE1_FONT, fontFile); } catch (final Exception e1) { - throw new HutoolException(e); + throw ExceptionUtil.wrapRuntime(e); } } catch (final IOException e) { throw new IORuntimeException(e); @@ -98,7 +99,7 @@ public class FontUtil { try { return Font.createFont(Font.TYPE1_FONT, fontStream); } catch (final Exception e1) { - throw new HutoolException(e1); + throw ExceptionUtil.wrapRuntime(e1); } } catch (final IOException e) { throw new IORuntimeException(e); diff --git a/hutool-swing/src/main/java/org/dromara/hutool/swing/img/ImgMetaUtil.java b/hutool-swing/src/main/java/org/dromara/hutool/swing/img/ImgMetaUtil.java index 3bc266d3a..39ad484b2 100644 --- a/hutool-swing/src/main/java/org/dromara/hutool/swing/img/ImgMetaUtil.java +++ b/hutool-swing/src/main/java/org/dromara/hutool/swing/img/ImgMetaUtil.java @@ -47,7 +47,7 @@ public class ImgMetaUtil { } catch (final ImageProcessingException e) { throw new HutoolException(e); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException(e); } return getOrientation(metadata); }