KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > enhancer > ClassRenamer


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * 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  */

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 JavaDoc;
29
30 /**
31  * Renames a class.
32  *
33  * Adapted from modifyClassName in EnhancerTool.
34  */

35 public class ClassRenamer extends LoggedClassAdapter {
36
37     /**
38      * The old class name, and the names of its super classes.
39      */

40     private Set JavaDoc oldNames;
41
42     /**
43      * The new class name
44      */

45     private final String JavaDoc newName;
46
47     /**
48      * Constructs a new {@link ClassRenamer}.
49      *
50      * @param cv the visitor to be used to generate the renamed class
51      * @param newName the new class name
52      */

53     public ClassRenamer(final ClassVisitor cv,
54                         final Set JavaDoc oldNames,
55                         final String JavaDoc newName,
56                         Logger logger) {
57         super(cv, logger);
58         this.oldNames = oldNames;
59         this.newName = newName;
60     }
61
62     // IMPLEMENTATION OF THE ClassVisitor INTERFACE //
63
// ---------------------------------------------//
64

65     public void visit(final int version, final int access,
66                       final String JavaDoc name,
67                       final String JavaDoc superName,
68                       final String JavaDoc[] interfaces,
69                       final String JavaDoc 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 JavaDoc name,
79                                 final String JavaDoc outerName,
80                                 final String JavaDoc innerName,
81                                 final int access) {
82         cv.visitInnerClass(name, outerName, innerName, access);
83         // TODO manage inner classes
84
}
85
86     public CodeVisitor visitMethod(final int access,
87                                    final String JavaDoc name,
88                                    final String JavaDoc desc,
89                                    final String JavaDoc[] 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     /**
99      * Modifies instructions that reference the old class.
100      */

101     class CodeRenamer extends CodeAdapter {
102
103         /**
104          * Creates a new {@link CodeRenamer}.
105          *
106          * @param cv the visitor to be used to generate the modified code
107          */

108         public CodeRenamer(final CodeVisitor cv) {
109             super(cv);
110         }
111
112         // IMPLEMENTATION OF THE CodeVisitor INTERFACE //
113
// --------------------------------------------//
114

115         public void visitTypeInsn(int i, String JavaDoc 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 JavaDoc owner,
124                                    final String JavaDoc name,
125                                    final String JavaDoc 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 JavaDoc owner,
135                                     final String JavaDoc name,
136                                     final String JavaDoc 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