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.

InnerClassesAttribute.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.util.Map;
  18. import java.io.IOException;
  19. /**
  20. * <code>InnerClasses_attribute</code>.
  21. */
  22. public class InnerClassesAttribute extends AttributeInfo {
  23. /**
  24. * The name of this attribute <code>"InnerClasses"</code>.
  25. */
  26. public static final String tag = "InnerClasses";
  27. InnerClassesAttribute(ConstPool cp, int n, DataInputStream in)
  28. throws IOException
  29. {
  30. super(cp, n, in);
  31. }
  32. private InnerClassesAttribute(ConstPool cp, byte[] info) {
  33. super(cp, tag, info);
  34. }
  35. /**
  36. * Returns <code>number_of_classes</code>.
  37. */
  38. public int length() { return ByteArray.readU16bit(get(), 0); }
  39. /**
  40. * Returns <code>classes[nth].inner_class_info_index</code>.
  41. */
  42. public int innerClass(int nth) {
  43. return ByteArray.readU16bit(get(), nth * 8 + 2);
  44. }
  45. /**
  46. * Returns <code>classes[nth].outer_class_info_index</code>.
  47. */
  48. public int outerClass(int nth) {
  49. return ByteArray.readU16bit(get(), nth * 8 + 4);
  50. }
  51. /**
  52. * Returns <code>classes[nth].inner_name_index</code>.
  53. */
  54. public int innerName(int nth) {
  55. return ByteArray.readU16bit(get(), nth * 8 + 6);
  56. }
  57. /**
  58. * Returns <code>classes[nth].inner_class_access_flags</code>.
  59. */
  60. public int accessFlags(int nth) {
  61. return ByteArray.readU16bit(get(), nth * 8 + 8);
  62. }
  63. /**
  64. * Makes a copy. Class names are replaced according to the
  65. * given <code>Map</code> object.
  66. *
  67. * @param newCp the constant pool table used by the new copy.
  68. * @param classnames pairs of replaced and substituted
  69. * class names.
  70. */
  71. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  72. byte[] src = get();
  73. byte[] dest = new byte[src.length];
  74. ConstPool cp = getConstPool();
  75. InnerClassesAttribute attr = new InnerClassesAttribute(newCp, dest);
  76. int n = ByteArray.readU16bit(src, 0);
  77. ByteArray.write16bit(n, dest, 0);
  78. int j = 2;
  79. for (int i = 0; i < n; ++i) {
  80. int innerClass = ByteArray.readU16bit(src, j);
  81. int outerClass = ByteArray.readU16bit(src, j + 2);
  82. int innerName = ByteArray.readU16bit(src, j + 4);
  83. int innerAccess = ByteArray.readU16bit(src, j + 6);
  84. if (innerClass != 0)
  85. innerClass = cp.copy(innerClass, newCp, classnames);
  86. ByteArray.write16bit(innerClass, dest, j);
  87. if (outerClass != 0)
  88. outerClass = cp.copy(outerClass, newCp, classnames);
  89. ByteArray.write16bit(outerClass, dest, j + 2);
  90. if (innerName != 0)
  91. innerName = cp.copy(innerName, newCp, classnames);
  92. ByteArray.write16bit(innerName, dest, j + 4);
  93. ByteArray.write16bit(innerAccess, dest, j + 6);
  94. j += 8;
  95. }
  96. return attr;
  97. }
  98. }