mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
feat: generate none-null object for classes not in jvm package
This commit is contained in:
@@ -15,15 +15,15 @@ public enum ConstructionOption {
|
||||
EXCEPT_INTERFACE,
|
||||
|
||||
/**
|
||||
* 生成的接口成员方法返回非空对象(某些JDK内置接口不兼容此选项)
|
||||
* methods in constructed interface return real object instead of null
|
||||
* 生成的接口成员方法不返回初始化过的对象
|
||||
* allow return value of methods in constructed interface be null
|
||||
*/
|
||||
RICH_INTERFACE_METHOD,
|
||||
EXCEPT_RETURN_VALUE,
|
||||
|
||||
/**
|
||||
* 生成的虚拟抽象类调用父构造方法时,使用非空对象作为参数
|
||||
* methods in constructed interface return real object instead of null
|
||||
* 调用虚拟抽象类调用父构造方法时,不使用初始化过的参数对象
|
||||
* allow to invoke constructor of abstract class with null parameter
|
||||
*/
|
||||
RICH_INTERFACE_CONSTRUCTOR
|
||||
EXCEPT_CONSTRUCTOR_PARAMETER
|
||||
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
|
||||
import static com.alibaba.testable.core.constant.ConstPool.DOT;
|
||||
import static com.alibaba.testable.core.model.ConstructionOption.RICH_INTERFACE_CONSTRUCTOR;
|
||||
import static com.alibaba.testable.core.model.ConstructionOption.RICH_INTERFACE_METHOD;
|
||||
import static com.alibaba.testable.core.model.ConstructionOption.EXCEPT_CONSTRUCTOR_PARAMETER;
|
||||
import static com.alibaba.testable.core.model.ConstructionOption.EXCEPT_RETURN_VALUE;
|
||||
import static com.alibaba.testable.core.util.CollectionUtil.entryOf;
|
||||
import static com.alibaba.testable.core.util.CollectionUtil.mapOf;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ConstructionUtil {
|
||||
private static final String TESTABLE_IMPL = "$TestableImpl";
|
||||
|
||||
/**
|
||||
* Default value of basic types
|
||||
* Default value of basic and special types
|
||||
*/
|
||||
private static final Map<String, String> DEFAULT_VALUES = mapOf(
|
||||
entryOf("java.lang.String", "\"mock\""),
|
||||
@@ -37,7 +37,8 @@ public class ConstructionUtil {
|
||||
entryOf("long", "1L"),
|
||||
entryOf("java.lang.Long", "1L"),
|
||||
entryOf("boolean", "true"),
|
||||
entryOf("java.lang.Boolean", "true")
|
||||
entryOf("java.lang.Boolean", "true"),
|
||||
entryOf("java.nio.charset.Charset", "java.nio.charset.Charset.defaultCharset()")
|
||||
);
|
||||
|
||||
public static <T> T generateSubClassOf(Class<T> clazz, ConstructionOption[] options) throws InstantiationException {
|
||||
@@ -88,7 +89,7 @@ public class ConstructionUtil {
|
||||
}
|
||||
invocation.append(getDefaultValue(getClassName(genericParameterTypes[i], genericTypes),
|
||||
getClassName(parameterTypes[i]),
|
||||
CollectionUtil.contains(options, RICH_INTERFACE_CONSTRUCTOR), genericTypes));
|
||||
CollectionUtil.contains(options, EXCEPT_CONSTRUCTOR_PARAMETER), genericTypes));
|
||||
}
|
||||
invocation.append(");");
|
||||
return invocation.toString();
|
||||
@@ -130,7 +131,7 @@ public class ConstructionUtil {
|
||||
if (!"void".equals(returnType)) {
|
||||
sourceCode.append("\t\treturn ")
|
||||
.append(getDefaultValue(returnType, getClassName(m.getReturnType()),
|
||||
CollectionUtil.contains(options, RICH_INTERFACE_METHOD), genericTypes))
|
||||
CollectionUtil.contains(options, EXCEPT_RETURN_VALUE), genericTypes))
|
||||
.append(";\n");
|
||||
}
|
||||
sourceCode.append("\t}\n");
|
||||
@@ -153,15 +154,15 @@ public class ConstructionUtil {
|
||||
return methods;
|
||||
}
|
||||
|
||||
private static String getDefaultValue(String genericTypeName, String simpleTypeName, boolean richValue,
|
||||
private static String getDefaultValue(String genericTypeName, String simpleTypeName, boolean alwaysUseNullParameter,
|
||||
Map<String, String> genericTypes) {
|
||||
if (DEFAULT_VALUES.containsKey(genericTypeName)) {
|
||||
return DEFAULT_VALUES.get(genericTypeName);
|
||||
} else if (richValue) {
|
||||
} else if (genericTypeName.startsWith("java.") || alwaysUseNullParameter) {
|
||||
return "null";
|
||||
} else {
|
||||
return "(" + genericTypeName + ") " + getClassName(OmniConstructor.class, genericTypes) +
|
||||
".newInstance(" + simpleTypeName + ".class)";
|
||||
} else {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user