diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index 67a49d1..d53bd33 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -27,14 +27,27 @@ public class TestClassHandler extends BaseClassHandler { for (AnnotationNode n : mn.visibleAnnotations) { visibleAnnotationNames.add(n.desc); } - if (!visibleAnnotationNames.contains(ConstPool.TESTABLE_INJECT) && couldBeTestMethod(mn)) { - InsnList il = new InsnList(); - il.add(new VarInsnNode(ALOAD, 0)); - il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, ClassUtil.toByteCodeClassName(cn.name))); - mn.instructions.insertBefore(mn.instructions.get(0), il); + if (visibleAnnotationNames.contains(ClassUtil.toByteCodeClassName(ConstPool.TESTABLE_INJECT))) { + mn.access &= ~ACC_PRIVATE; + mn.access &= ~ACC_PROTECTED; + mn.access |= ACC_PUBLIC; + } else if (couldBeTestMethod(mn)) { + injectTestableRef(cn, mn); } } + private void injectTestableRef(ClassNode cn, MethodNode mn) { + InsnList il = new InsnList(); + il.add(new VarInsnNode(ALOAD, 0)); + il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, + ClassUtil.toByteCodeClassName(cn.name))); + mn.instructions.insertBefore(mn.instructions.get(0), il); + } + + /** + * Different unit test framework may have different @Test annotation + * but they should always NOT private, protected or static + */ private boolean couldBeTestMethod(MethodNode mn) { return (mn.access & (ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC)) == 0 ; } diff --git a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java index 19a6319..4d56bcd 100644 --- a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java +++ b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java @@ -77,19 +77,6 @@ public class EnableTestableTranslator extends BaseTranslator { super.visitExec(jcExpressionStatement); } - /** - * Search for TestableInject annotations - */ - @Override - public void visitMethodDef(JCMethodDecl jcMethodDecl) { - for (JCAnnotation a : jcMethodDecl.mods.annotations) { - if (a.type != null && ConstPool.ANNOTATION_TESTABLE_INJECT.equals(a.type.tsym.toString())) { - TypeUtil.toPublicFlags(jcMethodDecl.getModifiers()); - } - } - super.visitMethodDef(jcMethodDecl); - } - /** * Generate test setup method to initialize n.e.pool */ diff --git a/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java b/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java index b86a8a2..2a63695 100644 --- a/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java +++ b/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java @@ -1,27 +1,12 @@ package com.alibaba.testable.core.util; -import com.sun.tools.javac.tree.JCTree; - -import java.lang.reflect.Constructor; import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; /** * @author flin */ public class TypeUtil { - /** - * Convert modifier to public - */ - public static void toPublicFlags(JCTree.JCModifiers modifiers) { - modifiers.flags &= ~Modifier.PRIVATE; - modifiers.flags &= ~Modifier.PROTECTED; - modifiers.flags |= Modifier.PUBLIC; - } - /** * get classes of parameter objects */