From e02a71d32db8428d33237271849b6194be1be396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E5=99=A8=E8=BF=91?= Date: Thu, 12 Nov 2020 16:52:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E9=99=A4lang3=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +- .../yezhihao/protostar/field/BasicField.java | 4 +- .../yezhihao/protostar/util/StrUtils.java | 93 +++++++++++++++++++ 3 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/github/yezhihao/protostar/util/StrUtils.java diff --git a/pom.xml b/pom.xml index 9e56093..ddc3dec 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ junit junit - 4.12 + 4.13.1 test @@ -62,12 +62,8 @@ io.netty netty-buffer - 4.1.51.Final - - - org.apache.commons - commons-lang3 - 3.11 + 4.1.54.Final + provided diff --git a/src/main/java/io/github/yezhihao/protostar/field/BasicField.java b/src/main/java/io/github/yezhihao/protostar/field/BasicField.java index 03fc6ee..4fd677a 100644 --- a/src/main/java/io/github/yezhihao/protostar/field/BasicField.java +++ b/src/main/java/io/github/yezhihao/protostar/field/BasicField.java @@ -1,8 +1,8 @@ package io.github.yezhihao.protostar.field; import io.github.yezhihao.protostar.annotation.Field; +import io.github.yezhihao.protostar.util.StrUtils; import io.netty.buffer.ByteBuf; -import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +44,7 @@ public abstract class BasicField implements Comparable> { public void println(int index, String desc, String hex, Object value) { if (value != null) - System.out.println(index + "\t" + "[" + hex + "] " + desc + ": " + (value.getClass().isArray() ? ArrayUtils.toString(value) : value)); + System.out.println(index + "\t" + "[" + hex + "] " + desc + ": " + StrUtils.toString(value)); } public int index() { diff --git a/src/main/java/io/github/yezhihao/protostar/util/StrUtils.java b/src/main/java/io/github/yezhihao/protostar/util/StrUtils.java new file mode 100644 index 0000000..c8408e5 --- /dev/null +++ b/src/main/java/io/github/yezhihao/protostar/util/StrUtils.java @@ -0,0 +1,93 @@ +package io.github.yezhihao.protostar.util; + +/** + * @author yezhihao + * home https://gitee.com/yezhihao/jt808-server + */ +public class StrUtils { + + public static String toString(Object value) { + if (!value.getClass().isArray()) + return value.toString(); + + StringBuilder root = new StringBuilder(32); + toString(value, root); + return root.toString(); + } + + public static StringBuilder toString(Object value, StringBuilder builder) { + if (value == null) + return builder; + + builder.append('['); + int start = builder.length(); + + if (value instanceof long[]) { + long[] array = (long[]) value; + for (long t : array) + builder.append(t).append(','); + + } else if (value instanceof int[]) { + int[] array = (int[]) value; + for (int t : array) + builder.append(t).append(','); + + } else if (value instanceof short[]) { + short[] array = (short[]) value; + for (short t : array) + builder.append(t).append(','); + + } else if (value instanceof byte[]) { + byte[] array = (byte[]) value; + for (byte t : array) + builder.append(t).append(','); + + } else if (value instanceof char[]) { + char[] array = (char[]) value; + for (char t : array) + builder.append(t).append(','); + + } else if (value instanceof double[]) { + double[] array = (double[]) value; + for (double t : array) + builder.append(t).append(','); + + } else if (value instanceof float[]) { + float[] array = (float[]) value; + for (float t : array) + builder.append(t).append(','); + + } else if (value instanceof boolean[]) { + boolean[] array = (boolean[]) value; + for (boolean t : array) + builder.append(t).append(','); + + } else if (value instanceof String[]) { + String[] array = (String[]) value; + for (String t : array) + builder.append(t).append(','); + + } else if (isArray(value)) { + Object[] array = (Object[]) value; + for (Object t : array) + toString(t, builder).append(','); + + } else if (value instanceof Object[]) { + Object[] array = (Object[]) value; + for (Object t : array) + builder.append(t).append(','); + } + + int end = builder.length(); + if (end <= start) builder.append(']'); + else builder.setCharAt(end - 1, ']'); + return builder; + } + + private static boolean isArray(Object value) { + Class componentType = value.getClass().getComponentType(); + if (componentType == null) + return false; + return componentType.isArray(); + } +} \ No newline at end of file