Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BootstrapMethodsAttribute.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package javassist.bytecode;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. public class BootstrapMethodsAttribute extends AttributeInfo {
  6. /**
  7. * The name of this attribute <code>"BootstrapMethods"</code>.
  8. */
  9. public static final String tag = "BootstrapMethods";
  10. /**
  11. * An element of <code>bootstrap_methods</code>.
  12. */
  13. public static class BootstrapMethod {
  14. /**
  15. * Constructs an element of <code>bootstrap_methods</code>.
  16. *
  17. * @param method <code>bootstrap_method_ref</code>.
  18. * @param args <code>bootstrap_arguments</code>.
  19. */
  20. public BootstrapMethod(int method, int[] args) {
  21. methodRef = method;
  22. arguments = args;
  23. }
  24. /**
  25. * <code>bootstrap_method_ref</code>.
  26. * The value at this index must be a <code>CONSTANT_MethodHandle_info</code>.
  27. */
  28. public int methodRef;
  29. /**
  30. * <code>bootstrap_arguments</code>.
  31. */
  32. public int[] arguments;
  33. }
  34. BootstrapMethodsAttribute(ConstPool cp, int n, DataInputStream in)
  35. throws IOException
  36. {
  37. super(cp, n, in);
  38. }
  39. /**
  40. * Constructs a BootstrapMethods attribute.
  41. *
  42. * @param cp a constant pool table.
  43. * @param methods the contents.
  44. */
  45. public BootstrapMethodsAttribute(ConstPool cp, BootstrapMethod[] methods) {
  46. super(cp, tag);
  47. int size = 2;
  48. for (int i = 0; i < methods.length; i++)
  49. size += 4 + methods[i].arguments.length * 2;
  50. byte[] data = new byte[size];
  51. ByteArray.write16bit(methods.length, data, 0); // num_bootstrap_methods
  52. int pos = 2;
  53. for (int i = 0; i < methods.length; i++) {
  54. ByteArray.write16bit(methods[i].methodRef, data, pos);
  55. ByteArray.write16bit(methods[i].arguments.length, data, pos + 2);
  56. int[] args = methods[i].arguments;
  57. pos += 4;
  58. for (int k = 0; k < args.length; k++) {
  59. ByteArray.write16bit(args[k], data, pos);
  60. pos += 2;
  61. }
  62. }
  63. set(data);
  64. }
  65. /**
  66. * Obtains <code>bootstrap_methods</code> in this attribute.
  67. *
  68. * @return an array of <code>BootstrapMethod</code>. Since it
  69. * is a fresh copy, modifying the returned array does not
  70. * affect the original contents of this attribute.
  71. */
  72. public BootstrapMethod[] getMethods() {
  73. byte[] data = this.get();
  74. int num = ByteArray.readU16bit(data, 0);
  75. BootstrapMethod[] methods = new BootstrapMethod[num];
  76. int pos = 2;
  77. for (int i = 0; i < num; i++) {
  78. int ref = ByteArray.readU16bit(data, pos);
  79. int len = ByteArray.readU16bit(data, pos + 2);
  80. int[] args = new int[len];
  81. pos += 4;
  82. for (int k = 0; k < len; k++) {
  83. args[k] = ByteArray.readU16bit(data, pos);
  84. pos += 2;
  85. }
  86. methods[i] = new BootstrapMethod(ref, args);
  87. }
  88. return methods;
  89. }
  90. /**
  91. * Makes a copy. Class names are replaced according to the
  92. * given <code>Map</code> object.
  93. *
  94. * @param newCp the constant pool table used by the new copy.
  95. * @param classnames pairs of replaced and substituted
  96. * class names.
  97. */
  98. @Override
  99. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  100. BootstrapMethod[] methods = getMethods();
  101. ConstPool thisCp = getConstPool();
  102. for (int i = 0; i < methods.length; i++) {
  103. BootstrapMethod m = methods[i];
  104. m.methodRef = thisCp.copy(m.methodRef, newCp, classnames);
  105. for (int k = 0; k < m.arguments.length; k++)
  106. m.arguments[k] = thisCp.copy(m.arguments[k], newCp, classnames);
  107. }
  108. return new BootstrapMethodsAttribute(newCp, methods);
  109. }
  110. }