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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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;
  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 previousTail, CtMember tail) {
  25. tail.next = null;
  26. if (list == null)
  27. return tail;
  28. else {
  29. previousTail.next = tail;
  30. return list;
  31. }
  32. }
  33. static CtMember append(CtMember list, CtMember tail) {
  34. tail.next = null;
  35. if (list == null)
  36. return tail;
  37. else {
  38. CtMember lst = list;
  39. while (lst.next != null)
  40. lst = lst.next;
  41. lst.next = tail;
  42. return list;
  43. }
  44. }
  45. static int count(CtMember f) {
  46. int n = 0;
  47. while (f != null) {
  48. ++n;
  49. f = f.next;
  50. }
  51. return n;
  52. }
  53. static CtMember remove(CtMember list, CtMember m) {
  54. CtMember top = list;
  55. if (list == null)
  56. return null;
  57. else if (list == m)
  58. return list.next;
  59. else
  60. while (list.next != null) {
  61. if (list.next == m) {
  62. list.next = list.next.next;
  63. break;
  64. }
  65. list = list.next;
  66. }
  67. return top;
  68. }
  69. public String toString() {
  70. StringBuffer buffer = new StringBuffer(getClass().getName());
  71. buffer.append("@");
  72. buffer.append(Integer.toHexString(hashCode()));
  73. buffer.append("[");
  74. buffer.append(Modifier.toString(getModifiers()));
  75. extendToString(buffer);
  76. buffer.append("]");
  77. return buffer.toString();
  78. }
  79. /**
  80. * Invoked by {@link #toString()} to add to the buffer and provide the
  81. * complete value. Subclasses should invoke this method, adding a
  82. * space before each token. The modifiers for the member are
  83. * provided first; subclasses should provide additional data such
  84. * as return type, field or method name, etc.
  85. */
  86. protected abstract void extendToString(StringBuffer buffer);
  87. /**
  88. * Returns the class that declares this member.
  89. */
  90. public CtClass getDeclaringClass() { return declaringClass; }
  91. /**
  92. * Returns true if this member is accessible from the given class.
  93. */
  94. public boolean visibleFrom(CtClass clazz) {
  95. int mod = getModifiers();
  96. if (Modifier.isPublic(mod))
  97. return true;
  98. else if (Modifier.isPrivate(mod))
  99. return clazz == declaringClass;
  100. else { // package or protected
  101. String declName = declaringClass.getPackageName();
  102. String fromName = clazz.getPackageName();
  103. boolean visible;
  104. if (declName == null)
  105. visible = fromName == null;
  106. else
  107. visible = declName.equals(fromName);
  108. if (!visible && Modifier.isProtected(mod))
  109. return clazz.subclassOf(declaringClass);
  110. return visible;
  111. }
  112. }
  113. /**
  114. * Obtains the modifiers of the member.
  115. *
  116. * @return modifiers encoded with
  117. * <code>javassist.Modifier</code>.
  118. * @see Modifier
  119. */
  120. public abstract int getModifiers();
  121. /**
  122. * Sets the encoded modifiers of the member.
  123. *
  124. * @see Modifier
  125. */
  126. public abstract void setModifiers(int mod);
  127. /**
  128. * Returns the annotations associated with this member.
  129. * For example, if an annotation <code>@Author</code> is associated
  130. * with this member, the returned array contains an <code>Author</code>
  131. * object. The member values can be obtained by calling methods on
  132. * the <code>Author</code> object.
  133. *
  134. * @return an array of annotation-type objects.
  135. * @see CtClass#getAnnotations()
  136. */
  137. public abstract Object[] getAnnotations() throws ClassNotFoundException;
  138. /**
  139. * Obtains the name of the member.
  140. *
  141. * <p>As for constructor names, see <code>getName()</code>
  142. * in <code>CtConstructor</code>.
  143. *
  144. * @see CtConstructor#getName()
  145. */
  146. public abstract String getName();
  147. /**
  148. * Returns the character string representing the signature of the member.
  149. * If two members have the same signature (parameter types etc.),
  150. * <code>getSignature()</code> returns the same string.
  151. */
  152. public abstract String getSignature();
  153. /**
  154. * Obtains a user-defined attribute with the given name.
  155. * If that attribute is not found in the class file, this
  156. * method returns null.
  157. *
  158. * <p>Note that an attribute is a data block specified by
  159. * the class file format.
  160. * See {@link javassist.bytecode.AttributeInfo}.
  161. *
  162. * @param name attribute name
  163. */
  164. public abstract byte[] getAttribute(String name);
  165. /**
  166. * Adds a user-defined attribute. The attribute is saved in the class file.
  167. *
  168. * <p>Note that an attribute is a data block specified by
  169. * the class file format.
  170. * See {@link javassist.bytecode.AttributeInfo}.
  171. *
  172. * @param name attribute name
  173. * @param data attribute value
  174. */
  175. public abstract void setAttribute(String name, byte[] data);
  176. }