only save Class when enhance

This commit is contained in:
hengyunabc 2020-05-20 15:00:20 +08:00
parent 4afedf89e1
commit d25f28fd06

View File

@ -70,8 +70,8 @@ public class Enhancer implements ClassFileTransformer {
private final Matcher methodNameMatcher;
private final EnhancerAffect affect;
// -字节码缓存
private final static Map<Class<?>/* Class */, byte[]/* bytes of Class */> classBytesCache = new WeakHashMap<Class<?>, byte[]>();
// 被增强的类的缓存
private final static Map<Class<?>/* Class */, Object> classBytesCache = new WeakHashMap<Class<?>, Object>();
private static SpyImpl spyImpl = new SpyImpl();
static {
@ -190,7 +190,7 @@ public class Enhancer implements ClassFileTransformer {
}
} catch (Throwable e) {
logger.error("the classloader can not load SpyAPI, ignore it. classloader: {}, className: {}",
inClassLoader.getName(), className);
inClassLoader.getClass().getName(), className);
return null;
}
@ -200,13 +200,6 @@ public class Enhancer implements ClassFileTransformer {
return null;
}
// 首先先检查是否在缓存中存在Class字节码
// 因为要支持多人协作,存在多人同时增强的情况
final byte[] byteOfClassInCache = classBytesCache.get(classBeingRedefined);
if (null != byteOfClassInCache) {
classfileBuffer = byteOfClassInCache;
}
ClassNode classNode = AsmUtils.toClassNode(classfileBuffer);
// 生成增强字节码
@ -307,8 +300,8 @@ public class Enhancer implements ClassFileTransformer {
byte[] enhanceClassByteArray = AsmUtils.toBytes(classNode);
// 生成成功,推入缓存
classBytesCache.put(classBeingRedefined, enhanceClassByteArray);
// 增强成功记录类
classBytesCache.put(classBeingRedefined, new Object());
// dump the class
dumpClassIfNecessary(className, enhanceClassByteArray, affect);