diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index b7aff27..5452079 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -41,10 +41,9 @@ public class TestableClassTransformer implements ClassFileTransformer { private static final String CLASS_NAME_MOCK = "Mock"; /** - * Just avoid spend time to scan those surely non-user classes Should keep these lists as tiny as possible + * Just avoid spend time to scan those surely non-user classes, should keep these lists as tiny as possible */ - private final String[] BLACKLIST_PREFIXES = new String[] {"jdk/", "java/", "javax/", "sun/", "com/sun/", - "org/groovy", "org/apache/maven/", "com/alibaba/testable/", "junit/", "org/junit/", "org/testng/"}; + private final String[] BLACKLIST_PREFIXES = new String[] { "sun/", "com/sun/" }; public MockClassParser mockClassParser = new MockClassParser(); diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniAccessor.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniAccessor.java index 071fc7e..47dc3b3 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniAccessor.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniAccessor.java @@ -96,7 +96,7 @@ public class OmniAccessor { } private static List generateMemberIndex(String basePath, Class clazz) { - List fields = getAllFields(clazz); + List fields = TypeUtil.getAllFields(clazz); List paths = new ArrayList(); for (Field f : fields) { if (!f.getName().startsWith(THIS_REF_PREFIX)) { @@ -108,15 +108,6 @@ public class OmniAccessor { return paths; } - private static List getAllFields(Class clazz) { - Class rawClass = clazz.isArray() ? clazz.getComponentType() : clazz; - List fields = new ArrayList(Arrays.asList(rawClass.getDeclaredFields())); - if (rawClass.getSuperclass() != null) { - fields.addAll(getAllFields(rawClass.getSuperclass())); - } - return fields; - } - private static String toPath(Field field) { return field.getName() + BRACE_START + field.getType().getSimpleName() + BRACE_END; } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniConstructor.java b/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniConstructor.java index bb25736..bd8ee1f 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniConstructor.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/tool/OmniConstructor.java @@ -1,12 +1,19 @@ package com.alibaba.testable.core.tool; +import com.alibaba.testable.core.model.Null; +import com.alibaba.testable.core.util.TypeUtil; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; + /** * @author flin */ public class OmniConstructor { public static T newInstance(Class clazz) { - return null; + return newInstance(clazz, isJavaAgentEnabled(clazz)); } public static void appendInstance(Object target, Class clazz) { @@ -17,4 +24,37 @@ public class OmniConstructor { return; } + private static boolean isJavaAgentEnabled(Class clazz) { + try { + clazz.getDeclaredConstructor(Null.class); + } catch (NoSuchMethodException e) { + return false; + } + return true; + } + + private static T newInstance(Class clazz, boolean withJavaAgent) { + try { + Constructor constructor = withJavaAgent ? + clazz.getDeclaredConstructor(Null.class) : clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + Object ins = withJavaAgent ? constructor.newInstance(new Null()) : constructor.newInstance(); + for (Field f : TypeUtil.getAllFields(clazz)) { + f.setAccessible(true); + if (!f.getType().isPrimitive()) { + f.set(ins, newInstance(f.getType(), withJavaAgent)); + } + } + return (T)ins; + } catch (NoSuchMethodException e) { + return null; + } catch (IllegalAccessException e) { + return null; + } catch (InstantiationException e) { + return null; + } catch (InvocationTargetException e) { + return null; + } + } + } diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java index 2dd699b..321a470 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java @@ -3,6 +3,9 @@ package com.alibaba.testable.core.util; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * @author flin @@ -41,6 +44,20 @@ public class TypeUtil { } } + /** + * get all field from class and its parents + * @param clazz class contains fields + * @return all fields available + */ + public static List getAllFields(Class clazz) { + Class rawClass = clazz.isArray() ? clazz.getComponentType() : clazz; + List fields = new ArrayList(Arrays.asList(rawClass.getDeclaredFields())); + if (rawClass.getSuperclass() != null) { + fields.addAll(getAllFields(rawClass.getSuperclass())); + } + return fields; + } + /** * get constructor by parameter matching * @param clazz class to construct