You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassMap.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist;
  17. import javassist.bytecode.Descriptor;
  18. /**
  19. * A hash table associating class names with different names.
  20. *
  21. * <p>This hashtable is used for replacing class names in a class
  22. * definition or a method body. Define a subclass of this class
  23. * if a more complex mapping algorithm is needed. For example,
  24. *
  25. * <pre>class MyClassMap extends ClassMap {
  26. * public Object get(Object jvmClassName) {
  27. * String name = toJavaName((String)jvmClassName);
  28. * if (name.startsWith("java."))
  29. * return toJvmName("java2." + name.substring(5));
  30. * else
  31. * return super.get(jvmClassName);
  32. * }
  33. * }</pre>
  34. *
  35. * <p>This subclass maps <code>java.lang.String</code> to
  36. * <code>java2.lang.String</code>. Note that <code>get()</code>
  37. * receives and returns the internal representation of a class name.
  38. * For example, the internal representation of <code>java.lang.String</code>
  39. * is <code>java/lang/String</code>.
  40. *
  41. * <p>Note that this is a map from <code>String</code> to <code>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. private ClassMap parent;
  49. /**
  50. * Constructs a hash table.
  51. */
  52. public ClassMap() { parent = null; }
  53. ClassMap(ClassMap map) { parent = map; }
  54. /**
  55. * Maps a class name to another name in this hashtable.
  56. * The names are obtained with calling <code>Class.getName()</code>.
  57. * This method translates the given class names into the
  58. * internal form used in the JVM before putting it in
  59. * the hashtable.
  60. *
  61. * @param oldname the original class name
  62. * @param newname the substituted class name.
  63. */
  64. public void put(CtClass oldname, CtClass newname) {
  65. put(oldname.getName(), newname.getName());
  66. }
  67. /**
  68. * Maps a class name to another name in this hashtable.
  69. * If the hashtable contains another mapping from the same
  70. * class name, the old mapping is replaced.
  71. * This method translates the given class names into the
  72. * internal form used in the JVM before putting it in
  73. * the hashtable.
  74. *
  75. * <p>If <code>oldname</code> is identical to
  76. * <code>newname</code>, then this method does not
  77. * perform anything; it does not record the mapping from
  78. * <code>oldname</code> to <code>newname</code>. See
  79. * <code>fix</code> method.
  80. *
  81. * @param oldname the original class name.
  82. * @param newname the substituted class name.
  83. * @see #fix(String)
  84. */
  85. public void put(String oldname, String newname) {
  86. if (oldname == newname)
  87. return;
  88. String oldname2 = toJvmName(oldname);
  89. String s = (String)get(oldname2);
  90. if (s == null || !s.equals(oldname2))
  91. super.put(oldname2, toJvmName(newname));
  92. }
  93. /**
  94. * Is equivalent to <code>put()</code> except that
  95. * the given mapping is not recorded into the hashtable
  96. * if another mapping from <code>oldname</code> is
  97. * already included.
  98. *
  99. * @param oldname the original class name.
  100. * @param newname the substituted class name.
  101. */
  102. public void putIfNone(String oldname, String newname) {
  103. if (oldname == newname)
  104. return;
  105. String oldname2 = toJvmName(oldname);
  106. String s = (String)get(oldname2);
  107. if (s == null)
  108. super.put(oldname2, toJvmName(newname));
  109. }
  110. protected final void put0(Object oldname, Object newname) {
  111. super.put(oldname, newname);
  112. }
  113. /**
  114. * Returns the class name to wihch the given <code>jvmClassName</code>
  115. * is mapped. A subclass of this class should override this method.
  116. *
  117. * <p>This method receives and returns the internal representation of
  118. * class name used in the JVM.
  119. *
  120. * @see #toJvmName(String)
  121. * @see #toJavaName(String)
  122. */
  123. public Object get(Object jvmClassName) {
  124. Object found = super.get(jvmClassName);
  125. if (found == null && parent != null)
  126. return parent.get(jvmClassName);
  127. else
  128. return found;
  129. }
  130. /**
  131. * Prevents a mapping from the specified class name to another name.
  132. */
  133. public void fix(CtClass clazz) {
  134. fix(clazz.getName());
  135. }
  136. /**
  137. * Prevents a mapping from the specified class name to another name.
  138. */
  139. public void fix(String name) {
  140. String name2 = toJvmName(name);
  141. super.put(name2, name2);
  142. }
  143. /**
  144. * Converts a class name into the internal representation used in
  145. * the JVM.
  146. */
  147. public static String toJvmName(String classname) {
  148. return Descriptor.toJvmName(classname);
  149. }
  150. /**
  151. * Converts a class name from the internal representation used in
  152. * the JVM to the normal one used in Java.
  153. */
  154. public static String toJavaName(String classname) {
  155. return Descriptor.toJavaName(classname);
  156. }
  157. }