langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java
author chegar
Sun, 17 Aug 2014 15:52:32 +0100
changeset 25874 83c19f00452c
parent 22159 langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java@682da512ec17
child 38617 d93a7f64e231
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
17569
ef80738645e2 8012178: Cleanup use of Util.escapeHtmlChars
jjg
parents: 14260
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.taglets;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.doclets.internal.toolkit.Configuration;
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17569
diff changeset
    32
import com.sun.tools.doclets.internal.toolkit.Content;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.doclets.internal.toolkit.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * An inline Taglet representing the value tag. This tag should only be used with
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * constant fields that have a value.  It is used to access the value of constant
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * fields.  This inline tag has an optional field name parameter.  If the name is
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * specified, the constant value is retrieved from the specified field.  A link
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * is also created to the specified field.  If a name is not specified, the value
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * is retrieved for the field that the inline tag appears on.  The name is specifed
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * in the following format:  [fully qualified class name]#[constant field name].
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    44
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    45
 *  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
    46
 *  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
    47
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
public class ValueTaglet extends BaseInlineTaglet {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * Construct a new ValueTaglet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public ValueTaglet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        name = "value";
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public boolean inMethod() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public boolean inConstructor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public boolean inOverview() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    public boolean inPackage() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Will return false because this inline tag may
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * only appear in Fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * @return false since this is not a method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    public boolean inType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
20264
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   108
     * Given the name of the field, return the corresponding FieldDoc. Return null
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   109
     * due to invalid use of value tag if the name is null or empty string and if
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   110
     * the value tag is not used on a field.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * @param config the current configuration of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * @param tag the value tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * @param name the name of the field to search for.  The name should be in
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   115
     * {@code <qualified class name>#<field name>} format. If the class name is omitted,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * it is assumed that the field is in the current class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @return the corresponding FieldDoc. If the name is null or empty string,
20264
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   119
     * return field that the value tag was used in. Return null if the name is null
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   120
     * or empty string and if the value tag is not used on a field.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    private FieldDoc getFieldDoc(Configuration config, Tag tag, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (name == null || name.length() == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            //Base case: no label.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (tag.holder() instanceof FieldDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                return (FieldDoc) tag.holder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            } else {
20264
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   128
                // If the value tag does not specify a parameter which is a valid field and
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   129
                // it is not used within the comments of a valid field, return null.
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   130
                 return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        StringTokenizer st = new StringTokenizer(name, "#");
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        String memberName = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        ClassDoc cd = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        if (st.countTokens() == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            //Case 2:  @value in same class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            Doc holder = tag.holder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            if (holder instanceof MemberDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                cd = ((MemberDoc) holder).containingClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            } else if (holder instanceof ClassDoc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                cd = (ClassDoc) holder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            memberName = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            //Case 3: @value in different class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            cd = config.root.classNamed(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            memberName = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        if (cd == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        }
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 20264
diff changeset
   153
        for (FieldDoc field : cd.fields()) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 20264
diff changeset
   154
            if (field.name().equals(memberName)) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 20264
diff changeset
   155
                return field;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
17574
044c7e1e4d53 8012308: Remove TagletOutput in favor of direct use of Content
jjg
parents: 17569
diff changeset
   164
    public Content getTagletOutput(Tag tag, TagletWriter writer) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        FieldDoc field = getFieldDoc(
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            writer.configuration(), tag, tag.text());
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        if (field == null) {
20264
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   168
            if (tag.text().isEmpty()) {
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   169
                //Invalid use of @value
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   170
                writer.getMsgRetriever().warning(tag.holder().position(),
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   171
                        "doclet.value_tag_invalid_use");
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   172
            } else {
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   173
                //Reference is unknown.
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   174
                writer.getMsgRetriever().warning(tag.holder().position(),
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   175
                        "doclet.value_tag_invalid_reference", tag.text());
f7527e6828bb 8004825: javadoc crash DocletAbortException
bpatel
parents: 19667
diff changeset
   176
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        } else if (field.constantValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            return writer.valueTagOutput(field,
17569
ef80738645e2 8012178: Cleanup use of Util.escapeHtmlChars
jjg
parents: 14260
diff changeset
   179
                field.constantValueExpression(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                ! field.equals(tag.holder()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            //Referenced field is not a constant.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            writer.getMsgRetriever().warning(tag.holder().position(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                "doclet.value_tag_invalid_constant", field.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        return writer.getOutputInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
}