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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. package javassist;
  16. import javassist.bytecode.Descriptor;
  17. /**
  18. * A hashtable associating class names with different names.
  19. *
  20. * <p>This hashtable is used for replacing class names in a class
  21. * definition or a method body. Define a subclass of this class
  22. * if a more complex mapping algorithm is needed. For example,
  23. *
  24. * <ul><pre>class MyClassMap extends ClassMap {
  25. * public Object get(Object jvmClassName) {
  26. * String name = toJavaName((String)jvmClassName);
  27. * if (name.startsWith("java."))
  28. * return toJvmName("java2." + name.substring(5));
  29. * else
  30. * return super.get(jvmClassName);
  31. * }
  32. * }</pre></ul>
  33. *
  34. * <p>This subclass maps <code>java.lang.String</code> to
  35. * <code>java2.lang.String</code>. Note that <code>get()</code>
  36. * receives and returns the internal representation of a class name.
  37. * For example, the internal representation of <code>java.lang.String</code>
  38. * is <code>java/lang/String</code>.
  39. *
  40. * @see #get(Object)
  41. * @see CtClass#replaceClassName(ClassMap)
  42. * @see CtNewMethod#copy(CtMethod,String,CtClass,ClassMap)
  43. */
  44. public class ClassMap extends java.util.HashMap {
  45. /**
  46. * Maps a class name to another name in this hashtable.
  47. * The names are obtained with calling <code>Class.getName()</code>.
  48. * This method translates the given class names into the
  49. * internal form used in the JVM before putting it in
  50. * the hashtable.
  51. *
  52. * @param oldname the original class name
  53. * @param newname the substituted class name.
  54. */
  55. public void put(CtClass oldname, CtClass newname) {
  56. put(oldname.getName(), newname.getName());
  57. }
  58. /**
  59. * Maps a class name to another name in this hashtable.
  60. * This method translates the given class names into the
  61. * internal form used in the JVM before putting it in
  62. * the hashtable.
  63. *
  64. * @param oldname the original class name
  65. * @param newname the substituted class name.
  66. */
  67. public void put(String oldname, String newname) {
  68. if (oldname == newname)
  69. return;
  70. String oldname2 = toJvmName(oldname);
  71. String s = (String)get(oldname2);
  72. if (s == null || !s.equals(oldname2))
  73. super.put(oldname2, toJvmName(newname));
  74. }
  75. protected final void put0(Object oldname, Object newname) {
  76. super.put(oldname, newname);
  77. }
  78. /**
  79. * Returns the class name to wihch the given <code>jvmClassName</code>
  80. * is mapped. A subclass of this class should override this method.
  81. *
  82. * <p>This method receives and returns the internal representation of
  83. * class name used in the JVM.
  84. *
  85. * @see #toJvmName(String)
  86. * @see #toJavaName(String)
  87. */
  88. public Object get(Object jvmClassName) {
  89. return super.get(jvmClassName);
  90. }
  91. /**
  92. * Prevents a mapping from the specified class name to another name.
  93. */
  94. public void fix(CtClass clazz) {
  95. fix(clazz.getName());
  96. }
  97. /**
  98. * Prevents a mapping from the specified class name to another name.
  99. */
  100. public void fix(String name) {
  101. String name2 = toJvmName(name);
  102. super.put(name2, name2);
  103. }
  104. /**
  105. * Converts a class name into the internal representation used in
  106. * the JVM.
  107. */
  108. public static String toJvmName(String classname) {
  109. return Descriptor.toJvmName(classname);
  110. }
  111. /**
  112. * Converts a class name from the internal representation used in
  113. * the JVM to the normal one used in Java.
  114. */
  115. public static String toJavaName(String classname) {
  116. return Descriptor.toJavaName(classname);
  117. }
  118. }