aoqi@0: /*
aoqi@0: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javadoc;
aoqi@0:
aoqi@0: import com.sun.javadoc.*;
aoqi@0:
aoqi@0: import com.sun.tools.javac.code.Attribute;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
aoqi@0:
aoqi@0: /**
aoqi@0: * Represents a value of an annotation type element.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: *
aoqi@0: * @author Scott Seligman
aoqi@0: * @since 1.5
aoqi@0: */
aoqi@0:
aoqi@0: public class AnnotationValueImpl implements AnnotationValue {
aoqi@0:
aoqi@0: private final DocEnv env;
aoqi@0: private final Attribute attr;
aoqi@0:
aoqi@0:
aoqi@0: AnnotationValueImpl(DocEnv env, Attribute attr) {
aoqi@0: this.env = env;
aoqi@0: this.attr = attr;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the value.
aoqi@0: * The type of the returned object is one of the following:
aoqi@0: *
- a wrapper class for a primitive type
aoqi@0: *
-
String
aoqi@0: * -
Type
(representing a class literal)
aoqi@0: * -
FieldDoc
(representing an enum constant)
aoqi@0: * -
AnnotationDesc
aoqi@0: * -
AnnotationValue[]
aoqi@0: *
aoqi@0: */
aoqi@0: public Object value() {
aoqi@0: ValueVisitor vv = new ValueVisitor();
aoqi@0: attr.accept(vv);
aoqi@0: return vv.value;
aoqi@0: }
aoqi@0:
aoqi@0: private class ValueVisitor implements Attribute.Visitor {
aoqi@0: public Object value;
aoqi@0:
aoqi@0: public void visitConstant(Attribute.Constant c) {
aoqi@0: if (c.type.hasTag(BOOLEAN)) {
aoqi@0: // javac represents false and true as integers 0 and 1
aoqi@0: value = Boolean.valueOf(
aoqi@0: ((Integer)c.value).intValue() != 0);
aoqi@0: } else {
aoqi@0: value = c.value;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void visitClass(Attribute.Class c) {
aoqi@0: value = TypeMaker.getType(env,
aoqi@0: env.types.erasure(c.classType));
aoqi@0: }
aoqi@0:
aoqi@0: public void visitEnum(Attribute.Enum e) {
aoqi@0: value = env.getFieldDoc(e.value);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitCompound(Attribute.Compound c) {
aoqi@0: value = new AnnotationDescImpl(env, c);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitArray(Attribute.Array a) {
aoqi@0: AnnotationValue vals[] = new AnnotationValue[a.values.length];
aoqi@0: for (int i = 0; i < vals.length; i++) {
aoqi@0: vals[i] = new AnnotationValueImpl(env, a.values[i]);
aoqi@0: }
aoqi@0: value = vals;
aoqi@0: }
aoqi@0:
aoqi@0: public void visitError(Attribute.Error e) {
aoqi@0: value = "";
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns a string representation of the value.
aoqi@0: *
aoqi@0: * @return the text of a Java language annotation value expression
aoqi@0: * whose value is the value of this annotation type element.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: ToStringVisitor tv = new ToStringVisitor();
aoqi@0: attr.accept(tv);
aoqi@0: return tv.toString();
aoqi@0: }
aoqi@0:
aoqi@0: private class ToStringVisitor implements Attribute.Visitor {
aoqi@0: private final StringBuilder sb = new StringBuilder();
aoqi@0:
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return sb.toString();
aoqi@0: }
aoqi@0:
aoqi@0: public void visitConstant(Attribute.Constant c) {
aoqi@0: if (c.type.hasTag(BOOLEAN)) {
aoqi@0: // javac represents false and true as integers 0 and 1
aoqi@0: sb.append(((Integer)c.value).intValue() != 0);
aoqi@0: } else {
aoqi@0: sb.append(FieldDocImpl.constantValueExpression(c.value));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void visitClass(Attribute.Class c) {
aoqi@0: sb.append(c);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitEnum(Attribute.Enum e) {
aoqi@0: sb.append(e);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitCompound(Attribute.Compound c) {
aoqi@0: sb.append(new AnnotationDescImpl(env, c));
aoqi@0: }
aoqi@0:
aoqi@0: public void visitArray(Attribute.Array a) {
aoqi@0: // Omit braces from singleton.
aoqi@0: if (a.values.length != 1) sb.append('{');
aoqi@0:
aoqi@0: boolean first = true;
aoqi@0: for (Attribute elem : a.values) {
aoqi@0: if (first) {
aoqi@0: first = false;
aoqi@0: } else {
aoqi@0: sb.append(", ");
aoqi@0: }
aoqi@0: elem.accept(this);
aoqi@0: }
aoqi@0: // Omit braces from singleton.
aoqi@0: if (a.values.length != 1) sb.append('}');
aoqi@0: }
aoqi@0:
aoqi@0: public void visitError(Attribute.Error e) {
aoqi@0: sb.append("");
aoqi@0: }
aoqi@0: }
aoqi@0: }