001: /*
002: * MemberInfo.java - part of the CodeAid plugin for jEdit
003: * Copyright (C) 1999 Jason Ginchereau
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.acm.seguin.completer.info;
021:
022: import java.lang.reflect.Member;
023: import java.lang.reflect.Modifier;
024: import java.io.Serializable;
025: import java.util.StringTokenizer;
026:
027: /**
028: * The base class for class, field, constructor, and method info structures.
029: *
030: * @author Jason Ginchereau
031: */
032: public abstract class MemberInfo implements Serializable, Comparable {
033: private String declaringClass;
034: private int modifiers;
035: private String name;
036: private String comment;
037:
038: private SourceLocation location = SourceLocation.UNKNOWN;
039:
040: MemberInfo(Member m) {
041: this .declaringClass = m.getDeclaringClass().getName();
042: this .modifiers = m.getModifiers();
043: this .name = m.getName();
044: this .comment = null;
045: }
046:
047: MemberInfo(String declaringClass, int modifiers, String name,
048: String comment) {
049: this .declaringClass = declaringClass;
050: this .modifiers = modifiers;
051: this .name = name;
052: setComment(comment);
053: }
054:
055: /**
056: * Gets the fully-qualified name of the class that declares this member.
057: **/
058: public final String getDeclaringClass() {
059: return declaringClass;
060: }
061:
062: /**
063: * Gets the modifiers mask of this member, that can be decoded using the
064: * <code>java.lang.reflect.Modifier</code> class.
065: **/
066: public final int getModifiers() {
067: return modifiers;
068: }
069:
070: /**
071: * Gets the simple name of this member.
072: **/
073: public final String getName() {
074: return name;
075: }
076:
077: /**
078: * Gets the fully-qualified type of this member.
079: **/
080: public abstract String getType();
081:
082: public abstract String getLine();
083:
084: /**
085: * Gets the one-line comment associated with this member.
086: *
087: * @return the first-sentence portion of the javadoc comment, or
088: * <code>null</code> if there is no comment associated with
089: * this member.
090: **/
091: public final String getOneLineComment() {
092: if (comment == null) {
093: return null;
094: }
095: // TODO: return only first line
096: int i = comment.indexOf(". ");
097: int j = comment.indexOf(".\n");
098: if (i >= 0 && (j < 0 || i < j)) {
099: return comment.substring(0, i + 1);
100: } else if (j >= 0 && (i < 0 || j < i)) {
101: return comment.substring(0, j + 1);
102: } else {
103: return comment;
104: }
105: }
106:
107: /**
108: * Gets the comment associated with this member.
109: *
110: * @return the javadoc comment, or <code>null</code> if there is no comment
111: * associated with this member.
112: **/
113: public final String getComment() {
114: return comment;
115: }
116:
117: public final void setComment(String comment) {
118:
119: if (comment != null) {
120: StringTokenizer st = new StringTokenizer(comment, "\r\n");
121: comment = "";
122: while (st.hasMoreTokens()) {
123: String line = st.nextToken().trim();
124: if (line.startsWith("/**")) {
125: line = line.substring(3);
126: } else if (line.startsWith("*/")) {
127: line = line.substring(2);
128: } else if (line.startsWith("*")) {
129: line = line.substring(1);
130: }
131: comment += line.trim() + '\n';
132: }
133: comment = comment.trim();
134: }
135: this .comment = comment;
136: }
137:
138: /**
139: * Gets the location in the source code where this member was
140: * declared.
141: *
142: * @return the <code>SourceLocation</code>, or
143: * <code>SourceLocation.UNKNOWN</code> if the location is unknown.
144: **/
145: public final SourceLocation getSourceLocation() {
146: return location;
147: }
148:
149: public final void setSourceLocation(SourceLocation location) {
150: this .location = location;
151: }
152:
153: static String modifiersToString(int modifiers) {
154: String m = Modifier.toString(modifiers);
155: if (m.length() > 0) {
156: m += ' ';
157: }
158: return m;
159: }
160:
161: public boolean equals(Object o) {
162: return compareTo((MemberInfo) o) == 0;
163: }
164:
165: /**
166: * Just calls <code>compareTo(MemberInfo)</code>.
167: **/
168: public final int compareTo(Object o) {
169: return compareTo((MemberInfo) o);
170: }
171:
172: /**
173: * Implements a consistent sorting policy for all <code>MemberInfo</code>
174: * items that is primarily based on their simple name.
175: **/
176: public abstract int compareTo(MemberInfo mi);
177:
178: public String toString() {
179: return getName();
180: }
181: }
|