mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-03-23 16:20:26 +08:00
loaded class cache not actually working
This commit is contained in:
parent
f624c9131e
commit
179e71c7c2
@ -1,39 +0,0 @@
|
|||||||
package com.alibaba.testable.agent.model;
|
|
||||||
|
|
||||||
import org.objectweb.asm.tree.AnnotationNode;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Record parameter fetch from @MockWith annotation
|
|
||||||
*
|
|
||||||
* @author flin
|
|
||||||
*/
|
|
||||||
public class CachedMockParameter {
|
|
||||||
|
|
||||||
private final boolean classExist;
|
|
||||||
private final AnnotationNode mockWith;
|
|
||||||
|
|
||||||
private CachedMockParameter(boolean classExist, AnnotationNode mockWith) {
|
|
||||||
this.classExist = classExist;
|
|
||||||
this.mockWith = mockWith;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CachedMockParameter notExist() {
|
|
||||||
return new CachedMockParameter(false, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CachedMockParameter exist() {
|
|
||||||
return new CachedMockParameter(true, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static CachedMockParameter exist(AnnotationNode mockWith) {
|
|
||||||
return new CachedMockParameter(true, mockWith);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isClassExist() {
|
|
||||||
return classExist;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AnnotationNode getMockWith() {
|
|
||||||
return mockWith;
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,10 +3,8 @@ package com.alibaba.testable.agent.transformer;
|
|||||||
import com.alibaba.testable.agent.constant.ConstPool;
|
import com.alibaba.testable.agent.constant.ConstPool;
|
||||||
import com.alibaba.testable.agent.handler.SourceClassHandler;
|
import com.alibaba.testable.agent.handler.SourceClassHandler;
|
||||||
import com.alibaba.testable.agent.handler.TestClassHandler;
|
import com.alibaba.testable.agent.handler.TestClassHandler;
|
||||||
import com.alibaba.testable.agent.model.CachedMockParameter;
|
|
||||||
import com.alibaba.testable.agent.tool.ImmutablePair;
|
import com.alibaba.testable.agent.tool.ImmutablePair;
|
||||||
import com.alibaba.testable.agent.model.MethodInfo;
|
import com.alibaba.testable.agent.model.MethodInfo;
|
||||||
import com.alibaba.testable.agent.tool.ComparableWeakRef;
|
|
||||||
import com.alibaba.testable.agent.util.AnnotationUtil;
|
import com.alibaba.testable.agent.util.AnnotationUtil;
|
||||||
import com.alibaba.testable.agent.util.ClassUtil;
|
import com.alibaba.testable.agent.util.ClassUtil;
|
||||||
import com.alibaba.testable.core.util.LogUtil;
|
import com.alibaba.testable.core.util.LogUtil;
|
||||||
@ -29,8 +27,6 @@ import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassNa
|
|||||||
public class TestableClassTransformer implements ClassFileTransformer {
|
public class TestableClassTransformer implements ClassFileTransformer {
|
||||||
|
|
||||||
private static final String FIELD_DIAGNOSE = "diagnose";
|
private static final String FIELD_DIAGNOSE = "diagnose";
|
||||||
private final Map<ComparableWeakRef<String>, CachedMockParameter> loadedClass =
|
|
||||||
new WeakHashMap<ComparableWeakRef<String>, CachedMockParameter>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Just avoid spend time to scan those surely non-user classes
|
* Just avoid spend time to scan those surely non-user classes
|
||||||
@ -43,7 +39,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
@Override
|
@Override
|
||||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
||||||
ProtectionDomain protectionDomain, byte[] classFileBuffer) {
|
ProtectionDomain protectionDomain, byte[] classFileBuffer) {
|
||||||
if (isSystemClass(className) || loadedClass.containsKey(new ComparableWeakRef<String>(className))) {
|
if (isSystemClass(className)) {
|
||||||
// Ignore system class and reloaded class
|
// Ignore system class and reloaded class
|
||||||
LogUtil.verbose("Ignore class: " + (className == null ? "<lambda>" : className));
|
LogUtil.verbose("Ignore class: " + (className == null ? "<lambda>" : className));
|
||||||
return null;
|
return null;
|
||||||
@ -150,11 +146,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
* @return found annotation or not
|
* @return found annotation or not
|
||||||
*/
|
*/
|
||||||
private boolean hasMockAnnotation(String className) {
|
private boolean hasMockAnnotation(String className) {
|
||||||
CachedMockParameter cache = loadedClass.get(new ComparableWeakRef<String>(className));
|
|
||||||
if (cache != null) {
|
|
||||||
setupMockContext(cache.getMockWith());
|
|
||||||
return cache.isClassExist();
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
ClassNode cn = new ClassNode();
|
ClassNode cn = new ClassNode();
|
||||||
new ClassReader(className).accept(cn, 0);
|
new ClassReader(className).accept(cn, 0);
|
||||||
@ -162,7 +153,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
for (AnnotationNode an : cn.visibleAnnotations) {
|
for (AnnotationNode an : cn.visibleAnnotations) {
|
||||||
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.MOCK_WITH)) {
|
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.MOCK_WITH)) {
|
||||||
setupMockContext(an);
|
setupMockContext(an);
|
||||||
loadedClass.put(new ComparableWeakRef<String>(className), CachedMockParameter.exist(an));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,7 +164,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
|
if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
|
||||||
fullClassName.equals(ConstPool.TESTABLE_MOCK) ||
|
fullClassName.equals(ConstPool.TESTABLE_MOCK) ||
|
||||||
fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
||||||
loadedClass.put(new ComparableWeakRef<String>(className), CachedMockParameter.exist());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,7 +173,6 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
// Usually class not found, return without record
|
// Usually class not found, return without record
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
loadedClass.put(new ComparableWeakRef<String>(className), CachedMockParameter.notExist());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user