mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-04-02 07:31:04 +08:00
use pre-check to reduce unnecessary verbose parameter calculation
This commit is contained in:
parent
196dffc73d
commit
f8450d7047
@ -67,8 +67,10 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
if (invokeOps.contains(instructions[i].getOpcode())) {
|
if (invokeOps.contains(instructions[i].getOpcode())) {
|
||||||
MethodInsnNode node = (MethodInsnNode)instructions[i];
|
MethodInsnNode node = (MethodInsnNode)instructions[i];
|
||||||
if (CONSTRUCTOR.equals(node.name)) {
|
if (CONSTRUCTOR.equals(node.name)) {
|
||||||
LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i),
|
if (LogUtil.isVerboseEnabled()) {
|
||||||
MethodUtil.toJavaMethodDesc(node.owner, node.desc));
|
LogUtil.verbose(" Line %d, constructing \"%s\"", getLineNum(instructions, i),
|
||||||
|
MethodUtil.toJavaMethodDesc(node.owner, node.desc));
|
||||||
|
}
|
||||||
MethodInfo newOperatorInjectMethod = getNewOperatorInjectMethod(newOperatorInjectMethods, node);
|
MethodInfo newOperatorInjectMethod = getNewOperatorInjectMethod(newOperatorInjectMethods, node);
|
||||||
if (newOperatorInjectMethod != null) {
|
if (newOperatorInjectMethod != null) {
|
||||||
// it's a new operation and an inject method for it exist
|
// it's a new operation and an inject method for it exist
|
||||||
@ -82,8 +84,10 @@ public class SourceClassHandler extends BaseClassHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i),
|
if (LogUtil.isVerboseEnabled()) {
|
||||||
MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc));
|
LogUtil.verbose(" Line %d, invoking \"%s\"", getLineNum(instructions, i),
|
||||||
|
MethodUtil.toJavaMethodDesc(node.owner, node.name, node.desc));
|
||||||
|
}
|
||||||
MethodInfo mockMethod = getMemberInjectMethodName(memberInjectMethods, node);
|
MethodInfo mockMethod = getMemberInjectMethodName(memberInjectMethods, node);
|
||||||
if (mockMethod != null) {
|
if (mockMethod != null) {
|
||||||
// it's a member or static method and an inject method for it exist
|
// it's a member or static method and an inject method for it exist
|
||||||
|
@ -86,12 +86,16 @@ public class MockClassParser {
|
|||||||
for (AnnotationNode an : mn.visibleAnnotations) {
|
for (AnnotationNode an : mn.visibleAnnotations) {
|
||||||
String fullClassName = toDotSeparateFullClassName(an.desc);
|
String fullClassName = toDotSeparateFullClassName(an.desc);
|
||||||
if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
|
||||||
LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name,
|
if (LogUtil.isVerboseEnabled()) {
|
||||||
MethodUtil.toJavaMethodDesc(MethodUtil.getReturnType(mn.desc), mn.desc));
|
LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
|
||||||
|
ClassUtil.toDotSeparateFullClassName(MethodUtil.getReturnType(mn.desc)), mn.desc));
|
||||||
|
}
|
||||||
addMockConstructor(methodInfos, cn, mn);
|
addMockConstructor(methodInfos, cn, mn);
|
||||||
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) && AnnotationUtil.isValidMockMethod(mn, an)) {
|
} else if (fullClassName.equals(ConstPool.MOCK_METHOD) && AnnotationUtil.isValidMockMethod(mn, an)) {
|
||||||
LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
|
if (LogUtil.isVerboseEnabled()) {
|
||||||
getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an)));
|
LogUtil.verbose(" Mock method \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
|
||||||
|
getTargetMethodOwner(mn, an), getTargetMethodName(mn, an), getTargetMethodDesc(mn, an)));
|
||||||
|
}
|
||||||
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
String targetMethod = AnnotationUtil.getAnnotationParameter(
|
||||||
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
an, ConstPool.FIELD_TARGET_METHOD, mn.name, String.class);
|
||||||
if (CONSTRUCTOR.equals(targetMethod)) {
|
if (CONSTRUCTOR.equals(targetMethod)) {
|
||||||
|
@ -113,8 +113,9 @@ public class MethodUtil {
|
|||||||
* @return java style constructor descriptor
|
* @return java style constructor descriptor
|
||||||
*/
|
*/
|
||||||
public static String toJavaMethodDesc(String owner, String desc) {
|
public static String toJavaMethodDesc(String owner, String desc) {
|
||||||
|
String ownerInDotFormat = ClassUtil.toDotSeparatedName(owner);
|
||||||
String parameters = toJavaParameterDesc(extractParameters(desc));
|
String parameters = toJavaParameterDesc(extractParameters(desc));
|
||||||
return String.format("%s(%s)", owner, parameters);
|
return String.format("%s(%s)", ownerInDotFormat, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +33,7 @@ public class LogUtil {
|
|||||||
private static LogLevel currentLogLevel = LogLevel.LEVEL_WARN;
|
private static LogLevel currentLogLevel = LogLevel.LEVEL_WARN;
|
||||||
|
|
||||||
public static void verbose(String msg, Object... args) {
|
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));
|
System.out.println(String.format("[VERBOSE] " + msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,6 +54,13 @@ public class LogUtil {
|
|||||||
System.err.println(String.format("[ERROR] " + msg, args));
|
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) {
|
public static void setLevel(LogLevel level) {
|
||||||
currentLogLevel = level;
|
currentLogLevel = level;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user