get types instead of size

This commit is contained in:
金戟 2020-07-25 23:20:55 +08:00
parent 6009326150
commit 6473efd652
4 changed files with 16 additions and 16 deletions

View File

@ -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;
}

View File

@ -12,16 +12,16 @@ import java.util.List;
*/
public class TestClassHandler extends ClassHandler {
private static final List<String> testAnnotations = new ArrayList<String>();
private static final List<String> TEST_ANNOTATIONS = new ArrayList<String>();
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));

View File

@ -46,8 +46,8 @@ public class ClassUtil {
}
}
public static int getParameterCount(String desc) {
int paramCount = 0;
public static List<Byte> getParameterTypes(String desc) {
List<Byte> parameterTypes = new ArrayList<Byte>();
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) {

View File

@ -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