mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-09-04 05:32:58 +08:00
fit for kotlin auto generated accessor method
This commit is contained in:
@@ -20,6 +20,9 @@ public class ConstPool {
|
||||
|
||||
public static final String CGLIB_CLASS_INFIX = "$$EnhancerBy";
|
||||
|
||||
public static final String KOTLIN_POSTFIX_COMPANION = "$Companion";
|
||||
public static final String KOTLIN_PREFIX_ACCESS = "access$";
|
||||
|
||||
/**
|
||||
* Name of the constructor method
|
||||
*/
|
||||
|
||||
@@ -107,7 +107,10 @@ public class SourceClassHandler extends BaseClassHandler {
|
||||
private MethodInfo getMemberInjectMethodName(Set<MethodInfo> memberInjectMethods, MethodInsnNode node) {
|
||||
for (MethodInfo m : memberInjectMethods) {
|
||||
String nodeOwner = ClassUtil.fitCompanionClassName(node.owner);
|
||||
if (m.getClazz().equals(nodeOwner) && m.getName().equals(node.name) && m.getDesc().equals(node.desc)) {
|
||||
String nodeName = ClassUtil.fitKotlinAccessorName(node.name);
|
||||
// Kotlin accessor method will append a extra type parameter
|
||||
String nodeDesc = nodeName.equals(node.name) ? node.desc : ClassUtil.removeFirstParameter(node.desc);
|
||||
if (m.getClazz().equals(nodeOwner) && m.getName().equals(nodeName) && m.getDesc().equals(nodeDesc)) {
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public class ClassUtil {
|
||||
* @return is companion class or not
|
||||
*/
|
||||
public static boolean isCompanionClassName(String name) {
|
||||
return name.endsWith("$Companion");
|
||||
return name.endsWith(ConstPool.KOTLIN_POSTFIX_COMPANION);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +70,18 @@ public class ClassUtil {
|
||||
* @return original name
|
||||
*/
|
||||
public static String fitCompanionClassName(String name) {
|
||||
return name.replaceAll("\\$Companion$", "");
|
||||
return isCompanionClassName(name) ?
|
||||
name.substring(0, name.length() - ConstPool.KOTLIN_POSTFIX_COMPANION.length()) : name;
|
||||
}
|
||||
|
||||
/**
|
||||
* fit kotlin accessor method name to original name
|
||||
* @param name a accessor name (which could be a common kotlin method)
|
||||
* @return original name
|
||||
*/
|
||||
public static String fitKotlinAccessorName(String name) {
|
||||
return name.startsWith(ConstPool.KOTLIN_PREFIX_ACCESS) ?
|
||||
name.substring(ConstPool.KOTLIN_PREFIX_ACCESS.length()) : name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,6 +209,15 @@ public class ClassUtil {
|
||||
return toSlashSeparatedName(className).substring(1, className.length() - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* remove first parameter from method descriptor
|
||||
* @param desc original descriptor
|
||||
* @return descriptor without first parameter
|
||||
*/
|
||||
public static String removeFirstParameter(String desc) {
|
||||
return "(" + desc.substring(desc.indexOf(";") + 1);
|
||||
}
|
||||
|
||||
private static String toDescriptor(Byte type, String objectType) {
|
||||
return "(" + (char)type.byteValue() + ")L" + objectType + ";";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user