mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
travel all methods before handle testable injection
This commit is contained in:
parent
0410990a90
commit
8e95fefb58
@ -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 {
|
||||
|
@ -15,36 +15,11 @@ import com.sun.tools.javac.util.Name;
|
||||
public class EnableTestableInjectTranslator extends BaseTranslator {
|
||||
|
||||
private final TestableContext cx;
|
||||
private List<JCMethodDecl> methods;
|
||||
|
||||
/**
|
||||
* Methods to inject
|
||||
*/
|
||||
private List<JCMethodDecl> methods = List.nil();
|
||||
|
||||
/**
|
||||
* Fields to wrap
|
||||
*/
|
||||
private List<JCVariableDecl> fields = List.nil();
|
||||
|
||||
public List<JCMethodDecl> getMethods() {
|
||||
return methods;
|
||||
}
|
||||
|
||||
public List<JCVariableDecl> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
public EnableTestableInjectTranslator(TestableContext cx) {
|
||||
public EnableTestableInjectTranslator(TestableContext cx, List<JCMethodDecl> 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)) {
|
||||
|
@ -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<JCMethodDecl> methods = List.nil();
|
||||
|
||||
public List<JCMethodDecl> getMethods() {
|
||||
return methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record all methods
|
||||
*/
|
||||
@Override
|
||||
public void visitMethodDef(JCMethodDecl jcMethodDecl) {
|
||||
super.visitMethodDef(jcMethodDecl);
|
||||
methods = methods.append(jcMethodDecl);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user