From 174400e79118e5d86fd7cc98cb44b2ffa058b9a8 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sat, 26 Jun 2021 11:19:30 +0800 Subject: [PATCH] =?UTF-8?q?perf[hotswap]:=20=E4=BC=98=E5=8C=96=E4=BA=86Hot?= =?UTF-8?q?SwapUtils=E7=9A=84=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zfoo/hotswap/util/HotSwapUtils.java | 40 +++++----- .../zfoo/protocol/collection/ArrayUtils.java | 79 +++++++++++++++++++ 2 files changed, 101 insertions(+), 18 deletions(-) diff --git a/hotswap/src/main/java/com/zfoo/hotswap/util/HotSwapUtils.java b/hotswap/src/main/java/com/zfoo/hotswap/util/HotSwapUtils.java index ff9e1ba6..9abd06d3 100644 --- a/hotswap/src/main/java/com/zfoo/hotswap/util/HotSwapUtils.java +++ b/hotswap/src/main/java/com/zfoo/hotswap/util/HotSwapUtils.java @@ -1,6 +1,6 @@ package com.zfoo.hotswap.util; -import com.zfoo.protocol.exception.ExceptionUtils; +import com.zfoo.protocol.collection.ArrayUtils; import com.zfoo.protocol.util.IOUtils; import com.zfoo.protocol.util.StringUtils; import javassist.ClassPool; @@ -24,20 +24,6 @@ public abstract class HotSwapUtils { private static final Logger logger = LoggerFactory.getLogger(HotSwapUtils.class); - private static String readClassName(byte[] bytes) { - //ByteArrayInputStream byteArrayInputStream = null; - DataInputStream dataInputStream = null; - try { - dataInputStream = new DataInputStream(new ByteArrayInputStream(bytes)); - var classFile = new ClassFile(dataInputStream); - return classFile.getName().replaceAll(StringUtils.SLASH, StringUtils.PERIOD); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - IOUtils.closeIO(dataInputStream); - } - } - /** * 热更新java文件 * jvm的启动参数,jdk11过后默认的全款更不允许连接自己,VM: -Djdk.attach.allowAttachSelf=true @@ -47,20 +33,38 @@ public abstract class HotSwapUtils { * @param bytes .class结尾的字节码文件 */ public static synchronized void hotswapClass(byte[] bytes) { - if (bytes == null || bytes.length <= 0) { + if (ArrayUtils.isEmpty(bytes)) { return; } + var clazzName = readClassName(bytes); + Class clazz = null; try { - clazz = Class.forName(readClassName(bytes)); + clazz = Class.forName(clazzName); } catch (ClassNotFoundException e) { - logger.error(ExceptionUtils.getMessage(e)); + logger.info("无法在当前项目找到[class:{}],所以忽略本次热更新", clazzName); + return; } hotswapClassByJavassist(clazz, bytes); } + private static String readClassName(byte[] bytes) { + ByteArrayInputStream byteArrayInputStream = null; + DataInputStream dataInputStream = null; + try { + byteArrayInputStream = new ByteArrayInputStream(bytes); + dataInputStream = new DataInputStream(byteArrayInputStream); + var classFile = new ClassFile(dataInputStream); + return classFile.getName().replaceAll(StringUtils.SLASH, StringUtils.PERIOD); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + IOUtils.closeIO(dataInputStream, byteArrayInputStream); + } + } + private static void hotswapClassByJavassist(Class clazz, byte[] bytes) { ByteArrayInputStream byteArrayInputStream = null; CtClass ctClass = null; diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java b/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java index 96657b25..08bf85e5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java @@ -64,6 +64,85 @@ public abstract class ArrayUtils { return array == null ? 0 : array.length; } + /** + * isEmpty + */ + public static boolean isEmpty(boolean[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(byte[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(short[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(int[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(long[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(float[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(double[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(char[] array) { + return (array == null || array.length == 0); + } + + public static boolean isEmpty(T[] array) { + return (array == null || array.length == 0); + } + + /** + * isNotEmpty + */ + public static boolean isNotEmpty(boolean[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(byte[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(short[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(int[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(long[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(float[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(double[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(char[] array) { + return !isEmpty(array); + } + + public static boolean isNotEmpty(T[] array) { + return !isEmpty(array); + } + + /** * toList */