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.

EnclosingMethodAttribute.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import javassist.CtConstructor;
  21. /**
  22. * <code>EnclosingMethod_attribute</code>.
  23. */
  24. public class EnclosingMethodAttribute extends AttributeInfo {
  25. /**
  26. * The name of this attribute <code>"EnclosingMethod"</code>.
  27. */
  28. public static final String tag = "EnclosingMethod";
  29. EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream in)
  30. throws IOException
  31. {
  32. super(cp, n, in);
  33. }
  34. /**
  35. * Constructs an EnclosingMethod attribute.
  36. *
  37. * @param cp a constant pool table.
  38. * @param className the name of the innermost enclosing class.
  39. * @param methodName the name of the enclosing method.
  40. * @param methodDesc the descriptor of the enclosing method.
  41. */
  42. public EnclosingMethodAttribute(ConstPool cp, String className,
  43. String methodName, String methodDesc) {
  44. super(cp, tag);
  45. int ci = cp.addClassInfo(className);
  46. int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
  47. byte[] bvalue = new byte[4];
  48. bvalue[0] = (byte)(ci >>> 8);
  49. bvalue[1] = (byte)ci;
  50. bvalue[2] = (byte)(ni >>> 8);
  51. bvalue[3] = (byte)ni;
  52. set(bvalue);
  53. }
  54. /**
  55. * Constructs an EnclosingMethod attribute.
  56. * The value of <code>method_index</code> is set to 0.
  57. *
  58. * @param cp a constant pool table.
  59. * @param className the name of the innermost enclosing class.
  60. */
  61. public EnclosingMethodAttribute(ConstPool cp, String className) {
  62. super(cp, tag);
  63. int ci = cp.addClassInfo(className);
  64. int ni = 0;
  65. byte[] bvalue = new byte[4];
  66. bvalue[0] = (byte)(ci >>> 8);
  67. bvalue[1] = (byte)ci;
  68. bvalue[2] = (byte)(ni >>> 8);
  69. bvalue[3] = (byte)ni;
  70. set(bvalue);
  71. }
  72. /**
  73. * Returns the value of <code>class_index</code>.
  74. */
  75. public int classIndex() {
  76. return ByteArray.readU16bit(get(), 0);
  77. }
  78. /**
  79. * Returns the value of <code>method_index</code>.
  80. */
  81. public int methodIndex() {
  82. return ByteArray.readU16bit(get(), 2);
  83. }
  84. /**
  85. * Returns the name of the class specified by <code>class_index</code>.
  86. */
  87. public String className() {
  88. return getConstPool().getClassInfo(classIndex());
  89. }
  90. /**
  91. * Returns the method name specified by <code>method_index</code>.
  92. * If the method is a class initializer (static constructor),
  93. * {@link MethodInfo#nameClinit} is returned.
  94. */
  95. public String methodName() {
  96. ConstPool cp = getConstPool();
  97. int mi = methodIndex();
  98. if (mi == 0)
  99. return MethodInfo.nameClinit;
  100. else {
  101. int ni = cp.getNameAndTypeName(mi);
  102. return cp.getUtf8Info(ni);
  103. }
  104. }
  105. /**
  106. * Returns the method descriptor specified by <code>method_index</code>.
  107. */
  108. public String methodDescriptor() {
  109. ConstPool cp = getConstPool();
  110. int mi = methodIndex();
  111. int ti = cp.getNameAndTypeDescriptor(mi);
  112. return cp.getUtf8Info(ti);
  113. }
  114. /**
  115. * Makes a copy. Class names are replaced according to the
  116. * given <code>Map</code> object.
  117. *
  118. * @param newCp the constant pool table used by the new copy.
  119. * @param classnames pairs of replaced and substituted
  120. * class names.
  121. */
  122. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  123. if (methodIndex() == 0)
  124. return new EnclosingMethodAttribute(newCp, className());
  125. else
  126. return new EnclosingMethodAttribute(newCp, className(),
  127. methodName(), methodDescriptor());
  128. }
  129. }