diff --git a/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java b/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java index bd5b25f..ce8638d 100644 --- a/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java +++ b/core/src/main/java/com/alibaba/testable/processor/EnableTestableInjectProcessor.java @@ -3,6 +3,7 @@ package com.alibaba.testable.processor; import com.alibaba.testable.annotation.EnableTestableInject; import com.alibaba.testable.generator.StaticNewClassGenerator; import com.alibaba.testable.translator.EnableTestableInjectTranslator; +import com.alibaba.testable.translator.MethodRecordTranslator; import com.alibaba.testable.util.ConstPool; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.tree.JCTree; @@ -72,8 +73,9 @@ public class EnableTestableInjectProcessor extends BaseProcessor { private void processClassElement(Symbol.ClassSymbol clazz) { JCTree tree = cx.trees.getTree(clazz); - EnableTestableInjectTranslator translator = new EnableTestableInjectTranslator(cx); - tree.accept(translator); + MethodRecordTranslator methodRecordTranslator = new MethodRecordTranslator(); + tree.accept(methodRecordTranslator); + tree.accept(new EnableTestableInjectTranslator(cx, methodRecordTranslator.getMethods())); } private void writeSourceFile(String fullQualityTypeName, String content) throws IOException { diff --git a/core/src/main/java/com/alibaba/testable/translator/EnableTestableInjectTranslator.java b/core/src/main/java/com/alibaba/testable/translator/EnableTestableInjectTranslator.java index f4dae4b..ff33d12 100644 --- a/core/src/main/java/com/alibaba/testable/translator/EnableTestableInjectTranslator.java +++ b/core/src/main/java/com/alibaba/testable/translator/EnableTestableInjectTranslator.java @@ -15,36 +15,11 @@ import com.sun.tools.javac.util.Name; public class EnableTestableInjectTranslator extends BaseTranslator { private final TestableContext cx; + private List methods; - /** - * Methods to inject - */ - private List methods = List.nil(); - - /** - * Fields to wrap - */ - private List fields = List.nil(); - - public List getMethods() { - return methods; - } - - public List getFields() { - return fields; - } - - public EnableTestableInjectTranslator(TestableContext cx) { + public EnableTestableInjectTranslator(TestableContext cx, List methods) { this.cx = cx; - } - - /** - * Record all methods - */ - @Override - public void visitMethodDef(JCMethodDecl jcMethodDecl) { - super.visitMethodDef(jcMethodDecl); - methods = methods.append(jcMethodDecl); + this.methods = methods; } /** @@ -68,15 +43,11 @@ public class EnableTestableInjectTranslator extends BaseTranslator { } /** - * Record all private fields * Demo d = new Demo() -> Demo d = n.e.w(Demo.class) * Demo d = member() -> Demo d = n.e.f(this, "member") */ @Override public void visitVarDef(JCVariableDecl jcVariableDecl) { - if (isStubbornField(jcVariableDecl.mods)) { - fields = fields.append(jcVariableDecl); - } jcVariableDecl.init = checkAndExchange(jcVariableDecl.init); super.visitVarDef(jcVariableDecl); } @@ -117,11 +88,6 @@ public class EnableTestableInjectTranslator extends BaseTranslator { super.visitNewArray(jcNewArray); } - private boolean isStubbornField(JCModifiers mods) { - return mods.getFlags().contains(javax.lang.model.element.Modifier.PRIVATE) || - mods.getFlags().contains(javax.lang.model.element.Modifier.FINAL); - } - @Override protected JCExpression checkAndExchange(JCExpression expr) { if (isNewOperation(expr)) { diff --git a/core/src/main/java/com/alibaba/testable/translator/MethodRecordTranslator.java b/core/src/main/java/com/alibaba/testable/translator/MethodRecordTranslator.java new file mode 100644 index 0000000..98a08b3 --- /dev/null +++ b/core/src/main/java/com/alibaba/testable/translator/MethodRecordTranslator.java @@ -0,0 +1,32 @@ +package com.alibaba.testable.translator; + +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.TreeTranslator; +import com.sun.tools.javac.util.List; + +/** + * Travel AST + * + * @author flin + */ +public class MethodRecordTranslator extends TreeTranslator { + + /** + * Member methods + */ + private List methods = List.nil(); + + public List getMethods() { + return methods; + } + + /** + * Record all methods + */ + @Override + public void visitMethodDef(JCMethodDecl jcMethodDecl) { + super.visitMethodDef(jcMethodDecl); + methods = methods.append(jcMethodDecl); + } + +}