From f8450d70473c033d8fdfbb2af631e7059e5fe1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Tue, 2 Mar 2021 23:26:50 +0800 Subject: [PATCH] use pre-check to reduce unnecessary verbose parameter calculation --- .../testable/agent/handler/SourceClassHandler.java | 12 ++++++++---- .../testable/agent/transformer/MockClassParser.java | 12 ++++++++---- .../com/alibaba/testable/agent/util/MethodUtil.java | 3 ++- .../java/com/alibaba/testable/core/util/LogUtil.java | 9 ++++++++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index 1cf5c5e..0257d58 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -67,8 +67,10 @@ public class SourceClassHandler extends BaseClassHandler { if (invokeOps.contains(instructions[i].getOpcode())) { MethodInsnNode node = (MethodInsnNode)instructions[i]; if (CONSTRUCTOR.equals(node.name)) { - LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i), - MethodUtil.toJavaMethodDesc(node.owner, node.desc)); + if (LogUtil.isVerboseEnabled()) { + LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i), + MethodUtil.toJavaMethodDesc(node.owner, node.desc)); + } MethodInfo newOperatorInjectMethod = getNewOperatorInjectMethod(newOperatorInjectMethods, node); if (newOperatorInjectMethod != null) { // it's a new operation and an inject method for it exist @@ -82,8 +84,10 @@ public class SourceClassHandler extends BaseClassHandler { } } } else { - LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i), - MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc)); + if (LogUtil.isVerboseEnabled()) { + LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i), + MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc)); + } MethodInfo mockMethod = getMemberInjectMethodName(memberInjectMethods, node); if (mockMethod != null) { // it's a member or static method and an inject method for it exist diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java index 46cf2f0..62fc710 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/MockClassParser.java @@ -86,12 +86,16 @@ public class MockClassParser { for (AnnotationNode an : mn.visibleAnnotations) { String fullClassName = toDotSeparateFullClassName(an.desc); if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) { - LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, - MethodUtil.toJavaMethodDesc(MethodUtil.getReturnType(mn.desc), mn.desc)); + if (LogUtil.isVerboseEnabled()) { + LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc( + ClassUtil.toDotSeparateFullClassName(MethodUtil.getReturnType(mn.desc)), mn.desc)); + } addMockConstructor(methodInfos, cn, mn); } else if (fullClassName.equals(ConstPool.MOCK_METHOD) && AnnotationUtil.isValidMockMethod(mn, an)) { - LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc( - getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an))); + if (LogUtil.isVerboseEnabled()) { + LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc( + getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an))); + } String targetMethod = AnnotationUtil.getAnnotationParameter( an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class); if (CONSTRUCTOR.equals(targetMethod)) { 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 ba89eae..42b9b5e 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 @@ -113,8 +113,9 @@ public class MethodUtil { * @return java style constructor descriptor */ public static String toJavaMethodDesc(String owner, String desc) { + String ownerInDotFormat = ClassUtil.toDotSeparatedName(owner); String parameters = toJavaParameterDesc(extractParameters(desc)); - return String.format("%s(%s)", owner, parameters); + return String.format("%s(%s)", ownerInDotFormat, parameters); } /** diff --git a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java index 09fcea3..0e8b182 100644 --- a/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java +++ b/testable-core/src/main/java/com/alibaba/testable/core/util/LogUtil.java @@ -33,7 +33,7 @@ public class LogUtil { private static LogLevel currentLogLevel = LogLevel.LEVEL_WARN; public static void verbose(String msg, Object... args) { - if (currentLogLevel.level >= LogLevel.LEVEL_VERBOSE.level) { + if (isVerboseEnabled()) { System.out.println(String.format("[VERBOSE] " + msg, args)); } } @@ -54,6 +54,13 @@ public class LogUtil { System.err.println(String.format("[ERROR] " + msg, args)); } + /** + * a pre-check method for reduce verbose parameter calculation + */ + public static boolean isVerboseEnabled() { + return currentLogLevel.level >= LogLevel.LEVEL_VERBOSE.level; + } + public static void setLevel(LogLevel level) { currentLogLevel = level; }