mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
handle targetClass parameter
This commit is contained in:
parent
74b68d495a
commit
f64e0cd959
@ -11,6 +11,7 @@ public class ConstPool {
|
|||||||
public static final String TEST_POSTFIX = "Test";
|
public static final String TEST_POSTFIX = "Test";
|
||||||
|
|
||||||
public static final String FIELD_TARGET_METHOD = "targetMethod";
|
public static final String FIELD_TARGET_METHOD = "targetMethod";
|
||||||
|
public static final String FIELD_TARGET_CLASS = "targetClass";
|
||||||
|
|
||||||
public static final String MOCK_WITH = "com.alibaba.testable.core.annotation.MockWith";
|
public static final String MOCK_WITH = "com.alibaba.testable.core.annotation.MockWith";
|
||||||
public static final String MOCK_METHOD = "com.alibaba.testable.core.annotation.MockMethod";
|
public static final String MOCK_METHOD = "com.alibaba.testable.core.annotation.MockMethod";
|
||||||
|
@ -9,6 +9,7 @@ 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.agent.util.GlobalConfig;
|
import com.alibaba.testable.agent.util.GlobalConfig;
|
||||||
import com.alibaba.testable.agent.util.StringUtil;
|
import com.alibaba.testable.agent.util.StringUtil;
|
||||||
|
import com.alibaba.testable.core.model.NullType;
|
||||||
import com.alibaba.testable.core.util.LogUtil;
|
import com.alibaba.testable.core.util.LogUtil;
|
||||||
import com.alibaba.testable.core.model.MockDiagnose;
|
import com.alibaba.testable.core.model.MockDiagnose;
|
||||||
import org.objectweb.asm.ClassReader;
|
import org.objectweb.asm.ClassReader;
|
||||||
@ -131,8 +132,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkMethodAnnotation(ClassNode cn, List<MethodInfo> methodInfos, MethodNode mn) {
|
private void checkMethodAnnotation(ClassNode cn, List<MethodInfo> methodInfos, MethodNode mn) {
|
||||||
ImmutablePair<String, String> methodDescPair = extractFirstParameter(mn.desc);
|
if (mn.visibleAnnotations == null) {
|
||||||
if (methodDescPair == null || mn.visibleAnnotations == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||||
@ -141,6 +141,10 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
addMockConstructor(cn, methodInfos, mn);
|
addMockConstructor(cn, methodInfos, mn);
|
||||||
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
|
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
|
||||||
fullClassName.equals(ConstPool.TESTABLE_MOCK)) {
|
fullClassName.equals(ConstPool.TESTABLE_MOCK)) {
|
||||||
|
ImmutablePair<String, String> methodDescPair = getMethodDescPair(mn, an);
|
||||||
|
if (methodDescPair == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
||||||
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
||||||
if (targetMethod.equals(ConstPool.CONSTRUCTOR)) {
|
if (targetMethod.equals(ConstPool.CONSTRUCTOR)) {
|
||||||
@ -153,6 +157,16 @@ public class TestableClassTransformer implements ClassFileTransformer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ImmutablePair<String, String> getMethodDescPair(MethodNode mn, AnnotationNode an) {
|
||||||
|
Class<?> targetClass = AnnotationUtil.getAnnotationParameter(
|
||||||
|
an, ConstPool.FIELD_TARGET_CLASS, NullType.class, Class.class);
|
||||||
|
if (targetClass.equals(NullType.class)) {
|
||||||
|
return extractFirstParameter(mn.desc);
|
||||||
|
} else {
|
||||||
|
return ImmutablePair.of(ClassUtil.toByteCodeClassName(targetClass.getName()), mn.desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addMockMethod(List<MethodInfo> methodInfos, MethodNode mn,
|
private void addMockMethod(List<MethodInfo> methodInfos, MethodNode mn,
|
||||||
ImmutablePair<String, String> methodDescPair, String targetMethod) {
|
ImmutablePair<String, String> methodDescPair, String targetMethod) {
|
||||||
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
|
String targetClass = ClassUtil.toSlashSeparateFullClassName(methodDescPair.left);
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.alibaba.testable.agent.transformer;
|
||||||
|
|
||||||
|
import com.alibaba.testable.agent.tool.ImmutablePair;
|
||||||
|
import com.alibaba.testable.core.accessor.PrivateAccessor;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class TestableClassTransformerTest {
|
||||||
|
|
||||||
|
private TestableClassTransformer testableClassTransformer = new TestableClassTransformer();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_split_parameters() {
|
||||||
|
ImmutablePair<String, String> parameters =
|
||||||
|
PrivateAccessor.invoke(testableClassTransformer, "extractFirstParameter", "()");
|
||||||
|
assertNull(parameters);
|
||||||
|
parameters = PrivateAccessor.invoke(testableClassTransformer, "extractFirstParameter", "(Lcom.alibaba.demo.Class;ILjava.lang.String;Z)");
|
||||||
|
assertNotNull(parameters);
|
||||||
|
assertEquals("Lcom.alibaba.demo.Class;", parameters.left);
|
||||||
|
assertEquals("(ILjava.lang.String;Z)", parameters.right);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package com.alibaba.testable.core.annotation;
|
package com.alibaba.testable.core.annotation;
|
||||||
|
|
||||||
import javax.lang.model.type.NullType;
|
import com.alibaba.testable.core.model.NullType;
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.alibaba.testable.core.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author flin
|
||||||
|
*/
|
||||||
|
public interface NullType {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user