01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */package org.objectweb.speedo.generation.enhancer.common;
18:
19: import org.objectweb.asm.CodeAdapter;
20: import org.objectweb.asm.CodeVisitor;
21:
22: /**
23: * Modifies instructions that reference the old class in order to use the
24: * new class name.
25: *
26: * @author S.Chassande-Barrioz
27: */
28: public class CodeRenamer extends CodeAdapter {
29:
30: private final String oldClass;
31: private final String classToWrite;
32:
33: /**
34: * Creates a new {@link CodeRenamer}.
35: * @param old is the old name of the class
36: * @param neo is the new name of the class
37: * @param cv
38: * the visitor to be used to generate the modified code
39: */
40: public CodeRenamer(final CodeVisitor cv, String old, String neo) {
41: super (cv);
42: this .oldClass = old;
43: this .classToWrite = neo;
44: }
45:
46: // IMPLEMENTATION OF THE CodeVisitor INTERFACE //
47: // --------------------------------------------//
48:
49: public void visitTypeInsn(final int i, final String s) {
50: final String type;
51: if (oldClass.equals(s)) {
52: type = classToWrite;
53: } else {
54: type = s;
55: }
56: cv.visitTypeInsn(i, type);
57: }
58:
59: public void visitFieldInsn(final int opcode, final String owner,
60: final String name, final String desc) {
61: if (oldClass.equals(owner)) {
62: cv.visitFieldInsn(opcode, classToWrite, name, desc);
63: } else {
64: cv.visitFieldInsn(opcode, owner, name, desc);
65: }
66: }
67:
68: public void visitMethodInsn(final int opcode, final String owner,
69: final String name, final String desc) {
70: if (oldClass.equals(owner)) {
71: cv.visitMethodInsn(opcode, classToWrite, name, desc);
72: } else {
73: cv.visitMethodInsn(opcode, owner, name, desc);
74: }
75: }
76: }
|