src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/ValueTaglet.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

     1 /*
     2  * Copyright (c) 2001, 2013, 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  */
    26 package com.sun.tools.doclets.internal.toolkit.taglets;
    28 import java.util.*;
    30 import com.sun.javadoc.*;
    31 import com.sun.tools.doclets.internal.toolkit.Configuration;
    32 import com.sun.tools.doclets.internal.toolkit.Content;
    33 import com.sun.tools.doclets.internal.toolkit.util.*;
    35 /**
    36  * An inline Taglet representing the value tag. This tag should only be used with
    37  * constant fields that have a value.  It is used to access the value of constant
    38  * fields.  This inline tag has an optional field name parameter.  If the name is
    39  * specified, the constant value is retrieved from the specified field.  A link
    40  * is also created to the specified field.  If a name is not specified, the value
    41  * is retrieved for the field that the inline tag appears on.  The name is specifed
    42  * in the following format:  [fully qualified class name]#[constant field name].
    43  *
    44  *  <p><b>This is NOT part of any supported API.
    45  *  If you write code that depends on this, you do so at your own risk.
    46  *  This code and its internal interfaces are subject to change or
    47  *  deletion without notice.</b>
    48  *
    49  * @author Jamie Ho
    50  * @since 1.4
    51  */
    53 public class ValueTaglet extends BaseInlineTaglet {
    55     /**
    56      * Construct a new ValueTaglet.
    57      */
    58     public ValueTaglet() {
    59         name = "value";
    60     }
    62     /**
    63      * Will return false because this inline tag may
    64      * only appear in Fields.
    65      * @return false since this is not a method.
    66      */
    67     public boolean inMethod() {
    68         return true;
    69     }
    71     /**
    72      * Will return false because this inline tag may
    73      * only appear in Fields.
    74      * @return false since this is not a method.
    75      */
    76     public boolean inConstructor() {
    77         return true;
    78     }
    80     /**
    81      * Will return false because this inline tag may
    82      * only appear in Fields.
    83      * @return false since this is not a method.
    84      */
    85     public boolean inOverview() {
    86         return true;
    87     }
    89     /**
    90      * Will return false because this inline tag may
    91      * only appear in Fields.
    92      * @return false since this is not a method.
    93      */
    94     public boolean inPackage() {
    95         return true;
    96     }
    98     /**
    99      * Will return false because this inline tag may
   100      * only appear in Fields.
   101      * @return false since this is not a method.
   102      */
   103     public boolean inType() {
   104         return true;
   105     }
   107     /**
   108      * Given the name of the field, return the corresponding FieldDoc. Return null
   109      * due to invalid use of value tag if the name is null or empty string and if
   110      * the value tag is not used on a field.
   111      *
   112      * @param config the current configuration of the doclet.
   113      * @param tag the value tag.
   114      * @param name the name of the field to search for.  The name should be in
   115      * {@code <qualified class name>#<field name>} format. If the class name is omitted,
   116      * it is assumed that the field is in the current class.
   117      *
   118      * @return the corresponding FieldDoc. If the name is null or empty string,
   119      * return field that the value tag was used in. Return null if the name is null
   120      * or empty string and if the value tag is not used on a field.
   121      */
   122     private FieldDoc getFieldDoc(Configuration config, Tag tag, String name) {
   123         if (name == null || name.length() == 0) {
   124             //Base case: no label.
   125             if (tag.holder() instanceof FieldDoc) {
   126                 return (FieldDoc) tag.holder();
   127             } else {
   128                 // If the value tag does not specify a parameter which is a valid field and
   129                 // it is not used within the comments of a valid field, return null.
   130                  return null;
   131             }
   132         }
   133         StringTokenizer st = new StringTokenizer(name, "#");
   134         String memberName = null;
   135         ClassDoc cd = null;
   136         if (st.countTokens() == 1) {
   137             //Case 2:  @value in same class.
   138             Doc holder = tag.holder();
   139             if (holder instanceof MemberDoc) {
   140                 cd = ((MemberDoc) holder).containingClass();
   141             } else if (holder instanceof ClassDoc) {
   142                 cd = (ClassDoc) holder;
   143             }
   144             memberName = st.nextToken();
   145         } else {
   146             //Case 3: @value in different class.
   147             cd = config.root.classNamed(st.nextToken());
   148             memberName = st.nextToken();
   149         }
   150         if (cd == null) {
   151             return null;
   152         }
   153         FieldDoc[] fields = cd.fields();
   154         for (int i = 0; i < fields.length; i++) {
   155             if (fields[i].name().equals(memberName)) {
   156                 return fields[i];
   157             }
   158         }
   159         return null;
   160     }
   162     /**
   163      * {@inheritDoc}
   164      */
   165     public Content getTagletOutput(Tag tag, TagletWriter writer) {
   166         FieldDoc field = getFieldDoc(
   167             writer.configuration(), tag, tag.text());
   168         if (field == null) {
   169             if (tag.text().isEmpty()) {
   170                 //Invalid use of @value
   171                 writer.getMsgRetriever().warning(tag.holder().position(),
   172                         "doclet.value_tag_invalid_use");
   173             } else {
   174                 //Reference is unknown.
   175                 writer.getMsgRetriever().warning(tag.holder().position(),
   176                         "doclet.value_tag_invalid_reference", tag.text());
   177             }
   178         } else if (field.constantValue() != null) {
   179             return writer.valueTagOutput(field,
   180                 field.constantValueExpression(),
   181                 ! field.equals(tag.holder()));
   182         } else {
   183             //Referenced field is not a constant.
   184             writer.getMsgRetriever().warning(tag.holder().position(),
   185                 "doclet.value_tag_invalid_constant", field.name());
   186         }
   187         return writer.getOutputInstance();
   188     }
   189 }

mercurial