|
1 /* |
|
2 * Copyright 2001-2004 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.doclets.internal.toolkit.taglets; |
|
27 |
|
28 import com.sun.javadoc.*; |
|
29 import com.sun.tools.doclets.internal.toolkit.util.*; |
|
30 import java.util.*; |
|
31 |
|
32 /** |
|
33 * A taglet that represents the @throws tag. |
|
34 * |
|
35 * This code is not part of an API. |
|
36 * It is implementation that is subject to change. |
|
37 * Do not use it as an API |
|
38 * |
|
39 * @author Jamie Ho |
|
40 * @since 1.4 |
|
41 */ |
|
42 public class ThrowsTaglet extends BaseExecutableMemberTaglet |
|
43 implements InheritableTaglet { |
|
44 |
|
45 public ThrowsTaglet() { |
|
46 name = "throws"; |
|
47 } |
|
48 |
|
49 /** |
|
50 * {@inheritDoc} |
|
51 */ |
|
52 public void inherit(DocFinder.Input input, DocFinder.Output output) { |
|
53 ClassDoc exception; |
|
54 if (input.tagId == null) { |
|
55 ThrowsTag throwsTag = (ThrowsTag) input.tag; |
|
56 exception = throwsTag.exception(); |
|
57 input.tagId = exception == null ? |
|
58 throwsTag.exceptionName() : |
|
59 throwsTag.exception().qualifiedName(); |
|
60 } else { |
|
61 exception = input.method.containingClass().findClass(input.tagId); |
|
62 } |
|
63 |
|
64 ThrowsTag[] tags = input.method.throwsTags(); |
|
65 for (int i = 0; i < tags.length; i++) { |
|
66 if (input.tagId.equals(tags[i].exceptionName()) || |
|
67 (tags[i].exception() != null && |
|
68 (input.tagId.equals(tags[i].exception().qualifiedName())))) { |
|
69 output.holder = input.method; |
|
70 output.holderTag = tags[i]; |
|
71 output.inlineTags = input.isFirstSentence ? |
|
72 tags[i].firstSentenceTags() : tags[i].inlineTags(); |
|
73 output.tagList.add(tags[i]); |
|
74 } else if (exception != null && tags[i].exception() != null && |
|
75 tags[i].exception().subclassOf(exception)) { |
|
76 output.tagList.add(tags[i]); |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 /** |
|
82 * Add links for exceptions that are declared but not documented. |
|
83 */ |
|
84 private TagletOutput linkToUndocumentedDeclaredExceptions( |
|
85 Type[] declaredExceptionTypes, Set alreadyDocumented, |
|
86 TagletWriter writer) { |
|
87 TagletOutput result = writer.getOutputInstance(); |
|
88 //Add links to the exceptions declared but not documented. |
|
89 for (int i = 0; i < declaredExceptionTypes.length; i++) { |
|
90 if (declaredExceptionTypes[i].asClassDoc() != null && |
|
91 ! alreadyDocumented.contains( |
|
92 declaredExceptionTypes[i].asClassDoc().name()) && |
|
93 ! alreadyDocumented.contains( |
|
94 declaredExceptionTypes[i].asClassDoc().qualifiedName())) { |
|
95 if (alreadyDocumented.size() == 0) { |
|
96 result.appendOutput(writer.getThrowsHeader()); |
|
97 } |
|
98 result.appendOutput(writer.throwsTagOutput(declaredExceptionTypes[i])); |
|
99 alreadyDocumented.add(declaredExceptionTypes[i].asClassDoc().name()); |
|
100 } |
|
101 } |
|
102 return result; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Inherit throws documentation for exceptions that were declared but not |
|
107 * documented. |
|
108 */ |
|
109 private TagletOutput inheritThrowsDocumentation(Doc holder, |
|
110 Type[] declaredExceptionTypes, Set alreadyDocumented, |
|
111 TagletWriter writer) { |
|
112 TagletOutput result = writer.getOutputInstance(); |
|
113 if (holder instanceof MethodDoc) { |
|
114 Set declaredExceptionTags = new LinkedHashSet(); |
|
115 for (int j = 0; j < declaredExceptionTypes.length; j++) { |
|
116 DocFinder.Output inheritedDoc = |
|
117 DocFinder.search(new DocFinder.Input((MethodDoc) holder, this, |
|
118 declaredExceptionTypes[j].typeName())); |
|
119 if (inheritedDoc.tagList.size() == 0) { |
|
120 inheritedDoc = DocFinder.search(new DocFinder.Input( |
|
121 (MethodDoc) holder, this, |
|
122 declaredExceptionTypes[j].qualifiedTypeName())); |
|
123 } |
|
124 declaredExceptionTags.addAll(inheritedDoc.tagList); |
|
125 } |
|
126 result.appendOutput(throwsTagsOutput( |
|
127 (ThrowsTag[]) declaredExceptionTags.toArray(new ThrowsTag[] {}), |
|
128 writer, alreadyDocumented, false)); |
|
129 } |
|
130 return result; |
|
131 } |
|
132 |
|
133 /** |
|
134 * {@inheritDoc} |
|
135 */ |
|
136 public TagletOutput getTagletOutput(Doc holder, TagletWriter writer) { |
|
137 ExecutableMemberDoc execHolder = (ExecutableMemberDoc) holder; |
|
138 ThrowsTag[] tags = execHolder.throwsTags(); |
|
139 TagletOutput result = writer.getOutputInstance(); |
|
140 HashSet alreadyDocumented = new HashSet(); |
|
141 if (tags.length > 0) { |
|
142 result.appendOutput(throwsTagsOutput( |
|
143 execHolder.throwsTags(), writer, alreadyDocumented, true)); |
|
144 } |
|
145 result.appendOutput(inheritThrowsDocumentation(holder, |
|
146 execHolder.thrownExceptionTypes(), alreadyDocumented, writer)); |
|
147 result.appendOutput(linkToUndocumentedDeclaredExceptions( |
|
148 execHolder.thrownExceptionTypes(), alreadyDocumented, writer)); |
|
149 return result; |
|
150 } |
|
151 |
|
152 |
|
153 /** |
|
154 * Given an array of <code>Tag</code>s representing this custom |
|
155 * tag, return its string representation. |
|
156 * @param throwTags the array of <code>ThrowsTag</code>s to convert. |
|
157 * @param writer the TagletWriter that will write this tag. |
|
158 * @param alreadyDocumented the set of exceptions that have already |
|
159 * been documented. |
|
160 * @param allowDups True if we allow duplicate throws tags to be documented. |
|
161 * @return the TagletOutput representation of this <code>Tag</code>. |
|
162 */ |
|
163 protected TagletOutput throwsTagsOutput(ThrowsTag[] throwTags, |
|
164 TagletWriter writer, Set alreadyDocumented, boolean allowDups) { |
|
165 TagletOutput result = writer.getOutputInstance(); |
|
166 if (throwTags.length > 0) { |
|
167 for (int i = 0; i < throwTags.length; ++i) { |
|
168 ThrowsTag tt = throwTags[i]; |
|
169 ClassDoc cd = tt.exception(); |
|
170 if ((!allowDups) && (alreadyDocumented.contains(tt.exceptionName()) || |
|
171 (cd != null && alreadyDocumented.contains(cd.qualifiedName())))) { |
|
172 continue; |
|
173 } |
|
174 if (alreadyDocumented.size() == 0) { |
|
175 result.appendOutput(writer.getThrowsHeader()); |
|
176 } |
|
177 result.appendOutput(writer.throwsTagOutput(tt)); |
|
178 alreadyDocumented.add(cd != null ? |
|
179 cd.qualifiedName() : tt.exceptionName()); |
|
180 } |
|
181 } |
|
182 return result; |
|
183 } |
|
184 } |