From 75c6a67b8fc56c23654ecf559ea98621206c7ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sun, 25 Apr 2021 23:27:51 +0800 Subject: [PATCH] support construction options --- .../agent/handler/OmniClassHandler.java | 17 +++++++---------- .../core/model/ConstructionOption.java | 7 +++++++ .../testable/core/tool/OmniConstructor.java | 19 +++++++++++++++---- .../testable/core/util/CollectionUtil.java | 8 ++++++++ 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 testable-core/src/main/java/com/alibaba/testable/core/model/ConstructionOption.java diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/OmniClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/OmniClassHandler.java index 2a56869..d65a68c 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/OmniClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/OmniClassHandler.java @@ -7,13 +7,12 @@ import com.alibaba.testable.agent.util.CollectionUtil; import org.objectweb.asm.Label; import org.objectweb.asm.tree.*; -import java.util.HashSet; import java.util.List; -import java.util.Set; import static com.alibaba.testable.agent.util.ClassUtil.CLASS_OBJECT; import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR; import static com.alibaba.testable.core.constant.ConstPool.THIS_REF; +import static com.alibaba.testable.core.util.CollectionUtil.contains; /** * @author flin @@ -32,9 +31,9 @@ public class OmniClassHandler extends BaseClassHandler { private static final String[] JUNIT_TEST_ANNOTATIONS = new String[] { JUnit4Framework.ANNOTATION_TEST, JUnit5Framework.ANNOTATION_TEST, JUnit5Framework.ANNOTATION_PARAMETERIZED_TEST }; - private static final Set UNREACHABLE_CLASSES = new HashSet() {{ - add(CLASS_ABSTRACT_COLLECTION); add(CLASS_NUMBER); - }}; + private static final String[] UNREACHABLE_CLASSES = new String[] { + CLASS_ABSTRACT_COLLECTION, CLASS_NUMBER + }; @Override protected void transform(ClassNode cn) { @@ -96,10 +95,8 @@ public class OmniClassHandler extends BaseClassHandler { continue; } for (AnnotationNode an : mn.visibleAnnotations) { - for (String annotation : JUNIT_TEST_ANNOTATIONS) { - if (an.desc.equals(annotation)) { - return true; - } + if (contains(JUNIT_TEST_ANNOTATIONS, an.desc)) { + return true; } } } @@ -120,7 +117,7 @@ public class OmniClassHandler extends BaseClassHandler { InsnList il = new InsnList(); il.add(start); il.add(new VarInsnNode(ALOAD, 0)); - if (UNREACHABLE_CLASSES.contains(cn.superName)) { + if (contains(UNREACHABLE_CLASSES, cn.superName)) { il.add(new MethodInsnNode(INVOKESPECIAL, cn.superName, CONSTRUCTOR, VOID_METHOD, false)); } else { il.add(new VarInsnNode(ALOAD, 1)); diff --git a/testable-core/src/main/java/com/alibaba/testable/core/model/ConstructionOption.java b/testable-core/src/main/java/com/alibaba/testable/core/model/ConstructionOption.java new file mode 100644 index 0000000..c0b7dc8 --- /dev/null +++ b/testable-core/src/main/java/com/alibaba/testable/core/model/ConstructionOption.java @@ -0,0 +1,7 @@ +package com.alibaba.testable.core.model; + +public enum ConstructionOption { + + ALLOW_NULL_FOR_NESTED_TYPE + +} 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 2ed8687..9686df7 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,6 +1,8 @@ package com.alibaba.testable.core.tool; import com.alibaba.testable.core.exception.ClassConstructionException; +import com.alibaba.testable.core.model.ConstructionOption; +import com.alibaba.testable.core.util.CollectionUtil; import com.alibaba.testable.core.util.LogUtil; import com.alibaba.testable.core.util.TypeUtil; @@ -8,6 +10,7 @@ import java.lang.reflect.*; import java.util.*; import static com.alibaba.testable.core.constant.ConstPool.DOLLAR; +import static com.alibaba.testable.core.model.ConstructionOption.ALLOW_NULL_FOR_NESTED_TYPE; /** * @author flin @@ -25,8 +28,12 @@ public class OmniConstructor { * @param clazz 期望的对象类型 * @return 返回新创建的对象 */ - public static T newInstance(Class clazz) { - return handleCircleReference(newInstance(clazz, new HashSet>(INITIAL_CAPACITY))); + public static T newInstance(Class clazz, ConstructionOption... options) { + T ins = newInstance(clazz, new HashSet>(INITIAL_CAPACITY)); + if (ins == null || CollectionUtil.contains(options, ALLOW_NULL_FOR_NESTED_TYPE)) { + return ins; + } + return handleCircleReference(ins); } /** @@ -36,8 +43,12 @@ public class OmniConstructor { * @param size 数组大小 * @return 返回新创建的对象数组 */ - public static T[] newArray(Class clazz, int size) { - return (T[])handleCircleReference(newArray(clazz, size, new HashSet>(INITIAL_CAPACITY))); + public static T[] newArray(Class clazz, int size, ConstructionOption... options) { + T[] array = (T[])newArray(clazz, size, new HashSet>(INITIAL_CAPACITY)); + if (CollectionUtil.contains(options, ALLOW_NULL_FOR_NESTED_TYPE)) { + return array; + } + return handleCircleReference(array); } private static T newInstance(Class clazz, Set> classPool) { diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java index 6f8e6f8..0c2bb61 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/CollectionUtil.java @@ -25,4 +25,12 @@ public class CollectionUtil { return sb.toString(); } + public static boolean contains(T[] collection, T target) { + for (T item : collection) { + if (target.equals(item)) { + return true; + } + } + return false; + } }