author | ksrini |
Sat, 28 Nov 2015 18:52:17 -0800 | |
changeset 35426 | 374342e56a56 |
parent 25874 | langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java@83c19f00452c |
child 38911 | 48a00b5ee366 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
2 |
* Copyright (c) 2001, 2016, 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; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
35 |
import jdk.javadoc.internal.doclets.toolkit.Configuration; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
36 |
import jdk.javadoc.internal.doclets.toolkit.Content; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
37 |
import jdk.javadoc.internal.doclets.toolkit.util.CommentHelper; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
38 |
import jdk.javadoc.internal.doclets.toolkit.util.Utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
39 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
40 |
import static com.sun.source.doctree.DocTree.Kind.*; |
10 | 41 |
|
42 |
/** |
|
43 |
* An inline Taglet representing the value tag. This tag should only be used with |
|
44 |
* constant fields that have a value. It is used to access the value of constant |
|
45 |
* fields. This inline tag has an optional field name parameter. If the name is |
|
46 |
* specified, the constant value is retrieved from the specified field. A link |
|
47 |
* is also created to the specified field. If a name is not specified, the value |
|
48 |
* is retrieved for the field that the inline tag appears on. The name is specifed |
|
49 |
* in the following format: [fully qualified class name]#[constant field name]. |
|
50 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
51 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
52 |
* 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
|
53 |
* 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
|
54 |
* deletion without notice.</b> |
10 | 55 |
* |
56 |
* @author Jamie Ho |
|
57 |
*/ |
|
58 |
||
59 |
public class ValueTaglet extends BaseInlineTaglet { |
|
60 |
||
61 |
/** |
|
62 |
* Construct a new ValueTaglet. |
|
63 |
*/ |
|
64 |
public ValueTaglet() { |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
65 |
name = VALUE.tagName; |
10 | 66 |
} |
67 |
||
68 |
/** |
|
69 |
* Will return false because this inline tag may |
|
70 |
* only appear in Fields. |
|
71 |
* @return false since this is not a method. |
|
72 |
*/ |
|
73 |
public boolean inMethod() { |
|
74 |
return true; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Will return false because this inline tag may |
|
79 |
* only appear in Fields. |
|
80 |
* @return false since this is not a method. |
|
81 |
*/ |
|
82 |
public boolean inConstructor() { |
|
83 |
return true; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Will return false because this inline tag may |
|
88 |
* only appear in Fields. |
|
89 |
* @return false since this is not a method. |
|
90 |
*/ |
|
91 |
public boolean inOverview() { |
|
92 |
return true; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Will return false because this inline tag may |
|
97 |
* only appear in Fields. |
|
98 |
* @return false since this is not a method. |
|
99 |
*/ |
|
100 |
public boolean inPackage() { |
|
101 |
return true; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Will return false because this inline tag may |
|
106 |
* only appear in Fields. |
|
107 |
* @return false since this is not a method. |
|
108 |
*/ |
|
109 |
public boolean inType() { |
|
110 |
return true; |
|
111 |
} |
|
112 |
||
113 |
/** |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
114 |
* Given the name of the field, return the corresponding VariableElement. Return null |
20264 | 115 |
* due to invalid use of value tag if the name is null or empty string and if |
116 |
* the value tag is not used on a field. |
|
10 | 117 |
* |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
118 |
* @param holder the element holding the tag |
10 | 119 |
* @param config the current configuration of the doclet. |
120 |
* @param tag the value tag. |
|
121 |
* |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
122 |
* @return the corresponding VariableElement. If the name is null or empty string, |
20264 | 123 |
* return field that the value tag was used in. Return null if the name is null |
124 |
* or empty string and if the value tag is not used on a field. |
|
10 | 125 |
*/ |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
126 |
private VariableElement getVariableElement(Element holder, Configuration config, DocTree tag) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
127 |
Utils utils = config.utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
128 |
CommentHelper ch = utils.getCommentHelper(holder); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
129 |
String signature = ch.getReferencedSignature(tag); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
130 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
131 |
if (signature == null) { // no reference |
10 | 132 |
//Base case: no label. |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
133 |
if (utils.isVariableElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
134 |
return (VariableElement)(holder); |
10 | 135 |
} else { |
20264 | 136 |
// If the value tag does not specify a parameter which is a valid field and |
137 |
// it is not used within the comments of a valid field, return null. |
|
138 |
return null; |
|
10 | 139 |
} |
140 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
141 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
142 |
String[] sigValues = signature.split("#"); |
10 | 143 |
String memberName = null; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
144 |
TypeElement te = null; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
145 |
if (sigValues.length == 1) { |
10 | 146 |
//Case 2: @value in same class. |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
147 |
if (utils.isExecutableElement(holder) || utils.isVariableElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
148 |
te = utils.getEnclosingTypeElement(holder); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
149 |
} else if (utils.isTypeElement(holder)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
150 |
te = utils.getTopMostContainingTypeElement(holder); |
10 | 151 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
152 |
memberName = sigValues[0]; |
10 | 153 |
} else { |
154 |
//Case 3: @value in different class. |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
155 |
Elements elements = config.root.getElementUtils(); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
156 |
te = elements.getTypeElement(sigValues[0]); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
157 |
memberName = sigValues[1]; |
10 | 158 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
159 |
if (te == null) { |
10 | 160 |
return null; |
161 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
162 |
for (Element field : utils.getFields(te)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
163 |
if (utils.getSimpleName(field).equals(memberName)) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
164 |
return (VariableElement)field; |
10 | 165 |
} |
166 |
} |
|
167 |
return null; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* {@inheritDoc} |
|
172 |
*/ |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
173 |
public Content getTagletOutput(Element holder, DocTree tag, TagletWriter writer) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
174 |
Utils utils = writer.configuration().utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
175 |
VariableElement field = getVariableElement(holder, writer.configuration(), tag); |
10 | 176 |
if (field == null) { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
177 |
if (tag.toString().isEmpty()) { |
20264 | 178 |
//Invalid use of @value |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
179 |
writer.getMsgRetriever().warning(holder, |
20264 | 180 |
"doclet.value_tag_invalid_use"); |
181 |
} else { |
|
182 |
//Reference is unknown. |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
183 |
writer.getMsgRetriever().warning(holder, |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
184 |
"doclet.value_tag_invalid_reference", tag.toString()); |
20264 | 185 |
} |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
186 |
} else if (field.getConstantValue() != null) { |
10 | 187 |
return writer.valueTagOutput(field, |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
188 |
utils.constantValueExpresion(field), |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
189 |
// TODO: investigate and cleanup |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
190 |
// in the j.l.m world, equals will not be accurate |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
191 |
// !field.equals(tag.holder()) |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
192 |
!utils.elementsEqual(field, holder) |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
193 |
); |
10 | 194 |
} else { |
195 |
//Referenced field is not a constant. |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
196 |
writer.getMsgRetriever().warning(holder, |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
25874
diff
changeset
|
197 |
"doclet.value_tag_invalid_constant", utils.getSimpleName(field)); |
10 | 198 |
} |
199 |
return writer.getOutputInstance(); |
|
200 |
} |
|
201 |
} |