fix check-cast byte code format

This commit is contained in:
金戟 2021-03-13 00:38:00 +08:00
parent edd4c24d88
commit 323ab398b2
5 changed files with 24 additions and 15 deletions

View File

@ -16,7 +16,7 @@ import java.util.List;
import static com.alibaba.testable.agent.constant.ByteCodeConst.TYPE_ARRAY; import static com.alibaba.testable.agent.constant.ByteCodeConst.TYPE_ARRAY;
import static com.alibaba.testable.agent.constant.ByteCodeConst.TYPE_CLASS; import static com.alibaba.testable.agent.constant.ByteCodeConst.TYPE_CLASS;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName; import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR; import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR;
/** /**
@ -178,7 +178,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
il.add(new InsnNode(POP)); il.add(new InsnNode(POP));
il.add(new InsnNode(RETURN)); il.add(new InsnNode(RETURN));
} else if (returnType.charAt(0) == TYPE_ARRAY ||returnType.charAt(0) == TYPE_CLASS) { } else if (returnType.charAt(0) == TYPE_ARRAY ||returnType.charAt(0) == TYPE_CLASS) {
il.add(new TypeInsnNode(CHECKCAST, returnType)); il.add(new TypeInsnNode(CHECKCAST, ClassUtil.toSlashSeparateJavaStyleName(returnType)));
il.add(new InsnNode(ARETURN)); il.add(new InsnNode(ARETURN));
} else { } else {
String wrapperClass = ClassUtil.toWrapperClass(returnType.getBytes()[0]); String wrapperClass = ClassUtil.toWrapperClass(returnType.getBytes()[0]);
@ -284,7 +284,7 @@ public class MockClassHandler extends BaseClassWithContextHandler {
private boolean isMockForConstructor(MethodNode mn) { private boolean isMockForConstructor(MethodNode mn) {
for (AnnotationNode an : mn.visibleAnnotations) { for (AnnotationNode an : mn.visibleAnnotations) {
String annotationName = toDotSeparateFullClassName(an.desc); String annotationName = toJavaStyleClassName(an.desc);
if (ConstPool.MOCK_CONSTRUCTOR.equals(annotationName)) { if (ConstPool.MOCK_CONSTRUCTOR.equals(annotationName)) {
return true; return true;
} else if (ConstPool.MOCK_METHOD.equals(annotationName)) { } else if (ConstPool.MOCK_METHOD.equals(annotationName)) {

View File

@ -16,7 +16,7 @@ import org.objectweb.asm.tree.MethodNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName; import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
import static com.alibaba.testable.agent.util.MethodUtil.isStatic; import static com.alibaba.testable.agent.util.MethodUtil.isStatic;
import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR; import static com.alibaba.testable.core.constant.ConstPool.CONSTRUCTOR;
@ -57,7 +57,7 @@ public class MockClassParser {
for (MethodNode mn : cn.methods) { for (MethodNode mn : cn.methods) {
if (mn.visibleAnnotations != null) { if (mn.visibleAnnotations != null) {
for (AnnotationNode an : mn.visibleAnnotations) { for (AnnotationNode an : mn.visibleAnnotations) {
String fullClassName = toDotSeparateFullClassName(an.desc); String fullClassName = toJavaStyleClassName(an.desc);
if (fullClassName.equals(ConstPool.MOCK_METHOD) || if (fullClassName.equals(ConstPool.MOCK_METHOD) ||
fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) { fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
return true; return true;
@ -84,11 +84,11 @@ public class MockClassParser {
return; return;
} }
for (AnnotationNode an : mn.visibleAnnotations) { for (AnnotationNode an : mn.visibleAnnotations) {
String fullClassName = toDotSeparateFullClassName(an.desc); String fullClassName = toJavaStyleClassName(an.desc);
if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) { if (fullClassName.equals(ConstPool.MOCK_CONSTRUCTOR)) {
if (LogUtil.isVerboseEnabled()) { if (LogUtil.isVerboseEnabled()) {
LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc( LogUtil.verbose(" Mock constructor \"%s\" as \"%s\"", mn.name, MethodUtil.toJavaMethodDesc(
ClassUtil.toDotSeparateFullClassName(MethodUtil.getReturnType(mn.desc)), mn.desc)); ClassUtil.toJavaStyleClassName(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)) {

View File

@ -23,7 +23,7 @@ import java.security.ProtectionDomain;
import java.util.List; import java.util.List;
import static com.alibaba.testable.agent.constant.ConstPool.*; import static com.alibaba.testable.agent.constant.ConstPool.*;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName; import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
import static com.alibaba.testable.core.constant.ConstPool.TEST_POSTFIX; import static com.alibaba.testable.core.constant.ConstPool.TEST_POSTFIX;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC; import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC; import static org.objectweb.asm.Opcodes.ACC_STATIC;
@ -214,7 +214,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
private String lookForMockWithAnnotationAsTestClass(ClassNode cn) { private String lookForMockWithAnnotationAsTestClass(ClassNode cn) {
String mockClassName = parseMockWithAnnotation(cn, ClassType.TestClass); String mockClassName = parseMockWithAnnotation(cn, ClassType.TestClass);
if (mockClassName != null) { if (mockClassName != null) {
MockAssociationUtil.mockToTests.get(mockClassName).add(ClassUtil.toDotSeparateFullClassName(cn.name)); MockAssociationUtil.mockToTests.get(mockClassName).add(ClassUtil.toJavaStyleClassName(cn.name));
return ClassUtil.toSlashSeparatedName(mockClassName); return ClassUtil.toSlashSeparatedName(mockClassName);
} }
return null; return null;
@ -230,7 +230,7 @@ public class TestableClassTransformer implements ClassFileTransformer {
if (cn.visibleAnnotations != null) { if (cn.visibleAnnotations != null) {
for (AnnotationNode an : cn.visibleAnnotations) { for (AnnotationNode an : cn.visibleAnnotations) {
DiagnoseUtil.setupByAnnotation(an); DiagnoseUtil.setupByAnnotation(an);
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.MOCK_WITH)) { if (toJavaStyleClassName(an.desc).equals(ConstPool.MOCK_WITH)) {
ClassType type = AnnotationUtil.getAnnotationParameter(an, FIELD_TREAT_AS, ClassType.GuessByName, ClassType type = AnnotationUtil.getAnnotationParameter(an, FIELD_TREAT_AS, ClassType.GuessByName,
ClassType.class); ClassType.class);
if (isExpectedType(cn.name, type, expectedType)) { if (isExpectedType(cn.name, type, expectedType)) {

View File

@ -201,13 +201,22 @@ public class ClassUtil {
return (char)TYPE_CLASS + toSlashSeparatedName(className) + (char)CLASS_END; return (char)TYPE_CLASS + toSlashSeparatedName(className) + (char)CLASS_END;
} }
/**
* convert byte code class name to slash separated human readable name
* @param className original name
* @return converted name
*/
public static String toSlashSeparateJavaStyleName(String className) {
return className.substring(1, className.length() - 1);
}
/** /**
* convert byte code class name to dot separated human readable name * convert byte code class name to dot separated human readable name
* @param className original name * @param className original name
* @return converted name * @return converted name
*/ */
public static String toDotSeparateFullClassName(String className) { public static String toJavaStyleClassName(String className) {
return toDotSeparatedName(className).substring(1, className.length() - 1); return toDotSeparatedName(toSlashSeparateJavaStyleName(className));
} }
/** /**

View File

@ -7,7 +7,7 @@ import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import static com.alibaba.testable.agent.constant.ConstPool.MOCK_WITH; import static com.alibaba.testable.agent.constant.ConstPool.MOCK_WITH;
import static com.alibaba.testable.agent.util.ClassUtil.toDotSeparateFullClassName; import static com.alibaba.testable.agent.util.ClassUtil.toJavaStyleClassName;
public class DiagnoseUtil { public class DiagnoseUtil {
@ -19,7 +19,7 @@ public class DiagnoseUtil {
return; return;
} }
for (AnnotationNode an : cn.visibleAnnotations) { for (AnnotationNode an : cn.visibleAnnotations) {
if (toDotSeparateFullClassName(an.desc).equals(ConstPool.MOCK_DIAGNOSE)) { if (toJavaStyleClassName(an.desc).equals(ConstPool.MOCK_DIAGNOSE)) {
setupDiagnose(an, FIELD_VALUE); setupDiagnose(an, FIELD_VALUE);
} }
} }
@ -27,7 +27,7 @@ public class DiagnoseUtil {
public static void setupByAnnotation(AnnotationNode an) { public static void setupByAnnotation(AnnotationNode an) {
// to be remove in v0.6 // to be remove in v0.6
if (toDotSeparateFullClassName(an.desc).equals(MOCK_WITH)) { if (toJavaStyleClassName(an.desc).equals(MOCK_WITH)) {
setupDiagnose(an, FIELD_DIAGNOSE); setupDiagnose(an, FIELD_DIAGNOSE);
} }
} }