mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-19 09:43:29 +08:00
feat: generate empty sub class for interface
This commit is contained in:
@@ -114,11 +114,11 @@ public class OmniConstructor {
|
||||
return array;
|
||||
}
|
||||
|
||||
private static <T> T newAbstractClass(Class<T> clazz) {
|
||||
return null;
|
||||
private static <T> T newAbstractClass(Class<T> clazz) throws InstantiationException {
|
||||
return ConstructionUtil.generateSubClassOf(clazz);
|
||||
}
|
||||
|
||||
private static <T> T newInterface(Class<T> clazz) {
|
||||
private static <T> T newInterface(Class<T> clazz) throws InstantiationException {
|
||||
if (clazz.equals(List.class)) {
|
||||
return (T)Collections.emptyList();
|
||||
} else if (clazz.equals(Map.class)) {
|
||||
@@ -126,7 +126,7 @@ public class OmniConstructor {
|
||||
} else if (clazz.equals(Set.class)) {
|
||||
return (T)Collections.emptySet();
|
||||
}
|
||||
return null;
|
||||
return ConstructionUtil.generateSubClassOf(clazz);
|
||||
}
|
||||
|
||||
private static <T> T newEnum(Class<T> clazz)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alibaba.testable.core.util;
|
||||
|
||||
import com.alibaba.testable.core.compile.InMemoryJavaCompiler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class ConstructionUtil {
|
||||
|
||||
private static final String TESTABLE_IMPL = "$TestableImpl";
|
||||
private static final String TESTABLE_OMNI_PKG = "com.alibaba.testable.omni";
|
||||
|
||||
public static <T> T generateSubClassOf(Class<T> clazz) throws InstantiationException {
|
||||
StringBuilder sourceCode = new StringBuilder();
|
||||
sourceCode.append("package ").append(TESTABLE_OMNI_PKG).append(";\n")
|
||||
.append("public class ").append(getSubclassName(clazz))
|
||||
.append(clazz.isInterface() ? " implements " : " extends ").append(clazz.getName()).append(" {\n");
|
||||
for (Method m : clazz.getMethods()) {
|
||||
|
||||
}
|
||||
sourceCode.append("}");
|
||||
|
||||
try {
|
||||
Class<?> helloClass = InMemoryJavaCompiler.newInstance()
|
||||
.compile(TESTABLE_OMNI_PKG + "." + getSubclassName(clazz), sourceCode.toString());
|
||||
return (T) helloClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new InstantiationException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static String getSubclassName(Class<?> clazz) {
|
||||
return clazz.getName() + TESTABLE_IMPL;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user