From 196dffc73dbdb148a7af3455d319479832195fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Tue, 2 Mar 2021 23:18:27 +0800 Subject: [PATCH] print diagnose verbose using java style descriptor --- .../agent/constant/ByteCodeConst.java | 1 + .../testable/agent/util/MethodUtil.java | 68 ++++++++++++++++++- .../testable/agent/util/MethodUtilTest.java | 11 +++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ByteCodeConst.java b/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ByteCodeConst.java index aeb97bb..aa34d72 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ByteCodeConst.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/constant/ByteCodeConst.java @@ -14,6 +14,7 @@ public class ByteCodeConst { public static final byte TYPE_VOID = 'V'; public static final byte PARAM_END = ')'; public static final byte CLASS_END = ';'; + public static final byte PKG_SEGMENT = '/'; public static final byte TYPE_ARRAY = '['; } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/MethodUtil.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/MethodUtil.java index 2f02e7a..ba89eae 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/util/MethodUtil.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/util/MethodUtil.java @@ -137,6 +137,72 @@ public class MethodUtil { * @return java style descriptor */ private static String toJavaParameterDesc(String desc) { - return desc; + if (desc.isEmpty()) { + return ""; + } + StringBuilder sb = new StringBuilder(); + boolean isArray = false; + boolean isTravellingClass = false; + for (byte b : desc.getBytes()) { + if (isTravellingClass) { + switch (b) { + case CLASS_END: + sb.append(isArray ? "[]" : ""); + isArray = false; + isTravellingClass = false; + break; + case PKG_SEGMENT: + sb.append('.'); + break; + default: + sb.append((char)b); + } + } else { + switch (b) { + case TYPE_ARRAY: + isArray = true; + break; + case TYPE_BYTE: + sb.append(",byte").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_CHAR: + sb.append(",char").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_DOUBLE: + sb.append(",double").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_FLOAT: + sb.append(",float").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_INT: + sb.append(",int").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_LONG: + sb.append(",long").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_SHORT: + sb.append(",short").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_BOOL: + sb.append(",boolean").append(isArray ? "[]" : ""); + isArray = false; + break; + case TYPE_CLASS: + sb.append(","); + isTravellingClass = true; + break; + default: + break; + } + } + } + return sb.substring(1); } } diff --git a/testable-agent/src/test/java/com/alibaba/testable/agent/util/MethodUtilTest.java b/testable-agent/src/test/java/com/alibaba/testable/agent/util/MethodUtilTest.java index 0f8dc4f..9f94240 100644 --- a/testable-agent/src/test/java/com/alibaba/testable/agent/util/MethodUtilTest.java +++ b/testable-agent/src/test/java/com/alibaba/testable/agent/util/MethodUtilTest.java @@ -1,5 +1,6 @@ package com.alibaba.testable.agent.util; +import com.alibaba.testable.core.accessor.PrivateAccessor; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,5 +38,15 @@ class MethodUtilTest { assertEquals("", MethodUtil.getFirstParameter("()V")); } + @Test + void should_able_to_convert_bytecode_parameters() { + assertEquals("", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "")); + assertEquals("char", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "C")); + assertEquals("int,long", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "IJ")); + assertEquals("int[],long[]", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "[I[J")); + assertEquals("int,java.lang.String", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "ILjava/lang/String;")); + assertEquals("java.lang.String,int,long[]", PrivateAccessor.invokeStatic(MethodUtil.class, "toJavaParameterDesc", "Ljava/lang/String;I[J")); + } + }