author | ksrini |
Mon, 05 Jun 2017 13:45:34 -0700 | |
changeset 45417 | f7479ee8de69 |
parent 40303 | 96a1226aca18 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
45417 | 2 |
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
26 |
package jdk.javadoc.internal.doclets.toolkit.taglets; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
27 |
|
10 | 28 |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
29 |
import javax.lang.model.element.Element; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
30 |
import javax.lang.model.element.TypeElement; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
31 |
import javax.lang.model.element.VariableElement; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
32 |
import javax.lang.model.util.Elements; |
14258 | 33 |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
34 |
import com.sun.source.doctree.DocTree; |
45417 | 35 |
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
36 |
import jdk.javadoc.internal.doclets.toolkit.Content; |
40303 | 37 |
import jdk.javadoc.internal.doclets.toolkit.Messages; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
38 |
import jdk.javadoc.internal.doclets.toolkit.util.CommentHelper; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
39 |
import jdk.javadoc.internal.doclets.toolkit.util.Utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
40 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
41 |
import static com.sun.source.doctree.DocTree.Kind.*; |
10 | 42 |
|
43 |
/** |
|
44 |
* An inline Taglet representing the value tag. This tag should only be used with |
|
45 |
* constant fields that have a value. It is used to access the value of constant |
|
46 |
* fields. This inline tag has an optional field name parameter. If the name is |
|
47 |
* specified, the constant value is retrieved from the specified field. A link |
|
48 |
* is also created to the specified field. If a name is not specified, the value |
|
49 |
* is retrieved for the field that the inline tag appears on. The name is specifed |
|
50 |
* in the following format: [fully qualified class name]#[constant field name]. |
|
51 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
52 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
53 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
54 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
55 |
* deletion without notice.</b> |
10 | 56 |
* |
57 |
* @author Jamie Ho |
|
58 |
*/ |
|
59 |
||
60 |
public class ValueTaglet extends BaseInlineTaglet { |
|
61 |
||
62 |
/** |
|
63 |
* Construct a new ValueTaglet. |
|
64 |
*/ |
|
65 |
public ValueTaglet() { |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
66 |
name = VALUE.tagName; |
10 | 67 |
} |
68 |
||
69 |
/** |
|
70 |
* Will return false because this inline tag may |
|
71 |
* only appear in Fields. |
|
72 |
* @return false since this is not a method. |
|
73 |
*/ |
|
74 |
public boolean inMethod() { |
|
75 |
return true; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Will return false because this inline tag may |
|
80 |
* only appear in Fields. |
|
81 |
* @return false since this is not a method. |
|
82 |
*/ |
|
83 |
public boolean inConstructor() { |
|
84 |
return true; |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Will return false because this inline tag may |
|
89 |
* only appear in Fields. |
|
90 |
* @return false since this is not a method. |
|
91 |
*/ |
|
92 |
public boolean inOverview() { |
|
93 |
return true; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Will return false because this inline tag may |
|
98 |
* only appear in Fields. |
|
38911
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
99 |
* @return false since this is not a field. |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
100 |
*/ |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
101 |
public boolean inModule() { |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
102 |
return false; |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
103 |
} |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
104 |
|
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
105 |
/** |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
106 |
* Will return false because this inline tag may |
48a00b5ee366
8156077: Support javadoc tags in module documentation
bpatel
parents:
35426
diff
changeset
|
107 |
* only appear in Fields. |
10 | 108 |
* @return false since this is not a method. |
109 |
*/ |
|
110 |
public boolean inPackage() { |
|
111 |
return true; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Will return false because this inline tag may |
|
116 |
* only appear in Fields. |
|
117 |
* @return false since this is not a method. |
|
118 |
*/ |
|
119 |
public boolean inType() { |
|
120 |
return true; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
124 |
* Given the name of the field, return the corresponding VariableElement. Return null |
20264 | 125 |
* due to invalid use of value tag if the name is null or empty string and if |
126 |
* the value tag is not used on a field. |
|
10 | 127 |
* |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
128 |
* @param holder the element holding the tag |
10 | 129 |
* @param config the current configuration of the doclet. |
130 |
* @param tag the value tag. |
|
131 |
* |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
132 |
* @return the corresponding VariableElement. If the name is null or empty string, |
20264 | 133 |
* return field that the value tag was used in. Return null if the name is null |
134 |
* or empty string and if the value tag is not used on a field. |
|
10 | 135 |
*/ |
45417 | 136 |
private VariableElement getVariableElement(Element holder, BaseConfiguration config, DocTree tag) { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
137 |
Utils utils = config.utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
138 |
CommentHelper ch = utils.getCommentHelper(holder); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
139 |
String signature = ch.getReferencedSignature(tag); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
140 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
141 |
if (signature == null) { // no reference |
10 | 142 |
//Base case: no label. |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
143 |
if (utils.isVariableElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
144 |
return (VariableElement)(holder); |
10 | 145 |
} else { |
20264 | 146 |
// If the value tag does not specify a parameter which is a valid field and |
147 |
// it is not used within the comments of a valid field, return null. |
|
148 |
return null; |
|
10 | 149 |
} |
150 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
151 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
152 |
String[] sigValues = signature.split("#"); |
10 | 153 |
String memberName = null; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
154 |
TypeElement te = null; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
155 |
if (sigValues.length == 1) { |
10 | 156 |
//Case 2: @value in same class. |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
157 |
if (utils.isExecutableElement(holder) || utils.isVariableElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
158 |
te = utils.getEnclosingTypeElement(holder); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
159 |
} else if (utils.isTypeElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
160 |
te = utils.getTopMostContainingTypeElement(holder); |
10 | 161 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
162 |
memberName = sigValues[0]; |
10 | 163 |
} else { |
164 |
//Case 3: @value in different class. |
|
39364
bd6d4a7936b4
8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents:
38911
diff
changeset
|
165 |
Elements elements = config.docEnv.getElementUtils(); |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
166 |
te = elements.getTypeElement(sigValues[0]); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
167 |
memberName = sigValues[1]; |
10 | 168 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
169 |
if (te == null) { |
10 | 170 |
return null; |
171 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
172 |
for (Element field : utils.getFields(te)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
173 |
if (utils.getSimpleName(field).equals(memberName)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
174 |
return (VariableElement)field; |
10 | 175 |
} |
176 |
} |
|
177 |
return null; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* {@inheritDoc} |
|
182 |
*/ |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
183 |
public Content getTagletOutput(Element holder, DocTree tag, TagletWriter writer) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
184 |
Utils utils = writer.configuration().utils; |
40303 | 185 |
Messages messages = writer.configuration().getMessages(); |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
186 |
VariableElement field = getVariableElement(holder, writer.configuration(), tag); |
10 | 187 |
if (field == null) { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
188 |
if (tag.toString().isEmpty()) { |
20264 | 189 |
//Invalid use of @value |
40303 | 190 |
messages.warning(holder, |
20264 | 191 |
"doclet.value_tag_invalid_use"); |
192 |
} else { |
|
193 |
//Reference is unknown. |
|
40303 | 194 |
messages.warning(holder, |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
195 |
"doclet.value_tag_invalid_reference", tag.toString()); |
20264 | 196 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
197 |
} else if (field.getConstantValue() != null) { |
10 | 198 |
return writer.valueTagOutput(field, |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
199 |
utils.constantValueExpresion(field), |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
200 |
// TODO: investigate and cleanup |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
201 |
// in the j.l.m world, equals will not be accurate |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
202 |
// !field.equals(tag.holder()) |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
203 |
!utils.elementsEqual(field, holder) |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
204 |
); |
10 | 205 |
} else { |
206 |
//Referenced field is not a constant. |
|
40303 | 207 |
messages.warning(holder, |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
208 |
"doclet.value_tag_invalid_constant", utils.getSimpleName(field)); |
10 | 209 |
} |
210 |
return writer.getOutputInstance(); |
|
211 |
} |
|
212 |
} |