From 6473efd6525474c36fc5e87d072d10350d738174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Sat, 25 Jul 2020 23:20:55 +0800 Subject: [PATCH] get types instead of size --- .../testable/agent/handler/SourceClassHandler.java | 4 ++-- .../testable/agent/handler/TestClassHandler.java | 10 +++++----- .../com/alibaba/testable/agent/util/ClassUtil.java | 10 +++++----- .../com/alibaba/testable/agent/util/ClassUtilTest.java | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index 0959e21..8ee0620 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -96,7 +96,7 @@ public class SourceClassHandler extends ClassHandler { } private String getConstructorSubstitutionDesc(String constructorDesc) { - int paramCount = ClassUtil.getParameterCount(constructorDesc); + int paramCount = ClassUtil.getParameterTypes(constructorDesc).size(); return CONSTRUCTOR_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX; } @@ -116,7 +116,7 @@ public class SourceClassHandler extends ClassHandler { } private String getMethodSubstitutionDesc(String methodDesc) { - int paramCount = ClassUtil.getParameterCount(methodDesc); + int paramCount = ClassUtil.getParameterTypes(methodDesc).size(); return METHOD_DESC_PREFIX + StringUtil.repeat(OBJECT_DESC, paramCount) + METHOD_DESC_POSTFIX; } diff --git a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java index dbde623..c057bff 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java +++ b/agent/src/main/java/com/alibaba/testable/agent/handler/TestClassHandler.java @@ -12,16 +12,16 @@ import java.util.List; */ public class TestClassHandler extends ClassHandler { - private static final List testAnnotations = new ArrayList(); + private static final List TEST_ANNOTATIONS = new ArrayList(); private static final String TESTABLE_SETUP_METHOD_NAME = "testableSetup"; static { // JUnit4 - testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.Test")); + TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.Test")); // JUnit5 - testAnnotations.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test")); + TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.junit.jupiter.api.Test")); // TestNG - testAnnotations.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test")); + TEST_ANNOTATIONS.add(ClassUtil.toByteCodeClassName("org.testng.annotations.Test")); } @Override @@ -39,7 +39,7 @@ public class TestClassHandler extends ClassHandler { for (AnnotationNode n : mn.visibleAnnotations) { visibleAnnotationNames.add(n.desc); } - if (CollectionUtil.containsAny(visibleAnnotationNames, testAnnotations)) { + if (CollectionUtil.containsAny(visibleAnnotationNames, TEST_ANNOTATIONS)) { InsnList il = new InsnList(); il.add(new VarInsnNode(ALOAD, 0)); il.add(new MethodInsnNode(INVOKESPECIAL, cn.name, TESTABLE_SETUP_METHOD_NAME, "()V", false)); diff --git a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java index 18bce5c..229bd55 100644 --- a/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java +++ b/agent/src/main/java/com/alibaba/testable/agent/util/ClassUtil.java @@ -46,8 +46,8 @@ public class ClassUtil { } } - public static int getParameterCount(String desc) { - int paramCount = 0; + public static List getParameterTypes(String desc) { + List parameterTypes = new ArrayList(); boolean travelingClass = false; for (byte b : desc.getBytes()) { if (travelingClass) { @@ -57,16 +57,16 @@ public class ClassUtil { } else { if (b == TYPE_BYTE || b == TYPE_CHAR || b == TYPE_DOUBLE || b == TYPE_FLOAT || b == TYPE_INT || b == TYPE_LONG || b == TYPE_SHORT || b == TYPE_BOOL) { - paramCount++; + parameterTypes.add(b); } else if (b == TYPE_CLASS) { travelingClass = true; - paramCount++; + parameterTypes.add(b); } else if (b == PARAM_END) { break; } } } - return paramCount; + return parameterTypes; } public static String getReturnType(String desc) { diff --git a/agent/src/test/java/com/alibaba/testable/agent/util/ClassUtilTest.java b/agent/src/test/java/com/alibaba/testable/agent/util/ClassUtilTest.java index 692c1d9..5b2372c 100644 --- a/agent/src/test/java/com/alibaba/testable/agent/util/ClassUtilTest.java +++ b/agent/src/test/java/com/alibaba/testable/agent/util/ClassUtilTest.java @@ -8,10 +8,10 @@ class ClassUtilTest { @Test void should_able_to_get_parameter_count() { - assertEquals(1, ClassUtil.getParameterCount("(Ljava/lang/String;)V")); - assertEquals(6, ClassUtil.getParameterCount("(Ljava/lang/String;IDLjava/lang/String;ZLjava/net/URL;)V")); - assertEquals(10, ClassUtil.getParameterCount("(ZLjava/lang/String;IJFDCSBZ)V")); - assertEquals(3, ClassUtil.getParameterCount("(Ljava/lang/String;[I[Ljava/lang/String;)V")); + assertEquals(1, ClassUtil.getParameterTypes("(Ljava/lang/String;)V").size()); + assertEquals(6, ClassUtil.getParameterTypes("(Ljava/lang/String;IDLjava/lang/String;ZLjava/net/URL;)V").size()); + assertEquals(10, ClassUtil.getParameterTypes("(ZLjava/lang/String;IJFDCSBZ)V").size()); + assertEquals(3, ClassUtil.getParameterTypes("(Ljava/lang/String;[I[Ljava/lang/String;)V").size()); } @Test