langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/AnnotationValueImpl.java
changeset 37938 42baa89d2156
parent 37858 7c04fcb12bd4
child 37939 3eb8c2a89b77
equal deleted inserted replaced
37858:7c04fcb12bd4 37938:42baa89d2156
     1 /*
       
     2  * Copyright (c) 2003, 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  */
       
    25 
       
    26 package com.sun.tools.javadoc;
       
    27 
       
    28 import com.sun.javadoc.*;
       
    29 
       
    30 import com.sun.tools.javac.code.Attribute;
       
    31 
       
    32 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
       
    33 
       
    34 /**
       
    35  * Represents a value of an annotation type element.
       
    36  *
       
    37  *  <p><b>This is NOT part of any supported API.
       
    38  *  If you write code that depends on this, you do so at your own risk.
       
    39  *  This code and its internal interfaces are subject to change or
       
    40  *  deletion without notice.</b>
       
    41  *
       
    42  * @author Scott Seligman
       
    43  * @since 1.5
       
    44  */
       
    45 
       
    46 public class AnnotationValueImpl implements AnnotationValue {
       
    47 
       
    48     private final DocEnv env;
       
    49     private final Attribute attr;
       
    50 
       
    51 
       
    52     AnnotationValueImpl(DocEnv env, Attribute attr) {
       
    53         this.env = env;
       
    54         this.attr = attr;
       
    55     }
       
    56 
       
    57     /**
       
    58      * Returns the value.
       
    59      * The type of the returned object is one of the following:
       
    60      * <ul><li> a wrapper class for a primitive type
       
    61      *     <li> <code>String</code>
       
    62      *     <li> <code>Type</code> (representing a class literal)
       
    63      *     <li> <code>FieldDoc</code> (representing an enum constant)
       
    64      *     <li> <code>AnnotationDesc</code>
       
    65      *     <li> <code>AnnotationValue[]</code>
       
    66      * </ul>
       
    67      */
       
    68     public Object value() {
       
    69         ValueVisitor vv = new ValueVisitor();
       
    70         attr.accept(vv);
       
    71         return vv.value;
       
    72     }
       
    73 
       
    74     private class ValueVisitor implements Attribute.Visitor {
       
    75         public Object value;
       
    76 
       
    77         public void visitConstant(Attribute.Constant c) {
       
    78             if (c.type.hasTag(BOOLEAN)) {
       
    79                 // javac represents false and true as integers 0 and 1
       
    80                 value = Boolean.valueOf(
       
    81                                 ((Integer)c.value).intValue() != 0);
       
    82             } else {
       
    83                 value = c.value;
       
    84             }
       
    85         }
       
    86 
       
    87         public void visitClass(Attribute.Class c) {
       
    88             value = TypeMaker.getType(env,
       
    89                                       env.types.erasure(c.classType));
       
    90         }
       
    91 
       
    92         public void visitEnum(Attribute.Enum e) {
       
    93             value = env.getFieldDoc(e.value);
       
    94         }
       
    95 
       
    96         public void visitCompound(Attribute.Compound c) {
       
    97             value = new AnnotationDescImpl(env, c);
       
    98         }
       
    99 
       
   100         public void visitArray(Attribute.Array a) {
       
   101             AnnotationValue vals[] = new AnnotationValue[a.values.length];
       
   102             for (int i = 0; i < vals.length; i++) {
       
   103                 vals[i] = new AnnotationValueImpl(env, a.values[i]);
       
   104             }
       
   105             value = vals;
       
   106         }
       
   107 
       
   108         public void visitError(Attribute.Error e) {
       
   109             value = "<error>";
       
   110         }
       
   111     }
       
   112 
       
   113     /**
       
   114      * Returns a string representation of the value.
       
   115      *
       
   116      * @return the text of a Java language annotation value expression
       
   117      *          whose value is the value of this annotation type element.
       
   118      */
       
   119     @Override
       
   120     public String toString() {
       
   121         ToStringVisitor tv = new ToStringVisitor();
       
   122         attr.accept(tv);
       
   123         return tv.toString();
       
   124     }
       
   125 
       
   126     private class ToStringVisitor implements Attribute.Visitor {
       
   127         private final StringBuilder sb = new StringBuilder();
       
   128 
       
   129         @Override
       
   130         public String toString() {
       
   131             return sb.toString();
       
   132         }
       
   133 
       
   134         public void visitConstant(Attribute.Constant c) {
       
   135             if (c.type.hasTag(BOOLEAN)) {
       
   136                 // javac represents false and true as integers 0 and 1
       
   137                 sb.append(((Integer)c.value).intValue() != 0);
       
   138             } else {
       
   139                 sb.append(FieldDocImpl.constantValueExpression(c.value));
       
   140             }
       
   141         }
       
   142 
       
   143         public void visitClass(Attribute.Class c) {
       
   144             sb.append(c);
       
   145         }
       
   146 
       
   147         public void visitEnum(Attribute.Enum e) {
       
   148             sb.append(e);
       
   149         }
       
   150 
       
   151         public void visitCompound(Attribute.Compound c) {
       
   152             sb.append(new AnnotationDescImpl(env, c));
       
   153         }
       
   154 
       
   155         public void visitArray(Attribute.Array a) {
       
   156             // Omit braces from singleton.
       
   157             if (a.values.length != 1) sb.append('{');
       
   158 
       
   159             boolean first = true;
       
   160             for (Attribute elem : a.values) {
       
   161                 if (first) {
       
   162                     first = false;
       
   163                 } else {
       
   164                     sb.append(", ");
       
   165                 }
       
   166                 elem.accept(this);
       
   167             }
       
   168             // Omit braces from singleton.
       
   169             if (a.values.length != 1) sb.append('}');
       
   170         }
       
   171 
       
   172         public void visitError(Attribute.Error e) {
       
   173             sb.append("<error>");
       
   174         }
       
   175     }
       
   176 }