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

Thu, 18 Sep 2008 18:39:44 -0700

author
jjg
date
Thu, 18 Sep 2008 18:39:44 -0700
changeset 115
829dea15ff99
parent 1
9a66ca7c79fa
child 406
071a4e36cd87
permissions
-rw-r--r--

6744408: Extra ouput is appearing in stderr
Reviewed-by: bpatel

     1 /*
     2  * Copyright 2003-2004 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any 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.util.List;
    34 import com.sun.tools.javac.util.Pair;
    37 /**
    38  * Represents an annotation.
    39  * An annotation associates a value with each element of an annotation type.
    40  * Sure it ought to be called "Annotation", but that clashes with
    41  * java.lang.annotation.Annotation.
    42  *
    43  * @author Scott Seligman
    44  * @since 1.5
    45  */
    47 public class AnnotationDescImpl implements AnnotationDesc {
    49     private final DocEnv env;
    50     private final Attribute.Compound annotation;
    53     AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
    54         this.env = env;
    55         this.annotation = annotation;
    56     }
    58     /**
    59      * Returns the annotation type of this annotation.
    60      */
    61     public AnnotationTypeDoc annotationType() {
    62         ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
    63         return (AnnotationTypeDoc)env.getClassDoc(atsym);
    64     }
    66     /**
    67      * Returns this annotation's elements and their values.
    68      * Only those explicitly present in the annotation are
    69      * included, not those assuming their default values.
    70      * Returns an empty array if there are none.
    71      */
    72     public ElementValuePair[] elementValues() {
    73         List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
    74         ElementValuePair res[] = new ElementValuePair[vals.length()];
    75         int i = 0;
    76         for (Pair<MethodSymbol,Attribute> val : vals) {
    77             res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
    78         }
    79         return res;
    80     }
    82     /**
    83      * Returns a string representation of this annotation.
    84      * String is of one of the forms:
    85      *     @com.example.foo(name1=val1, name2=val2)
    86      *     @com.example.foo(val)
    87      *     @com.example.foo
    88      * Omit parens for marker annotations, and omit "value=" when allowed.
    89      */
    90     public String toString() {
    91         StringBuffer sb = new StringBuffer("@");
    92         sb.append(annotation.type.tsym);
    94         ElementValuePair vals[] = elementValues();
    95         if (vals.length > 0) {          // omit parens for marker annotation
    96             sb.append('(');
    97             boolean first = true;
    98             for (ElementValuePair val : vals) {
    99                 if (!first) {
   100                     sb.append(", ");
   101                 }
   102                 first = false;
   104                 String name = val.element().name();
   105                 if (vals.length == 1 && name.equals("value")) {
   106                     sb.append(val.value());
   107                 } else {
   108                     sb.append(val);
   109                 }
   110             }
   111             sb.append(')');
   112         }
   113         return sb.toString();
   114     }
   117     /**
   118      * Represents an association between an annotation type element
   119      * and one of its values.
   120      */
   121     public static class ElementValuePairImpl implements ElementValuePair {
   123         private final DocEnv env;
   124         private final MethodSymbol meth;
   125         private final Attribute value;
   127         ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
   128             this.env = env;
   129             this.meth = meth;
   130             this.value = value;
   131         }
   133         /**
   134          * Returns the annotation type element.
   135          */
   136         public AnnotationTypeElementDoc element() {
   137             return env.getAnnotationTypeElementDoc(meth);
   138         }
   140         /**
   141          * Returns the value associated with the annotation type element.
   142          */
   143         public AnnotationValue value() {
   144             return new AnnotationValueImpl(env, value);
   145         }
   147         /**
   148          * Returns a string representation of this pair
   149          * of the form "name=value".
   150          */
   151         public String toString() {
   152             return meth.name + "=" + value();
   153         }
   154     }
   155 }

mercurial