選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EnclosingMethodAttribute.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.IOException;
  18. import java.util.Map;
  19. /**
  20. * <code>EnclosingMethod_attribute</code>.
  21. */
  22. public class EnclosingMethodAttribute extends AttributeInfo {
  23. /**
  24. * The name of this attribute <code>"EnclosingMethod"</code>.
  25. */
  26. public static final String tag = "EnclosingMethod";
  27. EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream in)
  28. throws IOException
  29. {
  30. super(cp, n, in);
  31. }
  32. /**
  33. * Constructs an EnclosingMethod attribute.
  34. *
  35. * @param cp a constant pool table.
  36. * @param className the name of the innermost enclosing class.
  37. * @param methodName the name of the enclosing method.
  38. * @param methodDesc the descriptor of the enclosing method.
  39. */
  40. public EnclosingMethodAttribute(ConstPool cp, String className,
  41. String methodName, String methodDesc) {
  42. super(cp, tag);
  43. int ci = cp.addClassInfo(className);
  44. int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
  45. byte[] bvalue = new byte[4];
  46. bvalue[0] = (byte)(ci >>> 8);
  47. bvalue[1] = (byte)ci;
  48. bvalue[2] = (byte)(ni >>> 8);
  49. bvalue[3] = (byte)ni;
  50. set(bvalue);
  51. }
  52. /**
  53. * Constructs an EnclosingMethod attribute.
  54. *
  55. * @param cp a constant pool table.
  56. * @param className the name of the innermost enclosing class.
  57. */
  58. public EnclosingMethodAttribute(ConstPool cp, String className) {
  59. super(cp, tag);
  60. int ci = cp.addClassInfo(className);
  61. int ni = 0;
  62. byte[] bvalue = new byte[4];
  63. bvalue[0] = (byte)(ci >>> 8);
  64. bvalue[1] = (byte)ci;
  65. bvalue[2] = (byte)(ni >>> 8);
  66. bvalue[3] = (byte)ni;
  67. set(bvalue);
  68. }
  69. /**
  70. * Returns the value of <code>class_index</code>.
  71. */
  72. public int classIndex() {
  73. return ByteArray.readU16bit(get(), 0);
  74. }
  75. /**
  76. * Returns the value of <code>method_index</code>.
  77. */
  78. public int methodIndex() {
  79. return ByteArray.readU16bit(get(), 2);
  80. }
  81. /**
  82. * Returns the name of the class specified by <code>class_index</code>.
  83. */
  84. public String className() {
  85. return getConstPool().getClassInfo(classIndex());
  86. }
  87. /**
  88. * Returns the method name specified by <code>method_index</code>.
  89. */
  90. public String methodName() {
  91. ConstPool cp = getConstPool();
  92. int mi = methodIndex();
  93. int ni = cp.getNameAndTypeName(mi);
  94. return cp.getUtf8Info(ni);
  95. }
  96. /**
  97. * Returns the method descriptor specified by <code>method_index</code>.
  98. */
  99. public String methodDescriptor() {
  100. ConstPool cp = getConstPool();
  101. int mi = methodIndex();
  102. int ti = cp.getNameAndTypeDescriptor(mi);
  103. return cp.getUtf8Info(ti);
  104. }
  105. /**
  106. * Makes a copy. Class names are replaced according to the
  107. * given <code>Map</code> object.
  108. *
  109. * @param newCp the constant pool table used by the new copy.
  110. * @param classnames pairs of replaced and substituted
  111. * class names.
  112. */
  113. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  114. if (methodIndex() == 0)
  115. return new EnclosingMethodAttribute(newCp, className());
  116. return new EnclosingMethodAttribute(newCp, className(),
  117. methodName(), methodDescriptor());
  118. }
  119. }