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.

MethodParametersAttribute.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package javassist.bytecode;
  2. import java.io.DataInputStream;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. /**
  6. * <code>MethodParameters_attribute</code>.
  7. */
  8. public class MethodParametersAttribute extends AttributeInfo {
  9. /**
  10. * The name of this attribute <code>"MethodParameters"</code>.
  11. */
  12. public static final String tag = "MethodParameters";
  13. MethodParametersAttribute(ConstPool cp, int n, DataInputStream in)
  14. throws IOException
  15. {
  16. super(cp, n, in);
  17. }
  18. /**
  19. * Constructs an attribute.
  20. *
  21. * @param cp a constant pool table.
  22. * @param names an array of parameter names.
  23. * The i-th element is the name of the i-th parameter.
  24. * @param flags an array of parameter access flags.
  25. */
  26. public MethodParametersAttribute(ConstPool cp, String[] names, int[] flags) {
  27. super(cp, tag);
  28. byte[] data = new byte[names.length * 4 + 1];
  29. data[0] = (byte)names.length;
  30. for (int i = 0; i < names.length; i++) {
  31. String name = names[i];
  32. ByteArray.write16bit(name == null ? 0 : cp.addUtf8Info(name), data, i * 4 + 1);
  33. ByteArray.write16bit(flags[i], data, i * 4 + 3);
  34. }
  35. set(data);
  36. }
  37. /**
  38. * Returns <code>parameters_count</code>, which is the number of
  39. * parameters.
  40. */
  41. public int size() {
  42. return info[0] & 0xff;
  43. }
  44. /**
  45. * Returns the value of <code>name_index</code> of the i-th element of <code>parameters</code>.
  46. *
  47. * @param i the position of the parameter.
  48. */
  49. public int name(int i) {
  50. return ByteArray.readU16bit(info, i * 4 + 1);
  51. }
  52. /**
  53. * Returns the name of the i-th element of <code>parameters</code>.
  54. * @param i the position of the parameter.
  55. */
  56. public String parameterName(int i) {
  57. int index = name(i);
  58. return index == 0 ? null : getConstPool().getUtf8Info(index);
  59. }
  60. /**
  61. * Returns the value of <code>access_flags</code> of the i-th element of <code>parameters</code>.
  62. *
  63. * @param i the position of the parameter.
  64. * @see AccessFlag
  65. */
  66. public int accessFlags(int i) {
  67. return ByteArray.readU16bit(info, i * 4 + 3);
  68. }
  69. /**
  70. * Makes a copy.
  71. *
  72. * @param newCp the constant pool table used by the new copy.
  73. * @param classnames ignored.
  74. */
  75. @Override
  76. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  77. int s = size();
  78. ConstPool cp = getConstPool();
  79. String[] names = new String[s];
  80. int[] flags = new int[s];
  81. for (int i = 0; i < s; i++) {
  82. int index = name(i);
  83. names[i] = index == 0 ? null : cp.getUtf8Info(index);
  84. flags[i] = accessFlags(i);
  85. }
  86. return new MethodParametersAttribute(newCp, names, flags);
  87. }
  88. }