targetClass parameter use class type instead of string

This commit is contained in:
金戟 2020-10-22 23:41:34 +08:00
parent 8cc0b0313a
commit 4f6da20fa4
5 changed files with 29 additions and 9 deletions

View File

@ -25,17 +25,17 @@ class DemoServiceTest {
return "mock_" + text;
}
@TestableInject(targetClass="java.lang.String")
@TestableInject(targetClass = String.class)
private String trim(String self) {
return "trim_string";
}
@TestableInject(targetClass="java.lang.String", targetMethod = "substring")
@TestableInject(targetClass = String.class, targetMethod = "substring")
private String sub(String self, int i, int j) {
return "sub_string";
}
@TestableInject(targetClass="java.lang.String")
@TestableInject(targetClass = String.class)
private boolean startsWith(String self, String s) {
return false;
}

View File

@ -19,13 +19,13 @@ internal class DemoServiceTest {
@TestableInject
private fun innerFunc(text: String) = "mock_$text"
@TestableInject(targetClass = "com.alibaba.testable.demo.BlackBox")
@TestableInject(targetClass = BlackBox::class)
private fun trim(self: BlackBox) = "trim_string"
@TestableInject(targetClass = "com.alibaba.testable.demo.BlackBox", targetMethod = "substring")
@TestableInject(targetClass = BlackBox::class, targetMethod = "substring")
private fun sub(self: BlackBox, i: Int, j: Int) = "sub_string"
@TestableInject(targetClass = "com.alibaba.testable.demo.BlackBox")
@TestableInject(targetClass = BlackBox::class)
private fun startsWith(self: BlackBox, s: String) = false
@TestableInject

View File

@ -7,6 +7,7 @@ import com.alibaba.testable.agent.model.ImmutablePair;
import com.alibaba.testable.agent.model.MethodInfo;
import com.alibaba.testable.agent.util.ClassUtil;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
@ -112,7 +113,12 @@ public class TestableClassTransformer implements ClassFileTransformer {
if (an.values != null) {
int i = an.values.indexOf(key);
if (i % 2 == 0) {
return (String)an.values.get(i+1);
Object value = an.values.get(i + 1);
if (value instanceof Type) {
// fit for `targetClass` parameter
return ClassUtil.toSlashSeparateFullClassName(value.toString());
}
return value.toString();
}
}
return defaultValue;

View File

@ -117,6 +117,13 @@ public class ClassUtil {
}
}
/**
* convert slash separated name to dot separated name
*/
public static String toDotSeparatedName(String name) {
return name.replace(ConstPool.SLASH, ConstPool.DOT);
}
/**
* convert dot separated name to slash separated name
*/
@ -135,7 +142,14 @@ public class ClassUtil {
* convert byte code class name to dot separated human readable name
*/
public static String toDotSeparateFullClassName(String className) {
return className.replace(ConstPool.SLASH, ConstPool.DOT).substring(1, className.length() - 1);
return toDotSeparatedName(className).substring(1, className.length() - 1);
}
/**
* convert byte code class name to slash separated human readable name
*/
public static String toSlashSeparateFullClassName(String className) {
return toSlashSeparatedName(className).substring(1, className.length() - 1);
}
private static boolean isPrimaryType(byte b) {

View File

@ -15,7 +15,7 @@ public @interface TestableInject {
/**
* mock method of specified class instead of the class under test
*/
String targetClass() default "";
Class targetClass() default Object.class;
/**
* mock specified method instead of method with same name