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

Fri, 03 May 2013 17:44:38 -0700

author
jjg
date
Fri, 03 May 2013 17:44:38 -0700
changeset 1724
d918b63a5509
parent 1359
25e14ad23cef
child 1746
bd51ca92c013
permissions
-rw-r--r--

8008768: Using {@inheritDoc} in simple tag defined via -tag fails
Reviewed-by: jjg, mduigou
Contributed-by: jonathan.gibbons@oracle.com, mike.duigou@oracle.com

duke@1 1 /*
jjg@1357 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.taglets;
duke@1 27
jjg@1357 28 import java.util.*;
jjg@1357 29
duke@1 30 import com.sun.javadoc.*;
duke@1 31 import com.sun.tools.doclets.internal.toolkit.Configuration;
duke@1 32 import com.sun.tools.doclets.internal.toolkit.util.*;
duke@1 33
duke@1 34 /**
duke@1 35 * An inline Taglet representing the value tag. This tag should only be used with
duke@1 36 * constant fields that have a value. It is used to access the value of constant
duke@1 37 * fields. This inline tag has an optional field name parameter. If the name is
duke@1 38 * specified, the constant value is retrieved from the specified field. A link
duke@1 39 * is also created to the specified field. If a name is not specified, the value
duke@1 40 * is retrieved for the field that the inline tag appears on. The name is specifed
duke@1 41 * in the following format: [fully qualified class name]#[constant field name].
duke@1 42 *
jjg@1359 43 * <p><b>This is NOT part of any supported API.
jjg@1359 44 * If you write code that depends on this, you do so at your own risk.
jjg@1359 45 * This code and its internal interfaces are subject to change or
jjg@1359 46 * deletion without notice.</b>
duke@1 47 *
duke@1 48 * @author Jamie Ho
duke@1 49 * @since 1.4
duke@1 50 */
duke@1 51
duke@1 52 public class ValueTaglet extends BaseInlineTaglet {
duke@1 53
duke@1 54 /**
duke@1 55 * Construct a new ValueTaglet.
duke@1 56 */
duke@1 57 public ValueTaglet() {
duke@1 58 name = "value";
duke@1 59 }
duke@1 60
duke@1 61 /**
duke@1 62 * Will return false because this inline tag may
duke@1 63 * only appear in Fields.
duke@1 64 * @return false since this is not a method.
duke@1 65 */
duke@1 66 public boolean inMethod() {
duke@1 67 return true;
duke@1 68 }
duke@1 69
duke@1 70 /**
duke@1 71 * Will return false because this inline tag may
duke@1 72 * only appear in Fields.
duke@1 73 * @return false since this is not a method.
duke@1 74 */
duke@1 75 public boolean inConstructor() {
duke@1 76 return true;
duke@1 77 }
duke@1 78
duke@1 79 /**
duke@1 80 * Will return false because this inline tag may
duke@1 81 * only appear in Fields.
duke@1 82 * @return false since this is not a method.
duke@1 83 */
duke@1 84 public boolean inOverview() {
duke@1 85 return true;
duke@1 86 }
duke@1 87
duke@1 88 /**
duke@1 89 * Will return false because this inline tag may
duke@1 90 * only appear in Fields.
duke@1 91 * @return false since this is not a method.
duke@1 92 */
duke@1 93 public boolean inPackage() {
duke@1 94 return true;
duke@1 95 }
duke@1 96
duke@1 97 /**
duke@1 98 * Will return false because this inline tag may
duke@1 99 * only appear in Fields.
duke@1 100 * @return false since this is not a method.
duke@1 101 */
duke@1 102 public boolean inType() {
duke@1 103 return true;
duke@1 104 }
duke@1 105
duke@1 106 /**
duke@1 107 * Given the name of the field, return the corresponding FieldDoc.
duke@1 108 *
duke@1 109 * @param config the current configuration of the doclet.
duke@1 110 * @param tag the value tag.
duke@1 111 * @param name the name of the field to search for. The name should be in
jjg@1358 112 * {@code <qualified class name>#<field name>} format. If the class name is omitted,
duke@1 113 * it is assumed that the field is in the current class.
duke@1 114 *
duke@1 115 * @return the corresponding FieldDoc. If the name is null or empty string,
duke@1 116 * return field that the value tag was used in.
duke@1 117 *
duke@1 118 * @throws DocletAbortException if the value tag does not specify a name to
duke@1 119 * a value field and it is not used within the comments of a valid field.
duke@1 120 */
duke@1 121 private FieldDoc getFieldDoc(Configuration config, Tag tag, String name) {
duke@1 122 if (name == null || name.length() == 0) {
duke@1 123 //Base case: no label.
duke@1 124 if (tag.holder() instanceof FieldDoc) {
duke@1 125 return (FieldDoc) tag.holder();
duke@1 126 } else {
duke@1 127 //This should never ever happen.
duke@1 128 throw new DocletAbortException();
duke@1 129 }
duke@1 130 }
duke@1 131 StringTokenizer st = new StringTokenizer(name, "#");
duke@1 132 String memberName = null;
duke@1 133 ClassDoc cd = null;
duke@1 134 if (st.countTokens() == 1) {
duke@1 135 //Case 2: @value in same class.
duke@1 136 Doc holder = tag.holder();
duke@1 137 if (holder instanceof MemberDoc) {
duke@1 138 cd = ((MemberDoc) holder).containingClass();
duke@1 139 } else if (holder instanceof ClassDoc) {
duke@1 140 cd = (ClassDoc) holder;
duke@1 141 }
duke@1 142 memberName = st.nextToken();
duke@1 143 } else {
duke@1 144 //Case 3: @value in different class.
duke@1 145 cd = config.root.classNamed(st.nextToken());
duke@1 146 memberName = st.nextToken();
duke@1 147 }
duke@1 148 if (cd == null) {
duke@1 149 return null;
duke@1 150 }
duke@1 151 FieldDoc[] fields = cd.fields();
duke@1 152 for (int i = 0; i < fields.length; i++) {
duke@1 153 if (fields[i].name().equals(memberName)) {
duke@1 154 return fields[i];
duke@1 155 }
duke@1 156 }
duke@1 157 return null;
duke@1 158 }
duke@1 159
duke@1 160 /**
duke@1 161 * {@inheritDoc}
duke@1 162 */
duke@1 163 public TagletOutput getTagletOutput(Tag tag, TagletWriter writer) {
duke@1 164 FieldDoc field = getFieldDoc(
duke@1 165 writer.configuration(), tag, tag.text());
duke@1 166 if (field == null) {
duke@1 167 //Reference is unknown.
duke@1 168 writer.getMsgRetriever().warning(tag.holder().position(),
duke@1 169 "doclet.value_tag_invalid_reference", tag.text());
duke@1 170 } else if (field.constantValue() != null) {
duke@1 171 return writer.valueTagOutput(field,
duke@1 172 Util.escapeHtmlChars(field.constantValueExpression()),
duke@1 173 ! field.equals(tag.holder()));
duke@1 174 } else {
duke@1 175 //Referenced field is not a constant.
duke@1 176 writer.getMsgRetriever().warning(tag.holder().position(),
duke@1 177 "doclet.value_tag_invalid_constant", field.name());
duke@1 178 }
duke@1 179 return writer.getOutputInstance();
duke@1 180 }
duke@1 181 }

mercurial