1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.asm.Attribute; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.CodeAdapter; 23 import org.objectweb.asm.CodeVisitor; 24 import org.objectweb.asm.Constants; 25 import org.objectweb.util.monolog.api.BasicLevel; 26 import org.objectweb.util.monolog.api.Logger; 27 28 import java.util.Set ; 29 30 35 public class ClassRenamer extends LoggedClassAdapter { 36 37 40 private Set oldNames; 41 42 45 private final String newName; 46 47 53 public ClassRenamer(final ClassVisitor cv, 54 final Set oldNames, 55 final String newName, 56 Logger logger) { 57 super(cv, logger); 58 this.oldNames = oldNames; 59 this.newName = newName; 60 } 61 62 65 public void visit(final int version, final int access, 66 final String name, 67 final String superName, 68 final String [] interfaces, 69 final String sourceFile) { 70 oldNames.add(name); 71 if (debug) { 72 logger.log(BasicLevel.DEBUG, 73 "Rename the class '" + name + "' to '" + newName + "'"); 74 } 75 cv.visit(version, access, newName, superName, interfaces, sourceFile); 76 } 77 78 public void visitInnerClass(final String name, 79 final String outerName, 80 final String innerName, 81 final int access) { 82 cv.visitInnerClass(name, outerName, innerName, access); 83 } 85 86 public CodeVisitor visitMethod(final int access, 87 final String name, 88 final String desc, 89 final String [] exceptions, 90 final Attribute attrs) { 91 CodeVisitor _cv = this.cv.visitMethod(access, name, desc, exceptions, attrs); 92 if (_cv != null && (access & Constants.ACC_ABSTRACT) == 0) { 93 _cv = new CodeRenamer(_cv); 94 } 95 return _cv; 96 } 97 98 101 class CodeRenamer extends CodeAdapter { 102 103 108 public CodeRenamer(final CodeVisitor cv) { 109 super(cv); 110 } 111 112 115 public void visitTypeInsn(int i, String s) { 116 if (oldNames.contains(s)) { 117 s = newName; 118 } 119 cv.visitTypeInsn(i, s); 120 } 121 122 public void visitFieldInsn(final int opcode, 123 final String owner, 124 final String name, 125 final String desc) { 126 if (oldNames.contains(owner)) { 127 cv.visitFieldInsn(opcode, newName, name, desc); 128 } else { 129 cv.visitFieldInsn(opcode, owner, name, desc); 130 } 131 } 132 133 public void visitMethodInsn(final int opcode, 134 final String owner, 135 final String name, 136 final String desc) { 137 if (oldNames.contains(owner)) { 138 cv.visitMethodInsn(opcode, newName, name, desc); 139 } else { 140 cv.visitMethodInsn(opcode, owner, name, desc); 141 } 142 } 143 } 144 } | Popular Tags |