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

Tue, 18 Jun 2013 20:56:04 -0700

author
mfang
date
Tue, 18 Jun 2013 20:56:04 -0700
changeset 1841
792c40d5185a
parent 1751
ca8808c88f94
child 1985
0e6577980181
permissions
-rw-r--r--

8015657: jdk8 l10n resource file translation update 3
Reviewed-by: yhuang

     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.
   109      *
   110      * @param config the current configuration of the doclet.
   111      * @param tag the value tag.
   112      * @param name the name of the field to search for.  The name should be in
   113      * {@code <qualified class name>#<field name>} format. If the class name is omitted,
   114      * it is assumed that the field is in the current class.
   115      *
   116      * @return the corresponding FieldDoc. If the name is null or empty string,
   117      * return field that the value tag was used in.
   118      *
   119      * @throws DocletAbortException if the value tag does not specify a name to
   120      * a value field and it is not used within the comments of a valid 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                 //This should never ever happen.
   129                 throw new DocletAbortException();
   130             }
   131         }
   132         StringTokenizer st = new StringTokenizer(name, "#");
   133         String memberName = null;
   134         ClassDoc cd = null;
   135         if (st.countTokens() == 1) {
   136             //Case 2:  @value in same class.
   137             Doc holder = tag.holder();
   138             if (holder instanceof MemberDoc) {
   139                 cd = ((MemberDoc) holder).containingClass();
   140             } else if (holder instanceof ClassDoc) {
   141                 cd = (ClassDoc) holder;
   142             }
   143             memberName = st.nextToken();
   144         } else {
   145             //Case 3: @value in different class.
   146             cd = config.root.classNamed(st.nextToken());
   147             memberName = st.nextToken();
   148         }
   149         if (cd == null) {
   150             return null;
   151         }
   152         FieldDoc[] fields = cd.fields();
   153         for (int i = 0; i < fields.length; i++) {
   154             if (fields[i].name().equals(memberName)) {
   155                 return fields[i];
   156             }
   157         }
   158         return null;
   159     }
   161     /**
   162      * {@inheritDoc}
   163      */
   164     public Content getTagletOutput(Tag tag, TagletWriter writer) {
   165         FieldDoc field = getFieldDoc(
   166             writer.configuration(), tag, tag.text());
   167         if (field == null) {
   168             //Reference is unknown.
   169             writer.getMsgRetriever().warning(tag.holder().position(),
   170                 "doclet.value_tag_invalid_reference", tag.text());
   171         } else if (field.constantValue() != null) {
   172             return writer.valueTagOutput(field,
   173                 field.constantValueExpression(),
   174                 ! field.equals(tag.holder()));
   175         } else {
   176             //Referenced field is not a constant.
   177             writer.getMsgRetriever().warning(tag.holder().position(),
   178                 "doclet.value_tag_invalid_constant", field.name());
   179         }
   180         return writer.getOutputInstance();
   181     }
   182 }

mercurial