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.

CtMember.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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;
  16. /**
  17. * An instance of <code>CtMember</code> represents a field, a constructor,
  18. * or a method.
  19. */
  20. public abstract class CtMember {
  21. protected CtMember next; // for internal use
  22. protected CtClass declaringClass;
  23. protected CtMember(CtClass clazz) { declaringClass = clazz; }
  24. static CtMember append(CtMember list, CtMember tail) {
  25. tail.next = null;
  26. if (list == null)
  27. return tail;
  28. else {
  29. CtMember lst = list;
  30. while (lst.next != null)
  31. lst = lst.next;
  32. lst.next = tail;
  33. return list;
  34. }
  35. }
  36. static int count(CtMember f) {
  37. int n = 0;
  38. while (f != null) {
  39. ++n;
  40. f = f.next;
  41. }
  42. return n;
  43. }
  44. static CtMember remove(CtMember list, CtMember m) {
  45. CtMember top = list;
  46. if (list == null)
  47. return null;
  48. else if (list == m)
  49. return list.next;
  50. else
  51. while (list.next != null) {
  52. if (list.next == m) {
  53. list.next = list.next.next;
  54. break;
  55. }
  56. list = list.next;
  57. }
  58. return top;
  59. }
  60. public String toString() {
  61. StringBuffer buffer = new StringBuffer(getClass().getName());
  62. buffer.append("@");
  63. buffer.append(Integer.toHexString(hashCode()));
  64. buffer.append("[");
  65. buffer.append(Modifier.toString(getModifiers()));
  66. extendToString(buffer);
  67. buffer.append("]");
  68. return buffer.toString();
  69. }
  70. /**
  71. * Invoked by {@link #toString()} to add to the buffer and provide the
  72. * complete value. Subclasses should invoke this method, adding a
  73. * space before each token. The modifiers for the member are
  74. * provided first; subclasses should provide additional data such
  75. * as return type, field or method name, etc.
  76. */
  77. protected abstract void extendToString(StringBuffer buffer);
  78. /**
  79. * Returns the class that declares this member.
  80. */
  81. public CtClass getDeclaringClass() { return declaringClass; }
  82. /**
  83. * Obtains the modifiers of the member.
  84. *
  85. * @return modifiers encoded with
  86. * <code>javassist.Modifier</code>.
  87. * @see Modifier
  88. */
  89. public abstract int getModifiers();
  90. /**
  91. * Sets the encoded modifiers of the member.
  92. *
  93. * @see Modifier
  94. */
  95. public abstract void setModifiers(int mod);
  96. /**
  97. * Obtains the name of the member.
  98. *
  99. * <p>As for constructor names, see <code>getName()</code>
  100. * in <code>CtConstructor</code>.
  101. *
  102. * @see CtConstructor#getName()
  103. */
  104. public abstract String getName();
  105. /**
  106. * Obtains an attribute with the given name.
  107. * If that attribute is not found in the class file, this
  108. * method returns null.
  109. *
  110. * @param name attribute name
  111. */
  112. public abstract byte[] getAttribute(String name);
  113. /**
  114. * Adds an attribute. The attribute is saved in the class file.
  115. *
  116. * @param name attribute name
  117. * @param data attribute value
  118. */
  119. public abstract void setAttribute(String name, byte[] data);
  120. }