From 6eadd01117d79e9a940b5291c701f989e84fb81d Mon Sep 17 00:00:00 2001 From: calvin1978 Date: Tue, 24 Jan 2017 11:15:46 +0800 Subject: [PATCH] format code --- .../modules/utils/base/BooleanUtil.java | 1 - .../modules/utils/base/EnumUtil.java | 5 ++- .../modules/utils/base/ExceptionUtil.java | 28 ++++++++++++---- .../modules/utils/base/Platforms.java | 8 ++--- .../modules/utils/base/PropertiesUtil.java | 2 +- .../modules/utils/collection/ArrayUtil.java | 2 +- .../utils/collection/CollectionUtil.java | 4 +-- .../modules/utils/collection/ListUtil.java | 2 +- .../modules/utils/collection/MapUtil.java | 2 +- .../modules/utils/collection/QueueUtil.java | 2 +- .../modules/utils/collection/SetUtil.java | 2 +- .../modules/utils/collection/type/Pair.java | 23 ++++++++----- .../modules/utils/concurrent/ThreadUtil.java | 32 +++++++++++++++++-- .../concurrent/threadpool/ThreadPoolUtil.java | 10 +++--- .../utils/concurrent/throttle/Sampler.java | 2 ++ .../modules/utils/io/FilePathUtil.java | 7 ++-- .../modules/utils/io/FileTreeWalker.java | 3 +- .../springside/modules/utils/io/FileUtil.java | 7 ++-- .../modules/utils/io/GeneralResourceUtil.java | 2 +- .../springside/modules/utils/io/IOUtil.java | 5 +-- .../modules/utils/io/ResourceUtil.java | 2 +- .../modules/utils/mapper/BeanMapper.java | 2 +- .../springside/modules/utils/net/IPUtil.java | 2 +- .../springside/modules/utils/net/NetUtil.java | 2 +- .../modules/utils/number/MathUtil.java | 8 ++--- .../modules/utils/number/NumberUtil.java | 9 +++--- .../modules/utils/number/RandomUtil.java | 2 +- .../modules/utils/reflect/ClassUtil.java | 15 ++++++++- .../utils/reflect/FastMethodInvoker.java | 10 +++--- .../modules/utils/reflect/MethodInvoker.java | 2 +- .../modules/utils/reflect/ReflectionUtil.java | 13 ++++---- .../modules/utils/text/Charsets.java | 2 +- .../modules/utils/text/CsvUtil.java | 2 +- .../modules/utils/text/EncodeUtil.java | 2 +- .../modules/utils/text/EscapeUtil.java | 2 +- .../modules/utils/text/HashUtil.java | 2 +- .../modules/utils/text/MoreStringUtil.java | 14 ++++---- .../modules/utils/text/TextValidator.java | 2 +- .../modules/utils/time/ClockUtil.java | 2 +- .../modules/utils/time/DateFormatUtil.java | 2 +- .../modules/utils/time/DateUtil.java | 2 +- .../modules/utils/base/ExceptionUtilTest.java | 15 +++++++-- .../utils/concurrent/ThreadUtilTest.java | 16 +++++++++- .../modules/utils/reflect/ClassUtilTest.java | 5 +++ 44 files changed, 183 insertions(+), 101 deletions(-) diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/BooleanUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/base/BooleanUtil.java index d72ec4b98..55e78fc11 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/base/BooleanUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/base/BooleanUtil.java @@ -67,5 +67,4 @@ public static boolean or(final boolean... array) { return BooleanUtils.or(array); } - } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/EnumUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/base/EnumUtil.java index b425fea5c..a47c9283a 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/base/EnumUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/base/EnumUtil.java @@ -10,13 +10,12 @@ * @author calvin * */ -public abstract class EnumUtil { +public class EnumUtil { /** * 将若干个枚举值转换为long,用于使用long保存多个选项的情况. */ - public static > long generateBits(final Class enumClass, - final Iterable values) { + public static > long generateBits(final Class enumClass, final Iterable values) { return EnumUtils.generateBitVector(enumClass, values); } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/ExceptionUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/base/ExceptionUtil.java index c7f6c1b56..fa418848a 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/base/ExceptionUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/base/ExceptionUtil.java @@ -21,7 +21,7 @@ * * @author calvin */ -public abstract class ExceptionUtil { +public class ExceptionUtil { private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0]; @@ -43,14 +43,13 @@ public abstract class ExceptionUtil { */ public static RuntimeException unchecked(Throwable t) { - Throwable unwrapped = unwrap(t); - if (unwrapped instanceof RuntimeException) { - throw (RuntimeException) unwrapped; + if (t instanceof RuntimeException) { + throw (RuntimeException) t; } - if (unwrapped instanceof Error) { - throw (Error) unwrapped; + if (t instanceof Error) { + throw (Error) t; } - throw new UncheckedException(unwrapped); + throw new UncheckedException(t); } /** @@ -71,6 +70,21 @@ public static Throwable unwrap(Throwable t) { return t; } + /** + * 组合unchecked与unwrap的效果 + */ + public static RuntimeException uncheckedAndWrap(Throwable t) { + + Throwable unwrapped = unwrap(t); + if (unwrapped instanceof RuntimeException) { + throw (RuntimeException) unwrapped; + } + if (unwrapped instanceof Error) { + throw (Error) unwrapped; + } + throw new UncheckedException(unwrapped); + } + /** * 将StackTrace[]转换为String, 供Logger或e.printStackTrace()外的其他地方使用. * diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java b/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java index 97636b608..d125627f9 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/base/Platforms.java @@ -12,13 +12,13 @@ * * @author calvin */ -public abstract class Platforms { +public class Platforms { // 文件路径分隔符 public static final String FILE_PATH_SEPARATOR = File.separator; public static final char FILE_PATH_SEPARATOR_CHAR = File.separatorChar; - public static final char WINDOWS_FILE_PATH_SEPARATOR_CHAR='\\'; - public static final char LINUX_FILE_PATH_SEPARATOR_CHAR='/'; + public static final char WINDOWS_FILE_PATH_SEPARATOR_CHAR = '\\'; + public static final char LINUX_FILE_PATH_SEPARATOR_CHAR = '/'; // ClassPath分隔符 public static final String CLASS_PATH_SEPARATOR = File.pathSeparator; @@ -66,7 +66,7 @@ public static int getPid() { if (split.length != 2) { return -1; } - + try { return Integer.parseInt(split[0]); } catch (Exception e) { diff --git a/modules/utils/src/main/java/org/springside/modules/utils/base/PropertiesUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/base/PropertiesUtil.java index 884c18ac7..74559ac71 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/base/PropertiesUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/base/PropertiesUtil.java @@ -21,7 +21,7 @@ * * @author calvin */ -public abstract class PropertiesUtil { +public class PropertiesUtil { private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/ArrayUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/ArrayUtil.java index 73960ed4d..8b7a1fffe 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/ArrayUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/ArrayUtil.java @@ -27,7 +27,7 @@ public class ArrayUtil { /** * 将传入的数组乱序 */ - public final static T[] shuffle(T[] array) { + public static T[] shuffle(T[] array) { List list = new ArrayList(array.length); Collections.addAll(list, array); Collections.shuffle(list); diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/CollectionUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/CollectionUtil.java index eac5991cc..c37186899 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/CollectionUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/CollectionUtil.java @@ -13,7 +13,6 @@ import org.springside.modules.utils.collection.type.Pair; -import com.google.common.annotations.Beta; import com.google.common.collect.Iterables; import com.google.common.collect.Iterators; import com.google.common.collect.Ordering; @@ -29,8 +28,7 @@ * * @see com.google.common.collect.Ordering */ -@Beta -public abstract class CollectionUtil { +public class CollectionUtil { /** * 判断是否为空. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/ListUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/ListUtil.java index 411277778..b4a5dd1a3 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/ListUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/ListUtil.java @@ -27,7 +27,7 @@ * @author calvin */ @SuppressWarnings("unchecked") -public abstract class ListUtil { +public class ListUtil { /** * 判断是否为空. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/MapUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/MapUtil.java index 37b0dba02..dc98319ff 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/MapUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/MapUtil.java @@ -50,7 +50,7 @@ * @author calvin */ @SuppressWarnings("unchecked") -public abstract class MapUtil { +public class MapUtil { /** * 判断是否为空. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/QueueUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/QueueUtil.java index f64c51fcd..454f54765 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/QueueUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/QueueUtil.java @@ -19,7 +19,7 @@ * * 2.特殊类型Queue:LIFO的Stack, LRU的Queue */ -public abstract class QueueUtil { +public class QueueUtil { /** * 创建ArrayDeque (JDK无ArrayQueue) diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/SetUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/SetUtil.java index 921b6e274..6ffd82c94 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/SetUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/SetUtil.java @@ -19,7 +19,7 @@ * * 2. 集合运算函数(交集,并集等,from guava) */ -public abstract class SetUtil { +public class SetUtil { /** * 根据等号左边的类型,构造类型正确的HashSet. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/collection/type/Pair.java b/modules/utils/src/main/java/org/springside/modules/utils/collection/type/Pair.java index 05c470de2..d4eaef2b9 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/collection/type/Pair.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/collection/type/Pair.java @@ -46,26 +46,33 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } Pair other = (Pair) obj; if (first == null) { - if (other.first != null) + if (other.first != null) { return false; - } else if (!first.equals(other.first)) + } + } else if (!first.equals(other.first)) { return false; + } if (second == null) { - if (other.second != null) + if (other.second != null) { return false; - } else if (!second.equals(other.second)) + } + } else if (!second.equals(other.second)) { return false; + } return true; } - + @Override public String toString() { return "Pair [first=" + first + ", second=" + second + "]"; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/ThreadUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/ThreadUtil.java index cde6643bc..4013854ab 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/ThreadUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/ThreadUtil.java @@ -14,7 +14,7 @@ * * 1. 处理了InterruptedException的sleep */ -public abstract class ThreadUtil { +public class ThreadUtil { /** * sleep等待, 单位为毫秒, 已捕捉并处理InterruptedException. @@ -50,7 +50,7 @@ public static void handleInterruptedException() { */ public static String getCallerClass() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); - if (stacktrace.length >= 3) { + if (stacktrace.length >= 4) { StackTraceElement element = stacktrace[3]; return element.getClassName(); } else { @@ -63,7 +63,7 @@ public static String getCallerClass() { */ public static String getCallerMethod() { StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); - if (stacktrace.length >= 3) { + if (stacktrace.length >= 4) { StackTraceElement element = stacktrace[3]; return element.getClassName() + '.' + element.getMethodName() + "()"; } else { @@ -71,4 +71,30 @@ public static String getCallerMethod() { } } + /** + * 通过StackTrace,获得调用者的类名. + */ + public static String getCurrentClass() { + StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); + if (stacktrace.length >= 3) { + StackTraceElement element = stacktrace[2]; + return element.getClassName(); + } else { + return StringUtils.EMPTY; + } + } + + /** + * 通过StackTrace,获得当前方法的"类名.方法名()" + */ + public static String getCurrentMethod() { + StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); + if (stacktrace.length >= 3) { + StackTraceElement element = stacktrace[2]; + return element.getClassName() + '.' + element.getMethodName() + "()"; + } else { + return StringUtils.EMPTY; + } + } + } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/threadpool/ThreadPoolUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/threadpool/ThreadPoolUtil.java index d7913791f..853f2eea2 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/threadpool/ThreadPoolUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/threadpool/ThreadPoolUtil.java @@ -27,8 +27,6 @@ */ public class ThreadPoolUtil { - - /** * 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法. * @@ -93,16 +91,16 @@ public static Runnable safeRunnable(@NotNull Runnable runnable) { * 保证不会有Exception抛出到线程池的Runnable包裹类,防止用户没有捕捉异常导致中断了线程池中的线程, 使得SchedulerService无法执行. 在无法控制第三方包的Runnalbe实现时,使用本类进行包裹. */ public static class SafeRunnable implements Runnable { - + private static Logger logger = LoggerFactory.getLogger(SafeRunnable.class); - + private Runnable runnable; - + public SafeRunnable(Runnable runnable) { Validate.notNull(runnable); this.runnable = runnable; } - + @Override public void run() { try { diff --git a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/throttle/Sampler.java b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/throttle/Sampler.java index e91071c94..0f20f6a58 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/concurrent/throttle/Sampler.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/concurrent/throttle/Sampler.java @@ -54,6 +54,7 @@ public boolean select() { * 采样率为100时,总是返回true */ public static class AlwaysSampler extends Sampler { + @Override public boolean select() { return true; } @@ -63,6 +64,7 @@ public boolean select() { * 采样率为0时,总是返回false */ public static class NeverSampler extends Sampler { + @Override public boolean select() { return false; } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/FilePathUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/io/FilePathUtil.java index f1162fa09..74722b244 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/FilePathUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/FilePathUtil.java @@ -11,7 +11,7 @@ * * @author calvin */ -public abstract class FilePathUtil { +public class FilePathUtil { /** * 在Windows环境里,兼容Windows上的路径分割符,将 '/' 转回 '\' @@ -70,10 +70,11 @@ public static String getParentPath(String path) { parentPath = MoreStringUtil.removeEnd(parentPath, Platforms.FILE_PATH_SEPARATOR_CHAR); int idx = parentPath.lastIndexOf(Platforms.FILE_PATH_SEPARATOR_CHAR); - if (idx >= 0) + if (idx >= 0) { parentPath = parentPath.substring(0, idx + 1); - else + } else { parentPath = Platforms.FILE_PATH_SEPARATOR; + } return parentPath; } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/FileTreeWalker.java b/modules/utils/src/main/java/org/springside/modules/utils/io/FileTreeWalker.java index bc82e2bf1..9da1dbd32 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/FileTreeWalker.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/FileTreeWalker.java @@ -61,7 +61,8 @@ public static List listFileWithRegexFileName(final File rootDir, final Str * 如 ("/a/b/hello.txt", "he.*\.text") 将被返回 */ public static List listFileWithAntPath(final File rootDir, final String antPathPattern) { - return Files.fileTreeTraverser().preOrderTraversal(rootDir).filter(new AntPathFilter(FilePathUtil.contact(rootDir.getAbsolutePath(), antPathPattern))).toList(); + return Files.fileTreeTraverser().preOrderTraversal(rootDir) + .filter(new AntPathFilter(FilePathUtil.contact(rootDir.getAbsolutePath(), antPathPattern))).toList(); } /** diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/FileUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/io/FileUtil.java index e16f44681..241769dba 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/FileUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/FileUtil.java @@ -32,7 +32,7 @@ * * @author calvin */ -public abstract class FileUtil { +public class FileUtil { //////// 文件读写////// @@ -148,8 +148,9 @@ public static void copyDir(@NotNull File from, @NotNull File to) throws IOExcept if (files != null) { for (int i = 0; i < files.length; i++) { String name = files[i].getName(); - if (".".equals(name) || "..".equals(name)) + if (".".equals(name) || "..".equals(name)) { continue; + } copy(files[i], new File(to, name)); } } @@ -182,7 +183,7 @@ public static void moveDir(@NotNull File from, @NotNull File to) throws IOExcept copyDir(from, to); deleteDir(from); if (from.exists()) { - throw new IOException("Failed to delete original directory '" + from + "' after copy to '" + to + "'"); + throw new IOException("Failed to delete original directory '" + from + "' after copy to '" + to + '\''); } } } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/GeneralResourceUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/io/GeneralResourceUtil.java index 1747c94c9..58fc1e893 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/GeneralResourceUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/GeneralResourceUtil.java @@ -13,7 +13,7 @@ * @author calvin */ @Beta -public abstract class GeneralResourceUtil { +public class GeneralResourceUtil { public static final String CLASSPATH = "classpath://"; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/IOUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/io/IOUtil.java index bd4c51faa..10f35c3c1 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/IOUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/IOUtil.java @@ -16,8 +16,6 @@ import org.springside.modules.utils.io.type.StringBuilderWriter; import org.springside.modules.utils.text.Charsets; -import com.google.common.annotations.Beta; - /** * IO Stream/Reader相关工具集 * @@ -36,8 +34,7 @@ * * @author calvin */ -@Beta -public abstract class IOUtil { +public class IOUtil { private static final Logger logger = LoggerFactory.getLogger(IOUtil.class); private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/io/ResourceUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/io/ResourceUtil.java index 011cce976..2554f91b7 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/io/ResourceUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/io/ResourceUtil.java @@ -35,7 +35,7 @@ * * 如果有多个同名资源,除非调用getResources()获取全部资源,否则在URLClassLoader中按ClassPath顺序打开第一个命中的Jar文件. */ -public abstract class ResourceUtil { +public class ResourceUtil { // 打开单个文件//// /** diff --git a/modules/utils/src/main/java/org/springside/modules/utils/mapper/BeanMapper.java b/modules/utils/src/main/java/org/springside/modules/utils/mapper/BeanMapper.java index 20176a14f..899109143 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/mapper/BeanMapper.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/mapper/BeanMapper.java @@ -20,7 +20,7 @@ * * 注意: 需要参考本模块的POM文件,显式引用orika. */ -public abstract class BeanMapper { +public class BeanMapper { private static MapperFacade mapper; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/net/IPUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/net/IPUtil.java index 8689e56bb..5db1b2e58 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/net/IPUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/net/IPUtil.java @@ -21,7 +21,7 @@ * * @author calvin */ -public abstract class IPUtil { +public class IPUtil { /** * 从InetAddress转化到int, 传输和存储时, 用int代表InetAddress是最小的开销. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/net/NetUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/net/NetUtil.java index 2864cc1d2..723d4cde5 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/net/NetUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/net/NetUtil.java @@ -30,7 +30,7 @@ * */ @Beta -public abstract class NetUtil { +public class NetUtil { private static Logger logger = LoggerFactory.getLogger(NetUtil.class); diff --git a/modules/utils/src/main/java/org/springside/modules/utils/number/MathUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/number/MathUtil.java index 437cde239..af7fcf450 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/number/MathUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/number/MathUtil.java @@ -2,7 +2,6 @@ import java.math.RoundingMode; -import com.google.common.annotations.Beta; import com.google.common.math.IntMath; import com.google.common.math.LongMath; @@ -11,15 +10,12 @@ * * 1. 2的倍数的计算 * - * 2. 其他函数如最大公约数 - * - * 3. 安全的取模 + * 2. 其他函数如最大公约数, 乘方,开方,安全的取模,可控制取整方向的相除等。 * * @author calvin * */ -@Beta -public abstract class MathUtil { +public class MathUtil { /////// 2 的倍数的计算//// diff --git a/modules/utils/src/main/java/org/springside/modules/utils/number/NumberUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/number/NumberUtil.java index 0f0890e6b..3caf3ddf2 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/number/NumberUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/number/NumberUtil.java @@ -17,7 +17,7 @@ * * 3.10机制/16进制字符串 与 原始类型数字/数字对象 的双向转换(参考Common Lang自写) */ -public abstract class NumberUtil { +public class NumberUtil { ///////////// bytes[] 与原始类型数字转换 /////// @@ -255,11 +255,12 @@ public static String to2DigitString(double d) { /////////// 杂项 /////// /** - * 安全的将小于Integer.MAX的long转为int,否则抛出错误 + * 安全的将小于Integer.MAX的long转为int,否则抛出IllegalArgumentException异常 */ - public static int toInt32(long x) throws IllegalArgumentException { - if ((int) x == x) + public static int toInt32(long x) { + if ((int) x == x){ return (int) x; + } throw new IllegalArgumentException("Int " + x + " out of range"); } } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/number/RandomUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/number/RandomUtil.java index c561ff68b..024ca9ac9 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/number/RandomUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/number/RandomUtil.java @@ -20,7 +20,7 @@ * * @author calvin */ -public abstract class RandomUtil { +public class RandomUtil { /////////////////// 获取Random实例////////////// /** diff --git a/modules/utils/src/main/java/org/springside/modules/utils/reflect/ClassUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/reflect/ClassUtil.java index 3d6faa643..495faa6cd 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/reflect/ClassUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/reflect/ClassUtil.java @@ -36,7 +36,7 @@ * * @author calvin */ -public abstract class ClassUtil { +public class ClassUtil { private static final String CGLIB_CLASS_SEPARATOR = "$$"; @@ -473,4 +473,17 @@ public static Class getClassGenricType(final Class clazz, final int index) { return (Class) params[index]; } + /** + * 探测类是否存在classpath中 + */ + public static boolean isPresent(String className, ClassLoader classLoader) { + try { + classLoader.loadClass(className); + return true; + } catch (Throwable ex) { + // Class or one of its dependencies is not present... + return false; + } + } + } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/reflect/FastMethodInvoker.java b/modules/utils/src/main/java/org/springside/modules/utils/reflect/FastMethodInvoker.java index 5076680d0..7c8afa75a 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/reflect/FastMethodInvoker.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/reflect/FastMethodInvoker.java @@ -26,6 +26,10 @@ public class FastMethodInvoker { private final FastMethod fastMethod; + protected FastMethodInvoker(FastMethod fastMethod) { + this.fastMethod = fastMethod; + } + /** * 获取cglib生成的FastMethod,创建方法的FastMethodInvoker实例. */ @@ -73,10 +77,6 @@ public FastClass get() { return new FastMethodInvoker(fastClz.getMethod(method)); } - protected FastMethodInvoker(FastMethod fastMethod) { - this.fastMethod = fastMethod; - } - /** * 调用方法 */ @@ -85,7 +85,7 @@ public T invoke(Object obj, Object... args) { try { return (T) fastMethod.invoke(obj, args); } catch (Exception e) { - throw ExceptionUtil.unchecked(ExceptionUtil.unwrap(e)); + throw ExceptionUtil.uncheckedAndWrap(e); } } } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/reflect/MethodInvoker.java b/modules/utils/src/main/java/org/springside/modules/utils/reflect/MethodInvoker.java index ae5523551..1c7431a93 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/reflect/MethodInvoker.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/reflect/MethodInvoker.java @@ -45,7 +45,7 @@ public T invoke(final Object obj, Object... args) { try { return (T) method.invoke(obj, args); } catch (Exception e) { - throw ExceptionUtil.unchecked(ExceptionUtil.unwrap(e)); + throw ExceptionUtil.uncheckedAndWrap(e); } } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/reflect/ReflectionUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/reflect/ReflectionUtil.java index 91df4edee..58c4166ab 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/reflect/ReflectionUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/reflect/ReflectionUtil.java @@ -48,7 +48,8 @@ public static T invokeGetter(Object obj, String propertyName) { public static void invokeSetter(Object obj, String propertyName, Object value) { Method method = ClassUtil.getSetterMethod(obj.getClass(), propertyName, value.getClass()); if (method == null) { - throw new IllegalArgumentException("Could not find getter method [" + propertyName + "] on target [" + obj + ']'); + throw new IllegalArgumentException( + "Could not find getter method [" + propertyName + "] on target [" + obj + ']'); } invokeMethod(obj, method, value); } @@ -59,7 +60,7 @@ public static void invokeSetter(Object obj, String propertyName, Object value) { public static T getFieldValue(final Object obj, final String fieldName) { Field field = ClassUtil.getAccessibleField(obj.getClass(), fieldName); if (field == null) { - throw new IllegalArgumentException( "Could not find field [" + fieldName + "] on target [" + obj + ']'); + throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + ']'); } return getFieldValue(obj, field); } @@ -150,7 +151,7 @@ public static T invokeMethod(final Object obj, final String methodName, fina final Class[] parameterTypes) { Method method = ClassUtil.getAccessibleMethod(obj.getClass(), methodName, parameterTypes); if (method == null) { - throw new IllegalArgumentException( "Could not find method [" + methodName + "] on target [" + obj + ']'); + throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + ']'); } return invokeMethod(obj, method, args); } @@ -163,7 +164,7 @@ public static T invokeMethod(final Object obj, final String methodName, fina public static T invokeMethodByName(final Object obj, final String methodName, final Object[] args) { Method method = ClassUtil.getAccessibleMethodByName(obj.getClass(), methodName); if (method == null) { - throw new IllegalArgumentException( "Could not find method [" + methodName + "] on target [" + obj + ']'); + throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + ']'); } return invokeMethod(obj, method, args); } @@ -175,7 +176,7 @@ public static T invokeMethod(final Object obj, Method method, Object... args try { return (T) method.invoke(obj, args); } catch (Exception e) { - throw ExceptionUtil.unchecked(ExceptionUtil.unwrap(e)); + throw ExceptionUtil.uncheckedAndWrap(e); } } @@ -187,7 +188,7 @@ public static T invokeConstructor(final Class cls, Object... args) { try { return ConstructorUtils.invokeConstructor(cls, args); } catch (Exception e) { - throw ExceptionUtil.unchecked(e); + throw ExceptionUtil.uncheckedAndWrap(e); } } diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/Charsets.java b/modules/utils/src/main/java/org/springside/modules/utils/text/Charsets.java index 64cbccc8c..4e5aabb27 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/Charsets.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/Charsets.java @@ -11,7 +11,7 @@ * * @author calvin */ -public abstract class Charsets { +public class Charsets { public static final String UTF_8_NAME = "UTF-8"; public static final Charset UTF_8 = Charset.forName("UTF-8"); diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/CsvUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/text/CsvUtil.java index c0555308d..ddd611c5f 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/CsvUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/CsvUtil.java @@ -35,7 +35,7 @@ * * Helps with CSV strings. See: http://en.wikipedia.org/wiki/Comma-separated_values */ -public abstract class CsvUtil { +public class CsvUtil { protected static final char FIELD_SEPARATOR = ','; protected static final char FIELD_QUOTE = '"'; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/EncodeUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/text/EncodeUtil.java index 11f5bb0dc..fb5d9f074 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/EncodeUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/EncodeUtil.java @@ -10,7 +10,7 @@ /** * hex/base64 编解码工具集,依赖Guava, 取消了对Commmon Codec的依赖 */ -public abstract class EncodeUtil { +public class EncodeUtil { /** * Hex编码, 将byte[]编码为String,默认为ABCDEF为大写字母. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/EscapeUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/text/EscapeUtil.java index 9f6b177b7..ee5ca42cb 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/EscapeUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/EscapeUtil.java @@ -17,7 +17,7 @@ * * @author calvin */ -public abstract class EscapeUtil { +public class EscapeUtil { /** * URL 编码, Encode默认为UTF-8. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/HashUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/text/HashUtil.java index 867f22cbe..ff7bb5722 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/HashUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/HashUtil.java @@ -35,7 +35,7 @@ * * @author calvin */ -public abstract class HashUtil { +public class HashUtil { public static final int MURMUR_SEED = 1318007700; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/MoreStringUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/text/MoreStringUtil.java index 1977ecbf1..10319c7c1 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/MoreStringUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/MoreStringUtil.java @@ -16,7 +16,7 @@ * * @author calvin */ -public abstract class MoreStringUtil { +public class MoreStringUtil { /** * 高性能的Split,针对char的分隔符号,比JDK String自带的高效. @@ -117,16 +117,16 @@ public static boolean endWith(@Nullable CharSequence s, char c) { } return s.charAt(s.length() - 1) == c; } - + /** * 如果结尾字符为c, 去除掉该字符. */ public static String removeEnd(final String s, final char c) { - if (endWith(s,c)) { - return s.substring(0, s.length() - 1); - } - return s; - } + if (endWith(s, c)) { + return s.substring(0, s.length() - 1); + } + return s; + } /** * 计算字符串被UTF8编码后的字节数 via guava diff --git a/modules/utils/src/main/java/org/springside/modules/utils/text/TextValidator.java b/modules/utils/src/main/java/org/springside/modules/utils/text/TextValidator.java index c9e6721f6..9221f1acd 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/text/TextValidator.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/text/TextValidator.java @@ -12,7 +12,7 @@ * * @author calvin */ -public abstract class TextValidator { +public class TextValidator { /** * 正则:手机号(简单), 1字头+10位数字即可. diff --git a/modules/utils/src/main/java/org/springside/modules/utils/time/ClockUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/time/ClockUtil.java index c011702e4..b4972c0c7 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/time/ClockUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/time/ClockUtil.java @@ -9,7 +9,7 @@ * * @author calvin */ -public abstract class ClockUtil { +public class ClockUtil { private static Clock INSTANCE = new DefaultClock(); diff --git a/modules/utils/src/main/java/org/springside/modules/utils/time/DateFormatUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/time/DateFormatUtil.java index 4832f6789..1e142bdb9 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/time/DateFormatUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/time/DateFormatUtil.java @@ -24,7 +24,7 @@ * * @author calvin */ -public abstract class DateFormatUtil { +public class DateFormatUtil { // 以T分隔日期和时间,并带时区信息,符合ISO8601规范 public static final String PATTERN_ISO = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"; diff --git a/modules/utils/src/main/java/org/springside/modules/utils/time/DateUtil.java b/modules/utils/src/main/java/org/springside/modules/utils/time/DateUtil.java index e385de081..f596c83dd 100644 --- a/modules/utils/src/main/java/org/springside/modules/utils/time/DateUtil.java +++ b/modules/utils/src/main/java/org/springside/modules/utils/time/DateUtil.java @@ -14,7 +14,7 @@ * * @author calvin */ -public abstract class DateUtil { +public class DateUtil { public static final long MILLIS_PER_SECOND = 1000; // Number of milliseconds in a standard second. diff --git a/modules/utils/src/test/java/org/springside/modules/utils/base/ExceptionUtilTest.java b/modules/utils/src/test/java/org/springside/modules/utils/base/ExceptionUtilTest.java index 9700b50d3..07b837fe9 100644 --- a/modules/utils/src/test/java/org/springside/modules/utils/base/ExceptionUtilTest.java +++ b/modules/utils/src/test/java/org/springside/modules/utils/base/ExceptionUtilTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import org.springside.modules.utils.base.ExceptionUtil.CloneableException; import org.springside.modules.utils.base.ExceptionUtil.CloneableRuntimeException; +import org.springside.modules.utils.base.ExceptionUtil.UncheckedException; public class ExceptionUtilTest { @@ -67,6 +68,14 @@ public void unwrap() { InvocationTargetException ie = new InvocationTargetException(re); assertThat(ExceptionUtil.unwrap(ie)).isSameAs(re); + + Exception e = new Exception("my exception"); + ExecutionException ee2 = new ExecutionException(e); + try{ + ExceptionUtil.uncheckedAndWrap(ee2); + }catch (Throwable t) { + assertThat(t).isInstanceOf(UncheckedException.class).hasCauseExactlyInstanceOf(Exception.class); + } } @Test @@ -97,7 +106,7 @@ public void getRootCause() { RuntimeException runtimeException = new RuntimeException(illegalStateException); assertThat(ExceptionUtil.getRootCause(runtimeException)).isSameAs(ioexception); - //无cause + // 无cause assertThat(ExceptionUtil.getRootCause(ioexception)).isSameAs(ioexception); } @@ -110,9 +119,9 @@ public void buildMessage() { RuntimeException runtimeExcetpion = new RuntimeException("my runtimeException", ioexception); assertThat(ExceptionUtil.toStringWithRootCause(runtimeExcetpion)) .isEqualTo("RuntimeException: my runtimeException; <---IOException: my exception"); - + assertThat(ExceptionUtil.toStringWithRootCause(null)).isEqualTo(""); - //无cause + // 无cause assertThat(ExceptionUtil.toStringWithRootCause(ioexception)).isEqualTo("IOException: my exception"); } diff --git a/modules/utils/src/test/java/org/springside/modules/utils/concurrent/ThreadUtilTest.java b/modules/utils/src/test/java/org/springside/modules/utils/concurrent/ThreadUtilTest.java index 0b29f6230..85648a539 100644 --- a/modules/utils/src/test/java/org/springside/modules/utils/concurrent/ThreadUtilTest.java +++ b/modules/utils/src/test/java/org/springside/modules/utils/concurrent/ThreadUtilTest.java @@ -3,16 +3,30 @@ import static org.assertj.core.api.Assertions.*; import org.junit.Test; +import org.springside.modules.utils.base.ObjectUtil; public class ThreadUtilTest { @Test public void testCaller(){ hello(); + new MyClass().hello(); + assertThat(ThreadUtil.getCurrentClass()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest"); + assertThat(ThreadUtil.getCurrentMethod()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest.testCaller()"); + } private void hello(){ assertThat(ThreadUtil.getCallerClass()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest"); assertThat(ThreadUtil.getCallerMethod()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest.testCaller()"); - + StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); + System.out.println(ObjectUtil.toPrettyString(stacktrace)); } + public static class MyClass{ + public void hello(){ + assertThat(ThreadUtil.getCallerClass()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest"); + assertThat(ThreadUtil.getCallerMethod()).isEqualTo("org.springside.modules.utils.concurrent.ThreadUtilTest.testCaller()"); + StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); + System.out.println(ObjectUtil.toPrettyString(stacktrace)); + } + } } diff --git a/modules/utils/src/test/java/org/springside/modules/utils/reflect/ClassUtilTest.java b/modules/utils/src/test/java/org/springside/modules/utils/reflect/ClassUtilTest.java index bb50c4fd5..77cb8cc54 100644 --- a/modules/utils/src/test/java/org/springside/modules/utils/reflect/ClassUtilTest.java +++ b/modules/utils/src/test/java/org/springside/modules/utils/reflect/ClassUtilTest.java @@ -67,6 +67,11 @@ public void getSuperClassGenricType() { assertThat(ClassUtil.getClassGenricType(TestBean3.class)).isEqualTo(Object.class); } + public void classPresent(){ + assertThat(ClassUtil.isPresent("a.b.c", ClassUtil.getDefaultClassLoader())).isFalse(); + assertThat(ClassUtil.isPresent("org.springside.modules.utils.reflect.ClassUtil", ClassUtil.getDefaultClassLoader())).isTrue(); + } + public static class ParentBean { }