/Users/lyon/j4p/src/javassist/ClassMap.java
|
1 /*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16 package javassist;
17
18 import javassist.bytecode.Descriptor;
19
20 /**
21 * A hashtable associating class names with different names.
22 *
23 * <p>This hashtable is used for replacing class names in a class
24 * definition or a method body. Define a subclass of this class
25 * if a more complex mapping algorithm is needed. For example,
26 *
27 * <ul><pre>class MyClassMap extends ClassMap {
28 * public Object get(Object jvmClassName) {
29 * String name = toJavaName((String)jvmClassName);
30 * if (name.startsWith("java."))
31 * return toJvmName("java2." + name.substring(5));
32 * else
33 * return super.get(jvmClassName);
34 * }
35 * }</pre></ul>
36 *
37 * <p>This subclass maps <code>java.lang.String</code> to
38 * <code>java2.lang.String</code>. Note that <code>get()</code>
39 * receives and returns the internal representation of a class name.
40 * For example, the internal representation of <code>java.lang.String</code>
41 * is <code>java/lang/String</code>.
42 *
43 * @see #get(Object)
44 * @see CtClass#replaceClassName(ClassMap)
45 * @see CtNewMethod#copy(CtMethod,String,CtClass,ClassMap)
46 */
47 public class ClassMap extends java.util.HashMap {
48 /**
49 * Maps a class name to another name in this hashtable.
50 * The names are obtained with calling <code>Class.getName()</code>.
51 * This method translates the given class names into the
52 * internal form used in the JVM before putting it in
53 * the hashtable.
54 *
55 * @param oldname the original class name
56 * @param newname the substituted class name.
57 */
58 public void put(CtClass oldname, CtClass newname) {
59 put(oldname.getName(), newname.getName());
60 }
61
62 /**
63 * Maps a class name to another name in this hashtable.
64 * This method translates the given class names into the
65 * internal form used in the JVM before putting it in
66 * the hashtable.
67 *
68 * @param oldname the original class name
69 * @param newname the substituted class name.
70 */
71 public void put(String oldname, String newname) {
72 if (oldname == newname)
73 return;
74
75 String oldname2 = toJvmName(oldname);
76 String s = (String) get(oldname2);
77 if (s == null || !s.equals(oldname2))
78 super.put(oldname2, toJvmName(newname));
79 }
80
81 protected final void put0(Object oldname, Object newname) {
82 super.put(oldname, newname);
83 }
84
85 /**
86 * Returns the class name to wihch the given <code>jvmClassName</code>
87 * is mapped. A subclass of this class should override this method.
88 *
89 * <p>This method receives and returns the internal representation of
90 * class name used in the JVM.
91 *
92 * @see #toJvmName(String)
93 * @see #toJavaName(String)
94 */
95 public Object get(Object jvmClassName) {
96 return super.get(jvmClassName);
97 }
98
99 /**
100 * Prevents a mapping from the specified class name to another name.
101 */
102 public void fix(CtClass clazz) {
103 fix(clazz.getName());
104 }
105
106 /**
107 * Prevents a mapping from the specified class name to another name.
108 */
109 public void fix(String name) {
110 String name2 = toJvmName(name);
111 super.put(name2, name2);
112 }
113
114 /**
115 * Converts a class name into the internal representation used in
116 * the JVM.
117 */
118 public static String toJvmName(String classname) {
119 return Descriptor.toJvmName(classname);
120 }
121
122 /**
123 * Converts a class name from the internal representation used in
124 * the JVM to the normal one used in Java.
125 */
126 public static String toJavaName(String classname) {
127 return Descriptor.toJavaName(classname);
128 }
129 }
130