Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/AnnotationValueImpl.java
38899 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import com.sun.javadoc.*;2829import com.sun.tools.javac.code.Attribute;3031import static com.sun.tools.javac.code.TypeTag.BOOLEAN;3233/**34* Represents a value of an annotation type element.35*36* <p><b>This is NOT part of any supported API.37* If you write code that depends on this, you do so at your own risk.38* This code and its internal interfaces are subject to change or39* deletion without notice.</b>40*41* @author Scott Seligman42* @since 1.543*/4445public class AnnotationValueImpl implements AnnotationValue {4647private final DocEnv env;48private final Attribute attr;495051AnnotationValueImpl(DocEnv env, Attribute attr) {52this.env = env;53this.attr = attr;54}5556/**57* Returns the value.58* The type of the returned object is one of the following:59* <ul><li> a wrapper class for a primitive type60* <li> <code>String</code>61* <li> <code>Type</code> (representing a class literal)62* <li> <code>FieldDoc</code> (representing an enum constant)63* <li> <code>AnnotationDesc</code>64* <li> <code>AnnotationValue[]</code>65* </ul>66*/67public Object value() {68ValueVisitor vv = new ValueVisitor();69attr.accept(vv);70return vv.value;71}7273private class ValueVisitor implements Attribute.Visitor {74public Object value;7576public void visitConstant(Attribute.Constant c) {77if (c.type.hasTag(BOOLEAN)) {78// javac represents false and true as integers 0 and 179value = Boolean.valueOf(80((Integer)c.value).intValue() != 0);81} else {82value = c.value;83}84}8586public void visitClass(Attribute.Class c) {87value = TypeMaker.getType(env,88env.types.erasure(c.classType));89}9091public void visitEnum(Attribute.Enum e) {92value = env.getFieldDoc(e.value);93}9495public void visitCompound(Attribute.Compound c) {96value = new AnnotationDescImpl(env, c);97}9899public void visitArray(Attribute.Array a) {100AnnotationValue vals[] = new AnnotationValue[a.values.length];101for (int i = 0; i < vals.length; i++) {102vals[i] = new AnnotationValueImpl(env, a.values[i]);103}104value = vals;105}106107public void visitError(Attribute.Error e) {108value = "<error>";109}110}111112/**113* Returns a string representation of the value.114*115* @return the text of a Java language annotation value expression116* whose value is the value of this annotation type element.117*/118@Override119public String toString() {120ToStringVisitor tv = new ToStringVisitor();121attr.accept(tv);122return tv.toString();123}124125private class ToStringVisitor implements Attribute.Visitor {126private final StringBuilder sb = new StringBuilder();127128@Override129public String toString() {130return sb.toString();131}132133public void visitConstant(Attribute.Constant c) {134if (c.type.hasTag(BOOLEAN)) {135// javac represents false and true as integers 0 and 1136sb.append(((Integer)c.value).intValue() != 0);137} else {138sb.append(FieldDocImpl.constantValueExpression(c.value));139}140}141142public void visitClass(Attribute.Class c) {143sb.append(c);144}145146public void visitEnum(Attribute.Enum e) {147sb.append(e);148}149150public void visitCompound(Attribute.Compound c) {151sb.append(new AnnotationDescImpl(env, c));152}153154public void visitArray(Attribute.Array a) {155// Omit braces from singleton.156if (a.values.length != 1) sb.append('{');157158boolean first = true;159for (Attribute elem : a.values) {160if (first) {161first = false;162} else {163sb.append(", ");164}165elem.accept(this);166}167// Omit braces from singleton.168if (a.values.length != 1) sb.append('}');169}170171public void visitError(Attribute.Error e) {172sb.append("<error>");173}174}175}176177178