@Test public void testRenameClass() throws Exception { ClassNode classNode = AsmUtils.loadClass(AsmUtilsTest.class); byte[] classBytes = AsmUtils.toBytes(classNode); byte[] renameClass = AsmUtils.renameClass(classBytes, "com.test.Test.XXX"); VerifyUtils.asmVerify(renameClass); Object object = VerifyUtils.instanceVerity(renameClass); Assertions.assertThat(object.getClass().getName()).isEqualTo("com.test.Test.XXX"); }
public static byte[] renameClass(byte[] classBytes, final String newClassName) { final String internalName = newClassName.replace('.', '/'); ClassReader reader = new ClassReader(classBytes); ClassWriter writer = new ClassWriter(0); class RenameRemapper extends Remapper { private String className; @Override public String map(String typeName) { if (typeName.equals(className)) { return internalName; } return super.map(typeName); } public void setClassName(String className) { this.className = className; } } final RenameRemapper renameRemapper = new RenameRemapper(); ClassRemapper adapter = new ClassRemapper(writer, renameRemapper) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { renameRemapper.setClassName(name); super.visit(version, access, name, signature, superName, interfaces); } }; reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }
AsmUtils { public static byte[] renameClass(byte[] classBytes, final String newClassName) { final String internalName = newClassName.replace('.', '/'); ClassReader reader = new ClassReader(classBytes); ClassWriter writer = new ClassWriter(0); class RenameRemapper extends Remapper { private String className; @Override public String map(String typeName) { if (typeName.equals(className)) { return internalName; } return super.map(typeName); } public void setClassName(String className) { this.className = className; } } final RenameRemapper renameRemapper = new RenameRemapper(); ClassRemapper adapter = new ClassRemapper(writer, renameRemapper) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { renameRemapper.setClassName(name); super.visit(version, access, name, signature, superName, interfaces); } }; reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); } }
AsmUtils { public static byte[] renameClass(byte[] classBytes, final String newClassName) { final String internalName = newClassName.replace('.', '/'); ClassReader reader = new ClassReader(classBytes); ClassWriter writer = new ClassWriter(0); class RenameRemapper extends Remapper { private String className; @Override public String map(String typeName) { if (typeName.equals(className)) { return internalName; } return super.map(typeName); } public void setClassName(String className) { this.className = className; } } final RenameRemapper renameRemapper = new RenameRemapper(); ClassRemapper adapter = new ClassRemapper(writer, renameRemapper) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { renameRemapper.setClassName(name); super.visit(version, access, name, signature, superName, interfaces); } }; reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }  }
AsmUtils { public static byte[] renameClass(byte[] classBytes, final String newClassName) { final String internalName = newClassName.replace('.', '/'); ClassReader reader = new ClassReader(classBytes); ClassWriter writer = new ClassWriter(0); class RenameRemapper extends Remapper { private String className; @Override public String map(String typeName) { if (typeName.equals(className)) { return internalName; } return super.map(typeName); } public void setClassName(String className) { this.className = className; } } final RenameRemapper renameRemapper = new RenameRemapper(); ClassRemapper adapter = new ClassRemapper(writer, renameRemapper) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { renameRemapper.setClassName(name); super.visit(version, access, name, signature, superName, interfaces); } }; reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }  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 byte[] renameClass(byte[] classBytes, final String newClassName) { final String internalName = newClassName.replace('.', '/'); ClassReader reader = new ClassReader(classBytes); ClassWriter writer = new ClassWriter(0); class RenameRemapper extends Remapper { private String className; @Override public String map(String typeName) { if (typeName.equals(className)) { return internalName; } return super.map(typeName); } public void setClassName(String className) { this.className = className; } } final RenameRemapper renameRemapper = new RenameRemapper(); ClassRemapper adapter = new ClassRemapper(writer, renameRemapper) { @Override public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) { renameRemapper.setClassName(name); super.visit(version, access, name, signature, superName, interfaces); } }; reader.accept(adapter, ClassReader.EXPAND_FRAMES); writer.visitEnd(); return writer.toByteArray(); }  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);  }