From 9c5be87f42b976565452900309f19eef92a37826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Wed, 22 Jul 2020 23:57:23 +0800 Subject: [PATCH] refactor and add comments --- .../java/com/alibaba/testable/PreMain.java | 5 +++-- .../handler/TestableClassHandler.java | 19 ++++++++----------- .../transformer/TestableClassTransformer.java | 7 ++++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/agent/src/main/java/com/alibaba/testable/PreMain.java b/agent/src/main/java/com/alibaba/testable/PreMain.java index 09020f7..8459c0c 100755 --- a/agent/src/main/java/com/alibaba/testable/PreMain.java +++ b/agent/src/main/java/com/alibaba/testable/PreMain.java @@ -1,16 +1,17 @@ package com.alibaba.testable; -import com.alibaba.testable.transformer.TestableFileTransformer; +import com.alibaba.testable.transformer.TestableClassTransformer; import java.lang.instrument.Instrumentation; /** + * Agent entry, dynamically modify the byte code of classes under testing * @author flin */ public class PreMain { public static void premain(String agentArgs, Instrumentation inst) { - inst.addTransformer(new TestableFileTransformer()); + inst.addTransformer(new TestableClassTransformer()); } } diff --git a/agent/src/main/java/com/alibaba/testable/handler/TestableClassHandler.java b/agent/src/main/java/com/alibaba/testable/handler/TestableClassHandler.java index dbb6659..bf99b9c 100644 --- a/agent/src/main/java/com/alibaba/testable/handler/TestableClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/handler/TestableClassHandler.java @@ -1,4 +1,4 @@ -package com.alibaba.testable.transformer; +package com.alibaba.testable.handler; import com.alibaba.testable.model.TravelStatus; import com.alibaba.testable.util.ClassUtil; @@ -17,25 +17,22 @@ import static com.alibaba.testable.constant.Const.SYS_CLASSES; /** * @author flin */ -public class TestableClassTransformer implements Opcodes { +public class TestableClassHandler implements Opcodes { private static final String CONSTRUCTOR = ""; 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); + ClassNode cn = new ClassNode(); cr.accept(cn, 0); - } - - public byte[] getBytes() { - transform(); + transform(cn); ClassWriter cw = new ClassWriter( 0); cn.accept(cw); return cw.toByteArray(); } - private void transform() { + private void transform(ClassNode cn) { List methodNames = new ArrayList(); for (MethodNode m : cn.methods) { if (!CONSTRUCTOR.equals(m.name)) { @@ -43,11 +40,11 @@ public class TestableClassTransformer implements Opcodes { } } for (MethodNode m : cn.methods) { - transformMethod(m, methodNames); + transformMethod(cn, m, methodNames); } } - private void transformMethod(MethodNode mn, List methodNames) { + private void transformMethod(ClassNode cn, MethodNode mn, List methodNames) { AbstractInsnNode[] instructions = mn.instructions.toArray(); TravelStatus status = TravelStatus.INIT; String target = ""; diff --git a/agent/src/main/java/com/alibaba/testable/transformer/TestableClassTransformer.java b/agent/src/main/java/com/alibaba/testable/transformer/TestableClassTransformer.java index 47e8197..eb4d989 100644 --- a/agent/src/main/java/com/alibaba/testable/transformer/TestableClassTransformer.java +++ b/agent/src/main/java/com/alibaba/testable/transformer/TestableClassTransformer.java @@ -1,5 +1,6 @@ package com.alibaba.testable.transformer; +import com.alibaba.testable.handler.TestableClassHandler; import com.alibaba.testable.util.ClassUtil; import java.io.IOException; @@ -13,7 +14,7 @@ import java.util.Set; /** * @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_INJECT = "com.alibaba.testable.annotation.EnableTestableInject"; @@ -22,7 +23,7 @@ public class TestableFileTransformer implements ClassFileTransformer { private static final Set loadedClassNames = new HashSet(); public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, - ProtectionDomain protectionDomain, byte[] classfileBuffer) { + ProtectionDomain protectionDomain, byte[] classFileBuffer) { if (isSystemClass(loader, className) || loadedClassNames.contains(className)) { // Ignore system class and duplicate class return null; @@ -37,7 +38,7 @@ public class TestableFileTransformer implements ClassFileTransformer { try { loadedClassNames.add(className); - return new TestableClassTransformer(className).getBytes(); + return new TestableClassHandler().getBytes(className); } catch (IOException e) { return null; }