mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
refactor and add comments
This commit is contained in:
parent
9c757cebc1
commit
9c5be87f42
@ -1,16 +1,17 @@
|
|||||||
package com.alibaba.testable;
|
package com.alibaba.testable;
|
||||||
|
|
||||||
import com.alibaba.testable.transformer.TestableFileTransformer;
|
import com.alibaba.testable.transformer.TestableClassTransformer;
|
||||||
|
|
||||||
import java.lang.instrument.Instrumentation;
|
import java.lang.instrument.Instrumentation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Agent entry, dynamically modify the byte code of classes under testing
|
||||||
* @author flin
|
* @author flin
|
||||||
*/
|
*/
|
||||||
public class PreMain {
|
public class PreMain {
|
||||||
|
|
||||||
public static void premain(String agentArgs, Instrumentation inst) {
|
public static void premain(String agentArgs, Instrumentation inst) {
|
||||||
inst.addTransformer(new TestableFileTransformer());
|
inst.addTransformer(new TestableClassTransformer());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.alibaba.testable.transformer;
|
package com.alibaba.testable.handler;
|
||||||
|
|
||||||
import com.alibaba.testable.model.TravelStatus;
|
import com.alibaba.testable.model.TravelStatus;
|
||||||
import com.alibaba.testable.util.ClassUtil;
|
import com.alibaba.testable.util.ClassUtil;
|
||||||
@ -17,25 +17,22 @@ import static com.alibaba.testable.constant.Const.SYS_CLASSES;
|
|||||||
/**
|
/**
|
||||||
* @author flin
|
* @author flin
|
||||||
*/
|
*/
|
||||||
public class TestableClassTransformer implements Opcodes {
|
public class TestableClassHandler implements Opcodes {
|
||||||
|
|
||||||
private static final String CONSTRUCTOR = "<init>";
|
private static final String CONSTRUCTOR = "<init>";
|
||||||
private static final String TESTABLE_NE = "n/e";
|
private static final String TESTABLE_NE = "n/e";
|
||||||
private final ClassNode cn = new ClassNode();
|
|
||||||
|
|
||||||
public TestableClassTransformer(String className) throws IOException {
|
public byte[] getBytes(String className) throws IOException {
|
||||||
ClassReader cr = new ClassReader(className);
|
ClassReader cr = new ClassReader(className);
|
||||||
|
ClassNode cn = new ClassNode();
|
||||||
cr.accept(cn, 0);
|
cr.accept(cn, 0);
|
||||||
}
|
transform(cn);
|
||||||
|
|
||||||
public byte[] getBytes() {
|
|
||||||
transform();
|
|
||||||
ClassWriter cw = new ClassWriter( 0);
|
ClassWriter cw = new ClassWriter( 0);
|
||||||
cn.accept(cw);
|
cn.accept(cw);
|
||||||
return cw.toByteArray();
|
return cw.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void transform() {
|
private void transform(ClassNode cn) {
|
||||||
List<String> methodNames = new ArrayList<String>();
|
List<String> methodNames = new ArrayList<String>();
|
||||||
for (MethodNode m : cn.methods) {
|
for (MethodNode m : cn.methods) {
|
||||||
if (!CONSTRUCTOR.equals(m.name)) {
|
if (!CONSTRUCTOR.equals(m.name)) {
|
||||||
@ -43,11 +40,11 @@ public class TestableClassTransformer implements Opcodes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (MethodNode m : cn.methods) {
|
for (MethodNode m : cn.methods) {
|
||||||
transformMethod(m, methodNames);
|
transformMethod(cn, m, methodNames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void transformMethod(MethodNode mn, List<String> methodNames) {
|
private void transformMethod(ClassNode cn, MethodNode mn, List<String> methodNames) {
|
||||||
AbstractInsnNode[] instructions = mn.instructions.toArray();
|
AbstractInsnNode[] instructions = mn.instructions.toArray();
|
||||||
TravelStatus status = TravelStatus.INIT;
|
TravelStatus status = TravelStatus.INIT;
|
||||||
String target = "";
|
String target = "";
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.alibaba.testable.transformer;
|
package com.alibaba.testable.transformer;
|
||||||
|
|
||||||
|
import com.alibaba.testable.handler.TestableClassHandler;
|
||||||
import com.alibaba.testable.util.ClassUtil;
|
import com.alibaba.testable.util.ClassUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,7 +14,7 @@ import java.util.Set;
|
|||||||
/**
|
/**
|
||||||
* @author flin
|
* @author flin
|
||||||
*/
|
*/
|
||||||
public class TestableFileTransformer implements ClassFileTransformer {
|
public class TestableClassTransformer implements ClassFileTransformer {
|
||||||
|
|
||||||
private static final String ENABLE_TESTABLE = "com.alibaba.testable.annotation.EnableTestable";
|
private static final String ENABLE_TESTABLE = "com.alibaba.testable.annotation.EnableTestable";
|
||||||
private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.annotation.EnableTestableInject";
|
private static final String ENABLE_TESTABLE_INJECT = "com.alibaba.testable.annotation.EnableTestableInject";
|
||||||
@ -22,7 +23,7 @@ public class TestableFileTransformer implements ClassFileTransformer {
|
|||||||
private static final Set<String> loadedClassNames = new HashSet<String>();
|
private static final Set<String> loadedClassNames = new HashSet<String>();
|
||||||
|
|
||||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
||||||
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
|
ProtectionDomain protectionDomain, byte[] classFileBuffer) {
|
||||||
if (isSystemClass(loader, className) || loadedClassNames.contains(className)) {
|
if (isSystemClass(loader, className) || loadedClassNames.contains(className)) {
|
||||||
// Ignore system class and duplicate class
|
// Ignore system class and duplicate class
|
||||||
return null;
|
return null;
|
||||||
@ -37,7 +38,7 @@ public class TestableFileTransformer implements ClassFileTransformer {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
loadedClassNames.add(className);
|
loadedClassNames.add(className);
|
||||||
return new TestableClassTransformer(className).getBytes();
|
return new TestableClassHandler().getBytes(className);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user