001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist;
017:
018: /**
019: * An instance of <code>CtMember</code> represents a field, a constructor,
020: * or a method.
021: */
022: public abstract class CtMember {
023: protected CtMember next; // for internal use
024: protected CtClass declaringClass;
025:
026: protected CtMember(CtClass clazz) {
027: declaringClass = clazz;
028: }
029:
030: static CtMember append(CtMember list, CtMember previousTail,
031: CtMember tail) {
032: tail.next = null;
033: if (list == null)
034: return tail;
035: else {
036: previousTail.next = tail;
037: return list;
038: }
039: }
040:
041: static CtMember append(CtMember list, CtMember tail) {
042: tail.next = null;
043: if (list == null)
044: return tail;
045: else {
046: CtMember lst = list;
047: while (lst.next != null)
048: lst = lst.next;
049:
050: lst.next = tail;
051: return list;
052: }
053: }
054:
055: static int count(CtMember f) {
056: int n = 0;
057: while (f != null) {
058: ++n;
059: f = f.next;
060: }
061:
062: return n;
063: }
064:
065: static CtMember remove(CtMember list, CtMember m) {
066: CtMember top = list;
067: if (list == null)
068: return null;
069: else if (list == m)
070: return list.next;
071: else
072: while (list.next != null) {
073: if (list.next == m) {
074: list.next = list.next.next;
075: break;
076: }
077:
078: list = list.next;
079: }
080:
081: return top;
082: }
083:
084: public String toString() {
085: StringBuffer buffer = new StringBuffer(getClass().getName());
086: buffer.append("@");
087: buffer.append(Integer.toHexString(hashCode()));
088: buffer.append("[");
089: buffer.append(Modifier.toString(getModifiers()));
090: extendToString(buffer);
091: buffer.append("]");
092: return buffer.toString();
093: }
094:
095: /**
096: * Invoked by {@link #toString()} to add to the buffer and provide the
097: * complete value. Subclasses should invoke this method, adding a
098: * space before each token. The modifiers for the member are
099: * provided first; subclasses should provide additional data such
100: * as return type, field or method name, etc.
101: */
102: protected abstract void extendToString(StringBuffer buffer);
103:
104: /**
105: * Returns the class that declares this member.
106: */
107: public CtClass getDeclaringClass() {
108: return declaringClass;
109: }
110:
111: /**
112: * Returns true if this member is accessible from the given class.
113: */
114: public boolean visibleFrom(CtClass clazz) {
115: int mod = getModifiers();
116: if (Modifier.isPublic(mod))
117: return true;
118: else if (Modifier.isPrivate(mod))
119: return clazz == declaringClass;
120: else { // package or protected
121: String declName = declaringClass.getPackageName();
122: String fromName = clazz.getPackageName();
123: boolean visible;
124: if (declName == null)
125: visible = fromName == null;
126: else
127: visible = declName.equals(fromName);
128:
129: if (!visible && Modifier.isProtected(mod))
130: return clazz.subclassOf(declaringClass);
131:
132: return visible;
133: }
134: }
135:
136: /**
137: * Obtains the modifiers of the member.
138: *
139: * @return modifiers encoded with
140: * <code>javassist.Modifier</code>.
141: * @see Modifier
142: */
143: public abstract int getModifiers();
144:
145: /**
146: * Sets the encoded modifiers of the member.
147: *
148: * @see Modifier
149: */
150: public abstract void setModifiers(int mod);
151:
152: /**
153: * Returns the annotations associated with this member.
154: * For example, if an annotation <code>@Author</code> is associated
155: * with this member, the returned array contains an <code>Author</code>
156: * object. The member values can be obtained by calling methods on
157: * the <code>Author</code> object.
158: *
159: * @return an array of annotation-type objects.
160: * @see CtClass#getAnnotations()
161: */
162: public abstract Object[] getAnnotations()
163: throws ClassNotFoundException;
164:
165: /**
166: * Returns the annotations associated with this member.
167: * This method is equivalent to <code>getAnnotations()</code>
168: * except that, if any annotations are not on the classpath,
169: * they are not included in the returned array.
170: *
171: * @return an array of annotation-type objects.
172: * @see #getAnnotations()
173: * @see CtClass#getAvailableAnnotations()
174: * @since 3.3
175: */
176: public abstract Object[] getAvailableAnnotations()
177: throws ClassNotFoundException;
178:
179: /**
180: * Obtains the name of the member.
181: *
182: * <p>As for constructor names, see <code>getName()</code>
183: * in <code>CtConstructor</code>.
184: *
185: * @see CtConstructor#getName()
186: */
187: public abstract String getName();
188:
189: /**
190: * Returns the character string representing the signature of the member.
191: * If two members have the same signature (parameter types etc.),
192: * <code>getSignature()</code> returns the same string.
193: */
194: public abstract String getSignature();
195:
196: /**
197: * Obtains a user-defined attribute with the given name.
198: * If that attribute is not found in the class file, this
199: * method returns null.
200: *
201: * <p>Note that an attribute is a data block specified by
202: * the class file format.
203: * See {@link javassist.bytecode.AttributeInfo}.
204: *
205: * @param name attribute name
206: */
207: public abstract byte[] getAttribute(String name);
208:
209: /**
210: * Adds a user-defined attribute. The attribute is saved in the class file.
211: *
212: * <p>Note that an attribute is a data block specified by
213: * the class file format.
214: * See {@link javassist.bytecode.AttributeInfo}.
215: *
216: * @param name attribute name
217: * @param data attribute value
218: */
219: public abstract void setAttribute(String name, byte[] data);
220: }
|