mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-11 04:40:12 +08:00
dynamically update testable inject method access
This commit is contained in:
parent
efccedc996
commit
ce0e7e4d3a
@ -27,14 +27,27 @@ public class TestClassHandler extends BaseClassHandler {
|
|||||||
for (AnnotationNode n : mn.visibleAnnotations) {
|
for (AnnotationNode n : mn.visibleAnnotations) {
|
||||||
visibleAnnotationNames.add(n.desc);
|
visibleAnnotationNames.add(n.desc);
|
||||||
}
|
}
|
||||||
if (!visibleAnnotationNames.contains(ConstPool.TESTABLE_INJECT) && couldBeTestMethod(mn)) {
|
if (visibleAnnotationNames.contains(ClassUtil.toByteCodeClassName(ConstPool.TESTABLE_INJECT))) {
|
||||||
InsnList il = new InsnList();
|
mn.access &= ~ACC_PRIVATE;
|
||||||
il.add(new VarInsnNode(ALOAD, 0));
|
mn.access &= ~ACC_PROTECTED;
|
||||||
il.add(new FieldInsnNode(PUTSTATIC, cn.name, ConstPool.TESTABLE_INJECT_REF, ClassUtil.toByteCodeClassName(cn.name)));
|
mn.access |= ACC_PUBLIC;
|
||||||
mn.instructions.insertBefore(mn.instructions.get(0), il);
|
} 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) {
|
private boolean couldBeTestMethod(MethodNode mn) {
|
||||||
return (mn.access & (ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC)) == 0 ;
|
return (mn.access & (ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC)) == 0 ;
|
||||||
}
|
}
|
||||||
|
@ -77,19 +77,6 @@ public class EnableTestableTranslator extends BaseTranslator {
|
|||||||
super.visitExec(jcExpressionStatement);
|
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
|
* Generate test setup method to initialize n.e.pool
|
||||||
*/
|
*/
|
||||||
|
@ -1,27 +1,12 @@
|
|||||||
package com.alibaba.testable.core.util;
|
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.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author flin
|
* @author flin
|
||||||
*/
|
*/
|
||||||
public class TypeUtil {
|
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
|
* get classes of parameter objects
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user