@Test public void testMethodDeclaration() throws IOException { ClassNode classNode = AsmUtils.loadClass(TestClass.class); MethodNode sss = AsmUtils.findFirstMethod(classNode.methods, "sss"); MethodNode hello = AsmUtils.findFirstMethod(classNode.methods, "hello"); MethodNode constructor = AsmUtils.findFirstMethod(AsmUtils.loadClass(TestConstructorClass.class).methods, "<init>"); String helloDeclaration = AsmUtils.methodDeclaration(Type.getType(TestClass.class), hello); String sssDeclaration = AsmUtils.methodDeclaration(Type.getType(TestClass.class), sss); String constructorDeclaration = AsmUtils.methodDeclaration(Type.getType(TestConstructorClass.class), constructor); System.err.println(helloDeclaration); System.err.println(sssDeclaration); System.err.println(constructorDeclaration); Assertions.assertThat(helloDeclaration).isEqualTo("protected abstract java.lang.String hello(java.lang.String)"); Assertions.assertThat(sssDeclaration).isEqualTo( "public static synchronized java.util.List sss(int, long, java.util.List) throws java.io.IOException, java.lang.ArrayIndexOutOfBoundsException"); Assertions.assertThat(constructorDeclaration).isEqualTo("public com.taobao.arthas.bytekit.utils.AsmUtilsTest$TestConstructorClass(int, java.lang.String)"); }
public static String methodDeclaration(MethodInsnNode methodInsnNode) { StringBuilder sb = new StringBuilder(128); int opcode = methodInsnNode.getOpcode(); if(opcode == Opcodes.INVOKESTATIC) { sb.append("static "); } Type methodType = Type.getMethodType(methodInsnNode.desc); Type ownerType = Type.getObjectType(methodInsnNode.owner); if(methodInsnNode.name.equals("<init>")) { sb.append(ownerType.getClassName()); }else { sb.append(methodType.getReturnType().getClassName()).append(' '); sb.append(methodInsnNode.name); } sb.append('('); Type[] argumentTypes = methodType.getArgumentTypes(); for(int i = 0 ; i < argumentTypes.length; ++i) { sb.append(argumentTypes[i].getClassName()); if(i != argumentTypes.length - 1) { sb.append(", "); } } sb.append(')'); return sb.toString(); }
AsmUtils { public static String methodDeclaration(MethodInsnNode methodInsnNode) { StringBuilder sb = new StringBuilder(128); int opcode = methodInsnNode.getOpcode(); if(opcode == Opcodes.INVOKESTATIC) { sb.append("static "); } Type methodType = Type.getMethodType(methodInsnNode.desc); Type ownerType = Type.getObjectType(methodInsnNode.owner); if(methodInsnNode.name.equals("<init>")) { sb.append(ownerType.getClassName()); }else { sb.append(methodType.getReturnType().getClassName()).append(' '); sb.append(methodInsnNode.name); } sb.append('('); Type[] argumentTypes = methodType.getArgumentTypes(); for(int i = 0 ; i < argumentTypes.length; ++i) { sb.append(argumentTypes[i].getClassName()); if(i != argumentTypes.length - 1) { sb.append(", "); } } sb.append(')'); return sb.toString(); } }
AsmUtils { public static String methodDeclaration(MethodInsnNode methodInsnNode) { StringBuilder sb = new StringBuilder(128); int opcode = methodInsnNode.getOpcode(); if(opcode == Opcodes.INVOKESTATIC) { sb.append("static "); } Type methodType = Type.getMethodType(methodInsnNode.desc); Type ownerType = Type.getObjectType(methodInsnNode.owner); if(methodInsnNode.name.equals("<init>")) { sb.append(ownerType.getClassName()); }else { sb.append(methodType.getReturnType().getClassName()).append(' '); sb.append(methodInsnNode.name); } sb.append('('); Type[] argumentTypes = methodType.getArgumentTypes(); for(int i = 0 ; i < argumentTypes.length; ++i) { sb.append(argumentTypes[i].getClassName()); if(i != argumentTypes.length - 1) { sb.append(", "); } } sb.append(')'); return sb.toString(); }  }
AsmUtils { public static String methodDeclaration(MethodInsnNode methodInsnNode) { StringBuilder sb = new StringBuilder(128); int opcode = methodInsnNode.getOpcode(); if(opcode == Opcodes.INVOKESTATIC) { sb.append("static "); } Type methodType = Type.getMethodType(methodInsnNode.desc); Type ownerType = Type.getObjectType(methodInsnNode.owner); if(methodInsnNode.name.equals("<init>")) { sb.append(ownerType.getClassName()); }else { sb.append(methodType.getReturnType().getClassName()).append(' '); sb.append(methodInsnNode.name); } sb.append('('); Type[] argumentTypes = methodType.getArgumentTypes(); for(int i = 0 ; i < argumentTypes.length; ++i) { sb.append(argumentTypes[i].getClassName()); if(i != argumentTypes.length - 1) { sb.append(", "); } } sb.append(')'); return sb.toString(); }  static ClassNode loadClass(Class<?> clazz); static ClassNode toClassNode(byte[] classBytes); static ClassReader toClassNode(byte[] classBytes, ClassNode classNode); static byte[] toBytes(ClassNode classNode, ClassLoader classLoader, ClassReader classReader); static byte[] toBytes(ClassNode classNode); static byte[] renameClass(byte[] classBytes, final String newClassName); static void replaceMethod(ClassNode classNode, MethodNode methodNode); static String toASMCode(byte[] bytecode); static String toASMCode(byte[] bytecode, boolean debug); static String toASMCode(ClassNode classNode); static String toASMCode(MethodNode methodNode); static MethodNode newMethodNode(MethodNode source); static MethodNode removeJSRInstructions(MethodNode subjectMethod); static ClassNode removeJSRInstructions(ClassNode classNode); static MethodNode removeLineNumbers(MethodNode methodNode); static MethodNode findFirstMethod(Collection<MethodNode> methodNodes, String name); static List<MethodNode> findMethods(Collection<MethodNode> methodNodes, String name); static MethodNode findMethod(Collection<MethodNode> methodNodes, MethodNode target); static MethodNode findMethod(Collection<MethodNode> methodNodes, String name, String desc); static AbstractInsnNode findInitConstructorInstruction(MethodNode methodNode); static List<MethodInsnNode> findMethodInsnNodeWithPrefix(MethodNode methodNode, String prefix); static List<MethodInsnNode> findMethodInsnNode(MethodNode methodNode, String owner, String name); static List<MethodInsnNode> findMethodInsnNode(MethodNode methodNode, String owner, String name,
                    String desc); static boolean containsMethodInsnNode(MethodNode methodNode, String owner, String name); static boolean isStatic(MethodNode methodNode); static boolean isStatic(MethodInsnNode methodInsnNode); static boolean isConstructor(MethodNode methodNode); String[] getParameterNames(MethodNode methodNode); static MethodNode copy(MethodNode source); static ClassNode copy(ClassNode source); static String methodDeclaration(MethodInsnNode methodInsnNode); static String methodDeclaration(Type owner, MethodNode methodNode); static FieldNode findField(List<FieldNode> fields, String name); static void addField(ClassNode classNode, FieldNode fieldNode); static void addMethod(ClassNode classNode, MethodNode methodNode); static String uniqueNameForMethod(String className, String methodName, String desc); static int getMajorVersion(int version); static int setMajorVersion(int version, int majorVersion); }
AsmUtils { public static String methodDeclaration(MethodInsnNode methodInsnNode) { StringBuilder sb = new StringBuilder(128); int opcode = methodInsnNode.getOpcode(); if(opcode == Opcodes.INVOKESTATIC) { sb.append("static "); } Type methodType = Type.getMethodType(methodInsnNode.desc); Type ownerType = Type.getObjectType(methodInsnNode.owner); if(methodInsnNode.name.equals("<init>")) { sb.append(ownerType.getClassName()); }else { sb.append(methodType.getReturnType().getClassName()).append(' '); sb.append(methodInsnNode.name); } sb.append('('); Type[] argumentTypes = methodType.getArgumentTypes(); for(int i = 0 ; i < argumentTypes.length; ++i) { sb.append(argumentTypes[i].getClassName()); if(i != argumentTypes.length - 1) { sb.append(", "); } } sb.append(')'); return sb.toString(); }  static ClassNode loadClass(Class<?> clazz); static ClassNode toClassNode(byte[] classBytes); static ClassReader toClassNode(byte[] classBytes, ClassNode classNode); static byte[] toBytes(ClassNode classNode, ClassLoader classLoader, ClassReader classReader); static byte[] toBytes(ClassNode classNode); static byte[] renameClass(byte[] classBytes, final String newClassName); static void replaceMethod(ClassNode classNode, MethodNode methodNode); static String toASMCode(byte[] bytecode); static String toASMCode(byte[] bytecode, boolean debug); static String toASMCode(ClassNode classNode); static String toASMCode(MethodNode methodNode); static MethodNode newMethodNode(MethodNode source); static MethodNode removeJSRInstructions(MethodNode subjectMethod); static ClassNode removeJSRInstructions(ClassNode classNode); static MethodNode removeLineNumbers(MethodNode methodNode); static MethodNode findFirstMethod(Collection<MethodNode> methodNodes, String name); static List<MethodNode> findMethods(Collection<MethodNode> methodNodes, String name); static MethodNode findMethod(Collection<MethodNode> methodNodes, MethodNode target); static MethodNode findMethod(Collection<MethodNode> methodNodes, String name, String desc); static AbstractInsnNode findInitConstructorInstruction(MethodNode methodNode); static List<MethodInsnNode> findMethodInsnNodeWithPrefix(MethodNode methodNode, String prefix); static List<MethodInsnNode> findMethodInsnNode(MethodNode methodNode, String owner, String name); static List<MethodInsnNode> findMethodInsnNode(MethodNode methodNode, String owner, String name,
                    String desc); static boolean containsMethodInsnNode(MethodNode methodNode, String owner, String name); static boolean isStatic(MethodNode methodNode); static boolean isStatic(MethodInsnNode methodInsnNode); static boolean isConstructor(MethodNode methodNode); String[] getParameterNames(MethodNode methodNode); static MethodNode copy(MethodNode source); static ClassNode copy(ClassNode source); static String methodDeclaration(MethodInsnNode methodInsnNode); static String methodDeclaration(Type owner, MethodNode methodNode); static FieldNode findField(List<FieldNode> fields, String name); static void addField(ClassNode classNode, FieldNode fieldNode); static void addMethod(ClassNode classNode, MethodNode methodNode); static String uniqueNameForMethod(String className, String methodName, String desc); static int getMajorVersion(int version); static int setMajorVersion(int version, int majorVersion);  }