Wed, 23 Jan 2013 13:27:24 -0800
8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.formats.html;
28 import java.io.*;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.formats.html.markup.*;
32 import com.sun.tools.doclets.internal.toolkit.*;
33 import com.sun.tools.doclets.internal.toolkit.util.*;
35 /**
36 * Writes method documentation in HTML format.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own risk.
40 * This code and its internal interfaces are subject to change or
41 * deletion without notice.</b>
42 *
43 * @author Robert Field
44 * @author Atul M Dambalkar
45 * @author Jamie Ho (rewrite)
46 * @author Bhavesh Patel (Modified)
47 */
48 public class MethodWriterImpl extends AbstractExecutableMemberWriter
49 implements MethodWriter, MemberSummaryWriter {
51 /**
52 * Construct a new MethodWriterImpl.
53 *
54 * @param writer the writer for the class that the methods belong to.
55 * @param classDoc the class being documented.
56 */
57 public MethodWriterImpl(SubWriterHolderWriter writer, ClassDoc classDoc) {
58 super(writer, classDoc);
59 }
61 /**
62 * Construct a new MethodWriterImpl.
63 *
64 * @param writer The writer for the class that the methods belong to.
65 */
66 public MethodWriterImpl(SubWriterHolderWriter writer) {
67 super(writer);
68 }
70 /**
71 * {@inheritDoc}
72 */
73 public Content getMemberSummaryHeader(ClassDoc classDoc,
74 Content memberSummaryTree) {
75 memberSummaryTree.addContent(HtmlConstants.START_OF_METHOD_SUMMARY);
76 Content memberTree = writer.getMemberTreeHeader();
77 writer.addSummaryHeader(this, classDoc, memberTree);
78 return memberTree;
79 }
81 /**
82 * {@inheritDoc}
83 */
84 public Content getMethodDetailsTreeHeader(ClassDoc classDoc,
85 Content memberDetailsTree) {
86 memberDetailsTree.addContent(HtmlConstants.START_OF_METHOD_DETAILS);
87 Content methodDetailsTree = writer.getMemberTreeHeader();
88 methodDetailsTree.addContent(writer.getMarkerAnchor("method_detail"));
89 Content heading = HtmlTree.HEADING(HtmlConstants.DETAILS_HEADING,
90 writer.methodDetailsLabel);
91 methodDetailsTree.addContent(heading);
92 return methodDetailsTree;
93 }
95 /**
96 * {@inheritDoc}
97 */
98 public Content getMethodDocTreeHeader(MethodDoc method,
99 Content methodDetailsTree) {
100 String erasureAnchor;
101 if ((erasureAnchor = getErasureAnchor(method)) != null) {
102 methodDetailsTree.addContent(writer.getMarkerAnchor((erasureAnchor)));
103 }
104 methodDetailsTree.addContent(
105 writer.getMarkerAnchor(writer.getAnchor(method)));
106 Content methodDocTree = writer.getMemberTreeHeader();
107 Content heading = new HtmlTree(HtmlConstants.MEMBER_HEADING);
108 heading.addContent(method.name());
109 methodDocTree.addContent(heading);
110 return methodDocTree;
111 }
113 /**
114 * Get the signature for the given method.
115 *
116 * @param method the method being documented.
117 * @return a content object for the signature
118 */
119 public Content getSignature(MethodDoc method) {
120 writer.displayLength = 0;
121 Content pre = new HtmlTree(HtmlTag.PRE);
122 writer.addAnnotationInfo(method, pre);
123 addModifiers(method, pre);
124 addTypeParameters(method, pre);
125 addReturnType(method, pre);
126 if (configuration.linksource) {
127 Content methodName = new StringContent(method.name());
128 writer.addSrcLink(method, methodName, pre);
129 } else {
130 addName(method.name(), pre);
131 }
132 addParameters(method, pre);
133 addReceiverAnnotations(method, pre);
134 addExceptions(method, pre);
135 return pre;
136 }
138 /**
139 * {@inheritDoc}
140 */
141 public void addDeprecated(MethodDoc method, Content methodDocTree) {
142 addDeprecatedInfo(method, methodDocTree);
143 }
145 /**
146 * {@inheritDoc}
147 */
148 public void addComments(Type holder, MethodDoc method, Content methodDocTree) {
149 ClassDoc holderClassDoc = holder.asClassDoc();
150 if (method.inlineTags().length > 0) {
151 if (holder.asClassDoc().equals(classdoc) ||
152 (! (holderClassDoc.isPublic() ||
153 Util.isLinkable(holderClassDoc, configuration)))) {
154 writer.addInlineComment(method, methodDocTree);
155 } else {
156 Content link = new RawHtml(
157 writer.getDocLink(LinkInfoImpl.CONTEXT_METHOD_DOC_COPY,
158 holder.asClassDoc(), method,
159 holder.asClassDoc().isIncluded() ?
160 holder.typeName() : holder.qualifiedTypeName(),
161 false));
162 Content codelLink = HtmlTree.CODE(link);
163 Content strong = HtmlTree.STRONG(holder.asClassDoc().isClass()?
164 writer.descfrmClassLabel : writer.descfrmInterfaceLabel);
165 strong.addContent(writer.getSpace());
166 strong.addContent(codelLink);
167 methodDocTree.addContent(HtmlTree.DIV(HtmlStyle.block, strong));
168 writer.addInlineComment(method, methodDocTree);
169 }
170 }
171 }
173 /**
174 * {@inheritDoc}
175 */
176 public void addTags(MethodDoc method, Content methodDocTree) {
177 writer.addTagsInfo(method, methodDocTree);
178 }
180 /**
181 * {@inheritDoc}
182 */
183 public Content getMethodDetails(Content methodDetailsTree) {
184 return getMemberTree(methodDetailsTree);
185 }
187 /**
188 * {@inheritDoc}
189 */
190 public Content getMethodDoc(Content methodDocTree,
191 boolean isLastContent) {
192 return getMemberTree(methodDocTree, isLastContent);
193 }
195 /**
196 * Close the writer.
197 */
198 public void close() throws IOException {
199 writer.close();
200 }
202 public int getMemberKind() {
203 return VisibleMemberMap.METHODS;
204 }
206 /**
207 * {@inheritDoc}
208 */
209 public void addSummaryLabel(Content memberTree) {
210 Content label = HtmlTree.HEADING(HtmlConstants.SUMMARY_HEADING,
211 writer.getResource("doclet.Method_Summary"));
212 memberTree.addContent(label);
213 }
215 /**
216 * {@inheritDoc}
217 */
218 public String getTableSummary() {
219 return configuration.getText("doclet.Member_Table_Summary",
220 configuration.getText("doclet.Method_Summary"),
221 configuration.getText("doclet.methods"));
222 }
224 /**
225 * {@inheritDoc}
226 */
227 public String getCaption() {
228 return configuration.getText("doclet.Methods");
229 }
231 /**
232 * {@inheritDoc}
233 */
234 public String[] getSummaryTableHeader(ProgramElementDoc member) {
235 String[] header = new String[] {
236 writer.getModifierTypeHeader(),
237 configuration.getText("doclet.0_and_1",
238 configuration.getText("doclet.Method"),
239 configuration.getText("doclet.Description"))
240 };
241 return header;
242 }
244 /**
245 * {@inheritDoc}
246 */
247 public void addSummaryAnchor(ClassDoc cd, Content memberTree) {
248 memberTree.addContent(writer.getMarkerAnchor("method_summary"));
249 }
251 /**
252 * {@inheritDoc}
253 */
254 public void addInheritedSummaryAnchor(ClassDoc cd, Content inheritedTree) {
255 inheritedTree.addContent(writer.getMarkerAnchor(
256 "methods_inherited_from_class_" +
257 configuration.getClassName(cd)));
258 }
260 /**
261 * {@inheritDoc}
262 */
263 public void addInheritedSummaryLabel(ClassDoc cd, Content inheritedTree) {
264 Content classLink = new RawHtml(writer.getPreQualifiedClassLink(
265 LinkInfoImpl.CONTEXT_MEMBER, cd, false));
266 Content label = new StringContent(cd.isClass() ?
267 configuration.getText("doclet.Methods_Inherited_From_Class") :
268 configuration.getText("doclet.Methods_Inherited_From_Interface"));
269 Content labelHeading = HtmlTree.HEADING(HtmlConstants.INHERITED_SUMMARY_HEADING,
270 label);
271 labelHeading.addContent(writer.getSpace());
272 labelHeading.addContent(classLink);
273 inheritedTree.addContent(labelHeading);
274 }
276 /**
277 * {@inheritDoc}
278 */
279 protected void addSummaryType(ProgramElementDoc member, Content tdSummaryType) {
280 MethodDoc meth = (MethodDoc)member;
281 addModifierAndType(meth, meth.returnType(), tdSummaryType);
282 }
284 /**
285 * {@inheritDoc}
286 */
287 protected static void addOverridden(HtmlDocletWriter writer,
288 Type overriddenType, MethodDoc method, Content dl) {
289 if (writer.configuration.nocomment) {
290 return;
291 }
292 ClassDoc holderClassDoc = overriddenType.asClassDoc();
293 if (! (holderClassDoc.isPublic() ||
294 Util.isLinkable(holderClassDoc, writer.configuration))) {
295 //This is an implementation detail that should not be documented.
296 return;
297 }
298 if (overriddenType.asClassDoc().isIncluded() && ! method.isIncluded()) {
299 //The class is included but the method is not. That means that it
300 //is not visible so don't document this.
301 return;
302 }
303 Content label = writer.overridesLabel;
304 int context = LinkInfoImpl.CONTEXT_METHOD_OVERRIDES;
306 if (method != null) {
307 if (overriddenType.asClassDoc().isAbstract() && method.isAbstract()){
308 //Abstract method is implemented from abstract class,
309 //not overridden
310 label = writer.specifiedByLabel;
311 context = LinkInfoImpl.CONTEXT_METHOD_SPECIFIED_BY;
312 }
313 Content dt = HtmlTree.DT(HtmlTree.STRONG(label));
314 dl.addContent(dt);
315 Content overriddenTypeLink = new RawHtml(
316 writer.getLink(new LinkInfoImpl(writer.configuration, context, overriddenType)));
317 Content codeOverridenTypeLink = HtmlTree.CODE(overriddenTypeLink);
318 String name = method.name();
319 Content methlink = new RawHtml(writer.getLink(
320 new LinkInfoImpl(writer.configuration, LinkInfoImpl.CONTEXT_MEMBER,
321 overriddenType.asClassDoc(),
322 writer.getAnchor(method), name, false)));
323 Content codeMethLink = HtmlTree.CODE(methlink);
324 Content dd = HtmlTree.DD(codeMethLink);
325 dd.addContent(writer.getSpace());
326 dd.addContent(writer.getResource("doclet.in_class"));
327 dd.addContent(writer.getSpace());
328 dd.addContent(codeOverridenTypeLink);
329 dl.addContent(dd);
330 }
331 }
333 /**
334 * Parse the <Code> tag and return the text.
335 */
336 protected String parseCodeTag(String tag){
337 if(tag == null){
338 return "";
339 }
341 String lc = tag.toLowerCase();
342 int begin = lc.indexOf("<code>");
343 int end = lc.indexOf("</code>");
344 if(begin == -1 || end == -1 || end <= begin){
345 return tag;
346 } else {
347 return tag.substring(begin + 6, end);
348 }
349 }
351 /**
352 * {@inheritDoc}
353 */
354 protected static void addImplementsInfo(HtmlDocletWriter writer,
355 MethodDoc method, Content dl) {
356 if(writer.configuration.nocomment){
357 return;
358 }
359 ImplementedMethods implementedMethodsFinder =
360 new ImplementedMethods(method, writer.configuration);
361 MethodDoc[] implementedMethods = implementedMethodsFinder.build();
362 for (int i = 0; i < implementedMethods.length; i++) {
363 MethodDoc implementedMeth = implementedMethods[i];
364 Type intfac = implementedMethodsFinder.getMethodHolder(implementedMeth);
365 Content intfaclink = new RawHtml(writer.getLink(new LinkInfoImpl(
366 writer.configuration, LinkInfoImpl.CONTEXT_METHOD_SPECIFIED_BY, intfac)));
367 Content codeIntfacLink = HtmlTree.CODE(intfaclink);
368 Content dt = HtmlTree.DT(HtmlTree.STRONG(writer.specifiedByLabel));
369 dl.addContent(dt);
370 Content methlink = new RawHtml(writer.getDocLink(
371 LinkInfoImpl.CONTEXT_MEMBER, implementedMeth,
372 implementedMeth.name(), false));
373 Content codeMethLink = HtmlTree.CODE(methlink);
374 Content dd = HtmlTree.DD(codeMethLink);
375 dd.addContent(writer.getSpace());
376 dd.addContent(writer.getResource("doclet.in_interface"));
377 dd.addContent(writer.getSpace());
378 dd.addContent(codeIntfacLink);
379 dl.addContent(dd);
380 }
381 }
383 /**
384 * Add the return type.
385 *
386 * @param method the method being documented.
387 * @param htmltree the content tree to which the return type will be added
388 */
389 protected void addReturnType(MethodDoc method, Content htmltree) {
390 Type type = method.returnType();
391 if (type != null) {
392 Content linkContent = new RawHtml(writer.getLink(
393 new LinkInfoImpl(configuration, LinkInfoImpl.CONTEXT_RETURN_TYPE, type)));
394 htmltree.addContent(linkContent);
395 htmltree.addContent(writer.getSpace());
396 }
397 }
399 /**
400 * {@inheritDoc}
401 */
402 protected Content getNavSummaryLink(ClassDoc cd, boolean link) {
403 if (link) {
404 return writer.getHyperLink((cd == null)?
405 "method_summary":
406 "methods_inherited_from_class_" +
407 configuration.getClassName(cd),
408 writer.getResource("doclet.navMethod"));
409 } else {
410 return writer.getResource("doclet.navMethod");
411 }
412 }
414 /**
415 * {@inheritDoc}
416 */
417 protected void addNavDetailLink(boolean link, Content liNav) {
418 if (link) {
419 liNav.addContent(writer.getHyperLink("method_detail",
420 writer.getResource("doclet.navMethod")));
421 } else {
422 liNav.addContent(writer.getResource("doclet.navMethod"));
423 }
424 }
425 }