ValueTaglet.java revision 3233:b5d08bc0d224
1/*
2 * Copyright (c) 2001, 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
26package jdk.javadoc.internal.doclets.toolkit.taglets;
27
28
29import javax.lang.model.element.Element;
30import javax.lang.model.element.TypeElement;
31import javax.lang.model.element.VariableElement;
32import javax.lang.model.util.Elements;
33
34import com.sun.source.doctree.DocTree;
35import jdk.javadoc.internal.doclets.toolkit.Configuration;
36import jdk.javadoc.internal.doclets.toolkit.Content;
37import jdk.javadoc.internal.doclets.toolkit.util.CommentHelper;
38import jdk.javadoc.internal.doclets.toolkit.util.Utils;
39
40import static com.sun.source.doctree.DocTree.Kind.*;
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 *
51 *  <p><b>This is NOT part of any supported API.
52 *  If you write code that depends on this, you do so at your own risk.
53 *  This code and its internal interfaces are subject to change or
54 *  deletion without notice.</b>
55 *
56 * @author Jamie Ho
57 */
58
59public class ValueTaglet extends BaseInlineTaglet {
60
61    /**
62     * Construct a new ValueTaglet.
63     */
64    public ValueTaglet() {
65        name = VALUE.tagName;
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    /**
114     * Given the name of the field, return the corresponding VariableElement. Return null
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.
117     *
118     * @param holder the element holding the tag
119     * @param config the current configuration of the doclet.
120     * @param tag the value tag.
121     *
122     * @return the corresponding VariableElement. If the name is null or empty string,
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.
125     */
126    private VariableElement getVariableElement(Element holder, Configuration config, DocTree tag) {
127        Utils utils = config.utils;
128        CommentHelper ch = utils.getCommentHelper(holder);
129        String signature = ch.getReferencedSignature(tag);
130
131        if (signature == null) { // no reference
132            //Base case: no label.
133            if (utils.isVariableElement(holder)) {
134                return (VariableElement)(holder);
135            } else {
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;
139            }
140        }
141
142        String[] sigValues = signature.split("#");
143        String memberName = null;
144        TypeElement te = null;
145        if (sigValues.length == 1) {
146            //Case 2:  @value in same class.
147            if (utils.isExecutableElement(holder) || utils.isVariableElement(holder)) {
148                te = utils.getEnclosingTypeElement(holder);
149            } else if (utils.isTypeElement(holder)) {
150                te = utils.getTopMostContainingTypeElement(holder);
151            }
152            memberName = sigValues[0];
153        } else {
154            //Case 3: @value in different class.
155            Elements elements = config.root.getElementUtils();
156            te = elements.getTypeElement(sigValues[0]);
157            memberName = sigValues[1];
158        }
159        if (te == null) {
160            return null;
161        }
162        for (Element field : utils.getFields(te)) {
163            if (utils.getSimpleName(field).equals(memberName)) {
164                return (VariableElement)field;
165            }
166        }
167        return null;
168    }
169
170    /**
171     * {@inheritDoc}
172     */
173    public Content getTagletOutput(Element holder, DocTree tag, TagletWriter writer) {
174        Utils utils = writer.configuration().utils;
175        VariableElement field = getVariableElement(holder, writer.configuration(), tag);
176        if (field == null) {
177            if (tag.toString().isEmpty()) {
178                //Invalid use of @value
179                writer.getMsgRetriever().warning(holder,
180                        "doclet.value_tag_invalid_use");
181            } else {
182                //Reference is unknown.
183                writer.getMsgRetriever().warning(holder,
184                        "doclet.value_tag_invalid_reference", tag.toString());
185            }
186        } else if (field.getConstantValue() != null) {
187            return writer.valueTagOutput(field,
188                utils.constantValueExpresion(field),
189                // TODO: investigate and cleanup
190                // in the j.l.m world, equals will not be accurate
191                // !field.equals(tag.holder())
192                !utils.elementsEqual(field, holder)
193            );
194        } else {
195            //Referenced field is not a constant.
196            writer.getMsgRetriever().warning(holder,
197                "doclet.value_tag_invalid_constant", utils.getSimpleName(field));
198        }
199        return writer.getOutputInstance();
200    }
201}
202