author | ksrini |
Mon, 10 Oct 2016 11:21:00 -0700 | |
changeset 41452 | ddaef4bba083 |
parent 39364 | bd6d4a7936b4 |
child 43261 | d377e97291d8 |
permissions | -rw-r--r-- |
35426 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, 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 |
*/ |
|
25 |
||
26 |
/** |
|
27 |
* A utility class. |
|
28 |
* |
|
29 |
* <p><b>This is NOT part of any supported API. |
|
30 |
* If you write code that depends on this, you do so at your own risk. |
|
31 |
* This code and its internal interfaces are subject to change or |
|
32 |
* deletion without notice.</b> |
|
33 |
*/ |
|
34 |
||
35 |
package jdk.javadoc.internal.doclets.toolkit; |
|
36 |
||
37 |
import com.sun.source.doctree.DocCommentTree; |
|
38 |
import com.sun.source.doctree.DocTree; |
|
39 |
import com.sun.source.doctree.IdentifierTree; |
|
40 |
import com.sun.source.doctree.ReferenceTree; |
|
41 |
import com.sun.source.doctree.TextTree; |
|
42 |
import com.sun.source.util.DocTreeFactory; |
|
43 |
import com.sun.source.util.DocTreePath; |
|
44 |
import com.sun.source.util.DocTrees; |
|
45 |
import com.sun.source.util.TreePath; |
|
46 |
import java.util.ArrayList; |
|
47 |
import java.util.HashMap; |
|
48 |
import java.util.List; |
|
49 |
import javax.lang.model.element.Element; |
|
50 |
import javax.lang.model.element.ElementKind; |
|
51 |
import javax.lang.model.element.ExecutableElement; |
|
52 |
import javax.lang.model.element.Name; |
|
53 |
import javax.lang.model.element.PackageElement; |
|
54 |
import javax.lang.model.element.VariableElement; |
|
55 |
import javax.lang.model.util.Elements; |
|
56 |
import javax.tools.FileObject; |
|
57 |
import jdk.javadoc.internal.doclets.toolkit.util.Utils; |
|
58 |
||
59 |
public class CommentUtils { |
|
60 |
||
61 |
final Configuration configuration; |
|
62 |
final DocTreeFactory treeFactory; |
|
63 |
final HashMap<Element, DocCommentDuo> dcTreesMap = new HashMap<>(); |
|
64 |
final DocTrees trees; |
|
65 |
final Elements elementUtils; |
|
66 |
||
67 |
protected CommentUtils(Configuration configuration) { |
|
68 |
this.configuration = configuration; |
|
39364
bd6d4a7936b4
8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents:
35426
diff
changeset
|
69 |
trees = configuration.docEnv.getDocTrees(); |
35426 | 70 |
treeFactory = trees.getDocTreeFactory(); |
39364
bd6d4a7936b4
8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents:
35426
diff
changeset
|
71 |
elementUtils = configuration.docEnv.getElementUtils(); |
35426 | 72 |
} |
73 |
||
74 |
public List<? extends DocTree> makePropertyDescriptionTree(List<? extends DocTree> content) { |
|
75 |
List<DocTree> out = new ArrayList<>(); |
|
76 |
Name name = elementUtils.getName("propertyDescription"); |
|
77 |
out.add(treeFactory.newUnknownBlockTagTree(name, content)); |
|
78 |
return out; |
|
79 |
} |
|
80 |
||
81 |
public List<? extends DocTree> makePropertyDescriptionTree(String content) { |
|
82 |
List<DocTree> inlist = new ArrayList<>(); |
|
83 |
inlist.add(treeFactory.newCommentTree(content)); |
|
84 |
List<DocTree> out = new ArrayList<>(); |
|
85 |
Name name = elementUtils.getName("propertyDescription"); |
|
86 |
out.add(treeFactory.newUnknownBlockTagTree(name, inlist)); |
|
87 |
return out; |
|
88 |
} |
|
89 |
||
90 |
public List<? extends DocTree> makeFirstSentenceTree(String content) { |
|
91 |
List<DocTree> out = new ArrayList<>(); |
|
92 |
out.add(treeFactory.newTextTree(content)); |
|
93 |
return out; |
|
94 |
} |
|
95 |
||
96 |
public DocTree makeSeeTree(String sig, Element e) { |
|
97 |
List<DocTree> list = new ArrayList<>(); |
|
98 |
list.add(treeFactory.newReferenceTree(sig)); |
|
99 |
return treeFactory.newSeeTree(list); |
|
100 |
} |
|
101 |
||
102 |
public DocTree makeTextTree(String content) { |
|
103 |
TextTree text = treeFactory.newTextTree(content); |
|
104 |
return (DocTree) text; |
|
105 |
} |
|
106 |
||
107 |
public void setEnumValuesTree(Configuration config, Element e) { |
|
108 |
Utils utils = config.utils; |
|
109 |
String klassName = utils.getSimpleName(utils.getEnclosingTypeElement(e)); |
|
110 |
||
41452 | 111 |
List<DocTree> fullBody = new ArrayList<>(); |
112 |
fullBody.add(treeFactory.newTextTree(config.getText("doclet.enum_values_doc.fullbody", klassName))); |
|
35426 | 113 |
|
114 |
List<DocTree> descriptions = new ArrayList<>(); |
|
115 |
descriptions.add(treeFactory.newTextTree(config.getText("doclet.enum_values_doc.return"))); |
|
116 |
||
117 |
List<DocTree> tags = new ArrayList<>(); |
|
118 |
tags.add(treeFactory.newReturnTree(descriptions)); |
|
41452 | 119 |
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, tags); |
35426 | 120 |
dcTreesMap.put(e, new DocCommentDuo(null, docTree)); |
121 |
} |
|
122 |
||
123 |
public void setEnumValueOfTree(Configuration config, Element e) { |
|
124 |
||
41452 | 125 |
List<DocTree> fullBody = new ArrayList<>(); |
126 |
fullBody.add(treeFactory.newTextTree(config.getText("doclet.enum_valueof_doc.fullbody"))); |
|
35426 | 127 |
|
128 |
List<DocTree> tags = new ArrayList<>(); |
|
129 |
||
130 |
List<DocTree> paramDescs = new ArrayList<>(); |
|
131 |
paramDescs.add(treeFactory.newTextTree(config.getText("doclet.enum_valueof_doc.param_name"))); |
|
132 |
ExecutableElement ee = (ExecutableElement) e; |
|
133 |
java.util.List<? extends VariableElement> parameters = ee.getParameters(); |
|
134 |
VariableElement param = parameters.get(0); |
|
135 |
IdentifierTree id = treeFactory.newIdentifierTree(elementUtils.getName(param.getSimpleName().toString())); |
|
136 |
tags.add(treeFactory.newParamTree(false, id, paramDescs)); |
|
137 |
||
138 |
List<DocTree> returnDescs = new ArrayList<>(); |
|
139 |
returnDescs.add(treeFactory.newTextTree(config.getText("doclet.enum_valueof_doc.return"))); |
|
140 |
tags.add(treeFactory.newReturnTree(returnDescs)); |
|
141 |
||
142 |
List<DocTree> throwsDescs = new ArrayList<>(); |
|
143 |
throwsDescs.add(treeFactory.newTextTree(config.getText("doclet.enum_valueof_doc.throws_ila"))); |
|
144 |
||
145 |
ReferenceTree ref = treeFactory.newReferenceTree("java.lang.IllegalArgumentException"); |
|
146 |
tags.add(treeFactory.newThrowsTree(ref, throwsDescs)); |
|
147 |
||
148 |
throwsDescs = new ArrayList<>(); |
|
149 |
throwsDescs.add(treeFactory.newTextTree(config.getText("doclet.enum_valueof_doc.throws_npe"))); |
|
150 |
||
151 |
ref = treeFactory.newReferenceTree("java.lang.NullPointerException"); |
|
152 |
tags.add(treeFactory.newThrowsTree(ref, throwsDescs)); |
|
153 |
||
41452 | 154 |
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, tags); |
35426 | 155 |
|
156 |
dcTreesMap.put(e, new DocCommentDuo(null, docTree)); |
|
157 |
} |
|
158 |
||
159 |
/* |
|
160 |
* Returns the TreePath/DocCommentTree duo for synthesized element. |
|
161 |
*/ |
|
162 |
public DocCommentDuo getSyntheticCommentDuo(Element e) { |
|
163 |
return dcTreesMap.get(e); |
|
164 |
} |
|
165 |
||
166 |
/* |
|
167 |
* Returns the TreePath/DocCommentTree duo for html sources. |
|
168 |
*/ |
|
169 |
public DocCommentDuo getHtmlCommentDuo(Element e) { |
|
170 |
FileObject fo = null; |
|
171 |
if (e.getKind().equals(ElementKind.OTHER)) { |
|
172 |
fo = configuration.getOverviewPath(); |
|
173 |
} else if (e.getKind().equals(ElementKind.PACKAGE)) { |
|
174 |
fo = configuration.workArounds.getJavaFileObject((PackageElement)e); |
|
175 |
} |
|
176 |
if (fo == null) { |
|
177 |
return null; |
|
178 |
} |
|
179 |
||
180 |
DocCommentTree dcTree = trees.getDocCommentTree(fo); |
|
181 |
if (dcTree == null) { |
|
182 |
return null; |
|
183 |
} |
|
184 |
DocTreePath treePath = trees.getDocTreePath(fo); |
|
185 |
return new DocCommentDuo(treePath.getTreePath(), dcTree); |
|
186 |
} |
|
187 |
||
41452 | 188 |
public void setDocCommentTree(Element element, List<DocTree> fullBody, |
189 |
List<DocTree> blockTags, Utils utils) { |
|
190 |
DocCommentTree docTree = treeFactory.newDocCommentTree(fullBody, blockTags); |
|
35426 | 191 |
dcTreesMap.put(element, new DocCommentDuo(null, docTree)); |
192 |
// There maybe an entry with the original comments usually null, |
|
193 |
// therefore remove that entry if it exists, and allow a new one |
|
194 |
// to be reestablished. |
|
195 |
utils.removeCommentHelper(element); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* A simplistic container to transport a TreePath, DocCommentTree pair. |
|
200 |
* Here is why we need this: |
|
201 |
* a. not desirable to add javac's pair. |
|
202 |
* b. DocTreePath is not a viable option either, as a null TreePath is required |
|
203 |
* to represent synthetic comments for Enum.values, valuesOf, javafx properties. |
|
204 |
*/ |
|
205 |
public static class DocCommentDuo { |
|
206 |
public final TreePath treePath; |
|
207 |
public final DocCommentTree dcTree; |
|
208 |
||
209 |
public DocCommentDuo(TreePath treePath, DocCommentTree dcTree) { |
|
210 |
this.treePath = treePath; |
|
211 |
this.dcTree = dcTree; |
|
212 |
} |
|
213 |
} |
|
214 |
} |