use pre-check to reduce unnecessary verbose parameter calculation

This commit is contained in:
金戟 2021-03-02 23:26:50 +08:00
parent 196dffc73d
commit f8450d7047
4 changed files with 26 additions and 10 deletions

View File

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

View File

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

View File

@ -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);
}
/**

View File

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