Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javadoc; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.javadoc.*; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.tools.javac.code.Attribute; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import static com.sun.tools.javac.code.TypeTag.BOOLEAN; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * Represents a value of an annotation type element. |
aoqi@0 | 36 | * |
aoqi@0 | 37 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 38 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 39 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 40 | * deletion without notice.</b> |
aoqi@0 | 41 | * |
aoqi@0 | 42 | * @author Scott Seligman |
aoqi@0 | 43 | * @since 1.5 |
aoqi@0 | 44 | */ |
aoqi@0 | 45 | |
aoqi@0 | 46 | public class AnnotationValueImpl implements AnnotationValue { |
aoqi@0 | 47 | |
aoqi@0 | 48 | private final DocEnv env; |
aoqi@0 | 49 | private final Attribute attr; |
aoqi@0 | 50 | |
aoqi@0 | 51 | |
aoqi@0 | 52 | AnnotationValueImpl(DocEnv env, Attribute attr) { |
aoqi@0 | 53 | this.env = env; |
aoqi@0 | 54 | this.attr = attr; |
aoqi@0 | 55 | } |
aoqi@0 | 56 | |
aoqi@0 | 57 | /** |
aoqi@0 | 58 | * Returns the value. |
aoqi@0 | 59 | * The type of the returned object is one of the following: |
aoqi@0 | 60 | * <ul><li> a wrapper class for a primitive type |
aoqi@0 | 61 | * <li> <code>String</code> |
aoqi@0 | 62 | * <li> <code>Type</code> (representing a class literal) |
aoqi@0 | 63 | * <li> <code>FieldDoc</code> (representing an enum constant) |
aoqi@0 | 64 | * <li> <code>AnnotationDesc</code> |
aoqi@0 | 65 | * <li> <code>AnnotationValue[]</code> |
aoqi@0 | 66 | * </ul> |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | public Object value() { |
aoqi@0 | 69 | ValueVisitor vv = new ValueVisitor(); |
aoqi@0 | 70 | attr.accept(vv); |
aoqi@0 | 71 | return vv.value; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | private class ValueVisitor implements Attribute.Visitor { |
aoqi@0 | 75 | public Object value; |
aoqi@0 | 76 | |
aoqi@0 | 77 | public void visitConstant(Attribute.Constant c) { |
aoqi@0 | 78 | if (c.type.hasTag(BOOLEAN)) { |
aoqi@0 | 79 | // javac represents false and true as integers 0 and 1 |
aoqi@0 | 80 | value = Boolean.valueOf( |
aoqi@0 | 81 | ((Integer)c.value).intValue() != 0); |
aoqi@0 | 82 | } else { |
aoqi@0 | 83 | value = c.value; |
aoqi@0 | 84 | } |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | public void visitClass(Attribute.Class c) { |
aoqi@0 | 88 | value = TypeMaker.getType(env, |
aoqi@0 | 89 | env.types.erasure(c.classType)); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | public void visitEnum(Attribute.Enum e) { |
aoqi@0 | 93 | value = env.getFieldDoc(e.value); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | public void visitCompound(Attribute.Compound c) { |
aoqi@0 | 97 | value = new AnnotationDescImpl(env, c); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | public void visitArray(Attribute.Array a) { |
aoqi@0 | 101 | AnnotationValue vals[] = new AnnotationValue[a.values.length]; |
aoqi@0 | 102 | for (int i = 0; i < vals.length; i++) { |
aoqi@0 | 103 | vals[i] = new AnnotationValueImpl(env, a.values[i]); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | value = vals; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public void visitError(Attribute.Error e) { |
aoqi@0 | 109 | value = "<error>"; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Returns a string representation of the value. |
aoqi@0 | 115 | * |
aoqi@0 | 116 | * @return the text of a Java language annotation value expression |
aoqi@0 | 117 | * whose value is the value of this annotation type element. |
aoqi@0 | 118 | */ |
aoqi@0 | 119 | @Override |
aoqi@0 | 120 | public String toString() { |
aoqi@0 | 121 | ToStringVisitor tv = new ToStringVisitor(); |
aoqi@0 | 122 | attr.accept(tv); |
aoqi@0 | 123 | return tv.toString(); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | private class ToStringVisitor implements Attribute.Visitor { |
aoqi@0 | 127 | private final StringBuilder sb = new StringBuilder(); |
aoqi@0 | 128 | |
aoqi@0 | 129 | @Override |
aoqi@0 | 130 | public String toString() { |
aoqi@0 | 131 | return sb.toString(); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public void visitConstant(Attribute.Constant c) { |
aoqi@0 | 135 | if (c.type.hasTag(BOOLEAN)) { |
aoqi@0 | 136 | // javac represents false and true as integers 0 and 1 |
aoqi@0 | 137 | sb.append(((Integer)c.value).intValue() != 0); |
aoqi@0 | 138 | } else { |
aoqi@0 | 139 | sb.append(FieldDocImpl.constantValueExpression(c.value)); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | public void visitClass(Attribute.Class c) { |
aoqi@0 | 144 | sb.append(c); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | public void visitEnum(Attribute.Enum e) { |
aoqi@0 | 148 | sb.append(e); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | public void visitCompound(Attribute.Compound c) { |
aoqi@0 | 152 | sb.append(new AnnotationDescImpl(env, c)); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public void visitArray(Attribute.Array a) { |
aoqi@0 | 156 | // Omit braces from singleton. |
aoqi@0 | 157 | if (a.values.length != 1) sb.append('{'); |
aoqi@0 | 158 | |
aoqi@0 | 159 | boolean first = true; |
aoqi@0 | 160 | for (Attribute elem : a.values) { |
aoqi@0 | 161 | if (first) { |
aoqi@0 | 162 | first = false; |
aoqi@0 | 163 | } else { |
aoqi@0 | 164 | sb.append(", "); |
aoqi@0 | 165 | } |
aoqi@0 | 166 | elem.accept(this); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | // Omit braces from singleton. |
aoqi@0 | 169 | if (a.values.length != 1) sb.append('}'); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | public void visitError(Attribute.Error e) { |
aoqi@0 | 173 | sb.append("<error>"); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | } |
aoqi@0 | 176 | } |