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.4KB

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