src/share/classes/com/sun/tools/javadoc/AnnotationValueImpl.java

Fri, 04 Mar 2011 19:53:03 -0800

author
jjg
date
Fri, 04 Mar 2011 19:53:03 -0800
changeset 910
ebf7c13df6c0
parent 554
9d9f26857129
child 1313
873ddd9f4900
permissions
-rw-r--r--

6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
Reviewed-by: bpatel

     1 /*
     2  * Copyright (c) 2003, 2011, 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  */
    26 package com.sun.tools.javadoc;
    29 import com.sun.javadoc.*;
    31 import com.sun.tools.javac.code.Attribute;
    32 import com.sun.tools.javac.code.Symbol.*;
    33 import com.sun.tools.javac.code.TypeTags;
    36 /**
    37  * Represents a value of an annotation type element.
    38  *
    39  * @author Scott Seligman
    40  * @since 1.5
    41  */
    43 public class AnnotationValueImpl implements AnnotationValue {
    45     private final DocEnv env;
    46     private final Attribute attr;
    49     AnnotationValueImpl(DocEnv env, Attribute attr) {
    50         this.env = env;
    51         this.attr = attr;
    52     }
    54     /**
    55      * Returns the value.
    56      * The type of the returned object is one of the following:
    57      * <ul><li> a wrapper class for a primitive type
    58      *     <li> <code>String</code>
    59      *     <li> <code>Type</code> (representing a class literal)
    60      *     <li> <code>FieldDoc</code> (representing an enum constant)
    61      *     <li> <code>AnnotationDesc</code>
    62      *     <li> <code>AnnotationValue[]</code>
    63      * </ul>
    64      */
    65     public Object value() {
    66         ValueVisitor vv = new ValueVisitor();
    67         attr.accept(vv);
    68         return vv.value;
    69     }
    71     private class ValueVisitor implements Attribute.Visitor {
    72         public Object value;
    74         public void visitConstant(Attribute.Constant c) {
    75             if (c.type.tag == TypeTags.BOOLEAN) {
    76                 // javac represents false and true as integers 0 and 1
    77                 value = Boolean.valueOf(
    78                                 ((Integer)c.value).intValue() != 0);
    79             } else {
    80                 value = c.value;
    81             }
    82         }
    84         public void visitClass(Attribute.Class c) {
    85             value = TypeMaker.getType(env,
    86                                       env.types.erasure(c.type));
    87         }
    89         public void visitEnum(Attribute.Enum e) {
    90             value = env.getFieldDoc(e.value);
    91         }
    93         public void visitCompound(Attribute.Compound c) {
    94             value = new AnnotationDescImpl(env, c);
    95         }
    97         public void visitArray(Attribute.Array a) {
    98             AnnotationValue vals[] = new AnnotationValue[a.values.length];
    99             for (int i = 0; i < vals.length; i++) {
   100                 vals[i] = new AnnotationValueImpl(env, a.values[i]);
   101             }
   102             value = vals;
   103         }
   105         public void visitError(Attribute.Error e) {
   106             value = "<error>";
   107         }
   108     }
   110     /**
   111      * Returns a string representation of the value.
   112      *
   113      * @return the text of a Java language annotation value expression
   114      *          whose value is the value of this annotation type element.
   115      */
   116     @Override
   117     public String toString() {
   118         ToStringVisitor tv = new ToStringVisitor();
   119         attr.accept(tv);
   120         return tv.toString();
   121     }
   123     private class ToStringVisitor implements Attribute.Visitor {
   124         private final StringBuilder sb = new StringBuilder();
   126         @Override
   127         public String toString() {
   128             return sb.toString();
   129         }
   131         public void visitConstant(Attribute.Constant c) {
   132             if (c.type.tag == TypeTags.BOOLEAN) {
   133                 // javac represents false and true as integers 0 and 1
   134                 sb.append(((Integer)c.value).intValue() != 0);
   135             } else {
   136                 sb.append(FieldDocImpl.constantValueExpression(c.value));
   137             }
   138         }
   140         public void visitClass(Attribute.Class c) {
   141             sb.append(c);
   142         }
   144         public void visitEnum(Attribute.Enum e) {
   145             sb.append(e);
   146         }
   148         public void visitCompound(Attribute.Compound c) {
   149             sb.append(new AnnotationDescImpl(env, c));
   150         }
   152         public void visitArray(Attribute.Array a) {
   153             // Omit braces from singleton.
   154             if (a.values.length != 1) sb.append('{');
   156             boolean first = true;
   157             for (Attribute elem : a.values) {
   158                 if (first) {
   159                     first = false;
   160                 } else {
   161                     sb.append(", ");
   162                 }
   163                 elem.accept(this);
   164             }
   165             // Omit braces from singleton.
   166             if (a.values.length != 1) sb.append('}');
   167         }
   169         public void visitError(Attribute.Error e) {
   170             sb.append("<error>");
   171         }
   172     }
   173 }

mercurial