From 7f8ef70e5719870d59e0af4dd017222e4088e4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 15 May 2021 15:20:16 +0800 Subject: [PATCH] move byte code logic to util class --- .../agent/handler/MockClassHandler.java | 44 ++------------- .../testable/agent/util/BytecodeUtil.java | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java index c4a4626..9445686 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/MockClassHandler.java @@ -336,13 +336,13 @@ public class MockClassHandler extends BaseClassWithContextHandler { InsnList il = new InsnList(); List types = MethodUtil.getParameterTypes(mn.desc); int size = types.size(); - il.add(getIntInsn(size)); + il.add(BytecodeUtil.getIntInsn(size)); il.add(new TypeInsnNode(ANEWARRAY, CLASS_OBJECT)); int parameterOffset = MethodUtil.isStatic(mn) ? 0 : 1; for (int i = 0; i < size; i++) { il.add(new InsnNode(DUP)); - il.add(getIntInsn(i)); - ImmutablePair code = getLoadParameterByteCode(types.get(i)); + il.add(BytecodeUtil.getIntInsn(i)); + ImmutablePair code = BytecodeUtil.getLoadParameterByteCode(types.get(i)); il.add(new VarInsnNode(code.left, parameterOffset)); parameterOffset += code.right; MethodInsnNode typeConvertMethodNode = ClassUtil.getPrimaryTypeConvertMethod(types.get(i)); @@ -370,42 +370,4 @@ public class MockClassHandler extends BaseClassWithContextHandler { return false; } - private static ImmutablePair getLoadParameterByteCode(Byte type) { - switch (type) { - case ByteCodeConst.TYPE_BYTE: - case ByteCodeConst.TYPE_CHAR: - case ByteCodeConst.TYPE_SHORT: - case ByteCodeConst.TYPE_INT: - case ByteCodeConst.TYPE_BOOL: - return ImmutablePair.of(ILOAD, 1); - case ByteCodeConst.TYPE_DOUBLE: - return ImmutablePair.of(DLOAD, 2); - case ByteCodeConst.TYPE_FLOAT: - return ImmutablePair.of(FLOAD, 1); - case ByteCodeConst.TYPE_LONG: - return ImmutablePair.of(LLOAD, 2); - default: - return ImmutablePair.of(ALOAD, 1); - } - } - - private AbstractInsnNode getIntInsn(int num) { - switch (num) { - case 0: - return new InsnNode(ICONST_0); - case 1: - return new InsnNode(ICONST_1); - case 2: - return new InsnNode(ICONST_2); - case 3: - return new InsnNode(ICONST_3); - case 4: - return new InsnNode(ICONST_4); - case 5: - return new InsnNode(ICONST_5); - default: - return new IntInsnNode(BIPUSH, num); - } - } - } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/BytecodeUtil.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/BytecodeUtil.java index 1a7eb3d..b574ca6 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/util/BytecodeUtil.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/util/BytecodeUtil.java @@ -1,6 +1,11 @@ package com.alibaba.testable.agent.util; +import com.alibaba.testable.agent.constant.ByteCodeConst; +import com.alibaba.testable.agent.tool.ImmutablePair; import com.alibaba.testable.core.util.LogUtil; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.IntInsnNode; import java.io.FileOutputStream; import java.io.IOException; @@ -221,4 +226,52 @@ public class BytecodeUtil { e.printStackTrace(); } } + + /** + * get load ops-code of specified type + * @param type type symbol + * @return pair of [ops-code, stack occupation] + */ + public static ImmutablePair getLoadParameterByteCode(Byte type) { + switch (type) { + case ByteCodeConst.TYPE_BYTE: + case ByteCodeConst.TYPE_CHAR: + case ByteCodeConst.TYPE_SHORT: + case ByteCodeConst.TYPE_INT: + case ByteCodeConst.TYPE_BOOL: + return ImmutablePair.of(ILOAD, 1); + case ByteCodeConst.TYPE_DOUBLE: + return ImmutablePair.of(DLOAD, 2); + case ByteCodeConst.TYPE_FLOAT: + return ImmutablePair.of(FLOAD, 1); + case ByteCodeConst.TYPE_LONG: + return ImmutablePair.of(LLOAD, 2); + default: + return ImmutablePair.of(ALOAD, 1); + } + } + + /** + * get ops code of load a int number + * @param num number to load + * @return ops code + */ + public static AbstractInsnNode getIntInsn(int num) { + switch (num) { + case 0: + return new InsnNode(ICONST_0); + case 1: + return new InsnNode(ICONST_1); + case 2: + return new InsnNode(ICONST_2); + case 3: + return new InsnNode(ICONST_3); + case 4: + return new InsnNode(ICONST_4); + case 5: + return new InsnNode(ICONST_5); + default: + return new IntInsnNode(BIPUSH, num); + } + } }