001: /*
002: * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javadoc;
027:
028: import com.sun.tools.javac.util.*;
029:
030: import com.sun.javadoc.*;
031:
032: /**
033: * Represents a see also documentation tag.
034: * The @see tag can be plain text, or reference a class or member.
035: *
036: * @version 06/10/97
037: * @author Kaiyang Liu (original)
038: * @author Robert Field (rewrite)
039: * @author Atul M Dambalkar
040: *
041: */
042: class SeeTagImpl extends TagImpl implements SeeTag, LayoutCharacters {
043:
044: //### TODO: Searching for classes, fields, and methods
045: //### should follow the normal rules applied by the compiler.
046:
047: /**
048: * where of where#what - i.e. the class name (may be empty)
049: */
050: private String where;
051:
052: /**
053: * what of where#what - i.e. the member (may be null)
054: */
055: private String what;
056:
057: private PackageDoc referencedPackage;
058: private ClassDoc referencedClass;
059: private MemberDoc referencedMember;
060:
061: String label = "";
062:
063: SeeTagImpl(DocImpl holder, String name, String text) {
064: super (holder, name, text);
065: parseSeeString();
066: if (where != null) {
067: ClassDocImpl container = null;
068: if (holder instanceof MemberDoc) {
069: container = (ClassDocImpl) ((ProgramElementDoc) holder)
070: .containingClass();
071: } else if (holder instanceof ClassDoc) {
072: container = (ClassDocImpl) holder;
073: }
074: findReferenced(container);
075: }
076: }
077:
078: /**
079: * get the class name part of @see, For instance,
080: * if the comment is @see String#startsWith(java.lang.String) .
081: * This function returns String.
082: * Returns null if format was not that of java reference.
083: * Return empty string if class name was not specified..
084: */
085: public String referencedClassName() {
086: return where;
087: }
088:
089: /**
090: * get the package referenced by @see. For instance,
091: * if the comment is @see java.lang
092: * This function returns a PackageDocImpl for java.lang
093: * Returns null if no known package found.
094: */
095: public PackageDoc referencedPackage() {
096: return referencedPackage;
097: }
098:
099: /**
100: * get the class referenced by the class name part of @see, For instance,
101: * if the comment is @see String#startsWith(java.lang.String) .
102: * This function returns a ClassDocImpl for java.lang.String.
103: * Returns null if class is not a class specified on the javadoc command line..
104: */
105: public ClassDoc referencedClass() {
106: return referencedClass;
107: }
108:
109: /**
110: * get the name of the member referenced by the prototype part of @see,
111: * For instance,
112: * if the comment is @see String#startsWith(java.lang.String) .
113: * This function returns "startsWith(java.lang.String)"
114: * Returns null if format was not that of java reference.
115: * Return empty string if member name was not specified..
116: */
117: public String referencedMemberName() {
118: return what;
119: }
120:
121: /**
122: * get the member referenced by the prototype part of @see,
123: * For instance,
124: * if the comment is @see String#startsWith(java.lang.String) .
125: * This function returns a MethodDocImpl for startsWith.
126: * Returns null if member could not be determined.
127: */
128: public MemberDoc referencedMember() {
129: return referencedMember;
130: }
131:
132: /**
133: * parse @see part of comment. Determine 'where' and 'what'
134: */
135: private void parseSeeString() {
136: int len = text.length();
137: if (len == 0) {
138: return;
139: }
140: switch (text.charAt(0)) {
141: case '<':
142: if (text.charAt(len - 1) != '>') {
143: docenv().warning(holder,
144: "tag.see.no_close_bracket_on_url", name, text);
145: }
146: return;
147: case '"':
148: if (len == 1 || text.charAt(len - 1) != '"') {
149: docenv().warning(holder, "tag.see.no_close_quote",
150: name, text);
151: } else {
152: // text = text.substring(1,len-1); // strip quotes
153: }
154: return;
155: }
156:
157: // check that the text is one word, with possible parentheses
158: // this part of code doesn't allow
159: // @see <a href=.....>asfd</a>
160: // comment it.
161:
162: // the code assumes that there is no initial white space.
163: int parens = 0;
164: int commentstart = 0;
165: int start = 0;
166: int cp;
167: for (int i = start; i < len; i += Character.charCount(cp)) {
168: cp = text.codePointAt(i);
169: switch (cp) {
170: case '(':
171: parens++;
172: break;
173: case ')':
174: parens--;
175: break;
176: case '[':
177: case ']':
178: case '.':
179: case '#':
180: break;
181: case ',':
182: if (parens <= 0) {
183: docenv().warning(holder,
184: "tag.see.malformed_see_tag", name, text);
185: return;
186: }
187: break;
188: case ' ':
189: case '\t':
190: case '\n':
191: case CR:
192: if (parens == 0) { //here onwards the comment starts.
193: commentstart = i;
194: i = len;
195: }
196: break;
197: default:
198: if (!Character.isJavaIdentifierPart(cp)) {
199: docenv().warning(holder,
200: "tag.see.illegal_character", name, "" + cp,
201: text);
202: }
203: break;
204: }
205: }
206: if (parens != 0) {
207: docenv().warning(holder, "tag.see.malformed_see_tag", name,
208: text);
209: return;
210: }
211:
212: String seetext = "";
213: String labeltext = "";
214:
215: if (commentstart > 0) {
216: seetext = text.substring(start, commentstart);
217: labeltext = text.substring(commentstart + 1);
218: // strip off the white space which can be between seetext and the
219: // actual label.
220: for (int i = 0; i < labeltext.length(); i++) {
221: char ch2 = labeltext.charAt(i);
222: if (!(ch2 == ' ' || ch2 == '\t' || ch2 == '\n')) {
223: label = labeltext.substring(i);
224: break;
225: }
226: }
227: } else {
228: seetext = text;
229: label = "";
230: }
231:
232: int sharp = seetext.indexOf('#');
233: if (sharp >= 0) {
234: // class#member
235: where = seetext.substring(0, sharp);
236: what = seetext.substring(sharp + 1);
237: } else {
238: if (seetext.indexOf('(') >= 0) {
239: docenv().warning(holder, "tag.see.missing_sharp", name,
240: text);
241: where = "";
242: what = seetext;
243: } else {
244: // no member specified, text names class
245: where = seetext;
246: what = null;
247: }
248: }
249: }
250:
251: /**
252: * Find what is referenced by the see also. If possible, sets
253: * referencedClass and referencedMember.
254: *
255: * @param containingClass the class containing the comment containing
256: * the tag. May be null, if, for example, it is a package comment.
257: */
258: private void findReferenced(ClassDocImpl containingClass) {
259: if (where.length() > 0) {
260: if (containingClass != null) {
261: referencedClass = containingClass.findClass(where);
262: } else {
263: referencedClass = docenv().lookupClass(where);
264: }
265: if (referencedClass == null
266: && holder() instanceof ProgramElementDoc) {
267: referencedClass = docenv().lookupClass(
268: ((ProgramElementDoc) holder())
269: .containingPackage().name()
270: + "." + where);
271: }
272:
273: if (referencedClass == null) { /* may just not be in this run */
274: // docenv().warning(holder, "tag.see.class_not_found",
275: // where, text);
276: // check if it's a package name
277: referencedPackage = docenv().lookupPackage(where);
278: return;
279: }
280: } else {
281: if (containingClass == null) {
282: docenv().warning(holder, "tag.see.class_not_specified",
283: name, text);
284: return;
285: } else {
286: referencedClass = containingClass;
287: }
288: }
289: where = referencedClass.qualifiedName();
290:
291: if (what == null) {
292: return;
293: } else {
294: int paren = what.indexOf('(');
295: String memName = (paren >= 0 ? what.substring(0, paren)
296: : what);
297: String[] paramarr;
298: if (paren > 0) {
299: // has parameter list -- should be method or constructor
300: paramarr = new ParameterParseMachine(what.substring(
301: paren, what.length())).parseParameters();
302: if (paramarr != null) {
303: referencedMember = findExecutableMember(memName,
304: paramarr, referencedClass);
305: } else {
306: referencedMember = null;
307: }
308: } else {
309: // no parameter list -- should be field
310: referencedMember = findExecutableMember(memName, null,
311: referencedClass);
312: FieldDoc fd = ((ClassDocImpl) referencedClass)
313: .findField(memName);
314: // when no args given, prefer fields over methods
315: if (referencedMember == null
316: || (fd != null && fd.containingClass()
317: .subclassOf(
318: referencedMember
319: .containingClass()))) {
320: referencedMember = fd;
321: }
322: }
323: if (referencedMember == null) {
324: docenv().warning(holder, "tag.see.can_not_find_member",
325: name, what, where);
326: }
327: }
328: }
329:
330: private MemberDoc findReferencedMethod(String memName,
331: String[] paramarr, ClassDoc referencedClass) {
332: MemberDoc meth = findExecutableMember(memName, paramarr,
333: referencedClass);
334: ClassDoc[] nestedclasses = referencedClass.innerClasses();
335: if (meth == null) {
336: for (int i = 0; i < nestedclasses.length; i++) {
337: meth = findReferencedMethod(memName, paramarr,
338: nestedclasses[i]);
339: if (meth != null) {
340: return meth;
341: }
342: }
343: }
344: return null;
345: }
346:
347: private MemberDoc findExecutableMember(String memName,
348: String[] paramarr, ClassDoc referencedClass) {
349: if (memName.equals(referencedClass.name())) {
350: return ((ClassDocImpl) referencedClass).findConstructor(
351: memName, paramarr);
352: } else { // it's a method.
353: return ((ClassDocImpl) referencedClass).findMethod(memName,
354: paramarr);
355: }
356: }
357:
358: // separate "int, String" from "(int, String)"
359: // (int i, String s) ==> [0] = "int", [1] = String
360: // (int[][], String[]) ==> [0] = "int[][]" // [1] = "String[]"
361: class ParameterParseMachine {
362: final int START = 0;
363: final int TYPE = 1;
364: final int NAME = 2;
365: final int TNSPACE = 3; // space between type and name
366: final int ARRAYDECORATION = 4;
367: final int ARRAYSPACE = 5;
368:
369: String parameters;
370:
371: StringBuffer typeId;
372:
373: ListBuffer<String> paramList;
374:
375: ParameterParseMachine(String parameters) {
376: this .parameters = parameters;
377: this .paramList = new ListBuffer<String>();
378: typeId = new StringBuffer();
379: }
380:
381: public String[] parseParameters() {
382: if (parameters.equals("()")) {
383: return new String[0];
384: } // now strip off '(' and ')'
385: int state = START;
386: int prevstate = START;
387: parameters = parameters.substring(1,
388: parameters.length() - 1);
389: int cp;
390: for (int index = 0; index < parameters.length(); index += Character
391: .charCount(cp)) {
392: cp = parameters.codePointAt(index);
393: switch (state) {
394: case START:
395: if (Character.isJavaIdentifierStart(cp)) {
396: typeId.append(Character.toChars(cp));
397: state = TYPE;
398: }
399: prevstate = START;
400: break;
401: case TYPE:
402: if (Character.isJavaIdentifierPart(cp) || cp == '.') {
403: typeId.append(Character.toChars(cp));
404: } else if (cp == '[') {
405: typeId.append('[');
406: state = ARRAYDECORATION;
407: } else if (Character.isWhitespace(cp)) {
408: state = TNSPACE;
409: } else if (cp == ',') { // no name, just type
410: addTypeToParamList();
411: state = START;
412: }
413: prevstate = TYPE;
414: break;
415: case TNSPACE:
416: if (Character.isJavaIdentifierStart(cp)) { // name
417: if (prevstate == ARRAYDECORATION) {
418: docenv().warning(holder,
419: "tag.missing_comma_space", name,
420: "(" + parameters + ")");
421: return (String[]) null;
422: }
423: addTypeToParamList();
424: state = NAME;
425: } else if (cp == '[') {
426: typeId.append('[');
427: state = ARRAYDECORATION;
428: } else if (cp == ',') { // just the type
429: addTypeToParamList();
430: state = START;
431: } // consume rest all
432: prevstate = TNSPACE;
433: break;
434: case ARRAYDECORATION:
435: if (cp == ']') {
436: typeId.append(']');
437: state = TNSPACE;
438: } else if (!Character.isWhitespace(cp)) {
439: docenv().warning(holder,
440: "tag.illegal_char_in_arr_dim", name,
441: "(" + parameters + ")");
442: return (String[]) null;
443: }
444: prevstate = ARRAYDECORATION;
445: break;
446: case NAME:
447: if (cp == ',') { // just consume everything till ','
448: state = START;
449: }
450: prevstate = NAME;
451: break;
452: }
453: }
454: if (state == ARRAYDECORATION
455: || (state == START && prevstate == TNSPACE)) {
456: docenv().warning(holder, "tag.illegal_see_tag",
457: "(" + parameters + ")");
458: }
459: if (typeId.length() > 0) {
460: paramList.append(typeId.toString());
461: }
462: return (String[]) paramList.toArray(new String[paramList
463: .length()]);
464: }
465:
466: void addTypeToParamList() {
467: if (typeId.length() > 0) {
468: paramList.append(typeId.toString());
469: typeId.setLength(0);
470: }
471: }
472: }
473:
474: /**
475: * Return the kind of this tag.
476: */
477: public String kind() {
478: return "@see";
479: }
480:
481: /**
482: * Return the label of the see tag.
483: */
484: public String label() {
485: return label;
486: }
487: }
|