001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javadoc;
027:
028: import com.sun.javadoc.*;
029:
030: import com.sun.tools.javac.code.Attribute;
031: import com.sun.tools.javac.code.Symbol.*;
032: import com.sun.tools.javac.util.List;
033: import com.sun.tools.javac.util.Pair;
034:
035: /**
036: * Represents an annotation.
037: * An annotation associates a value with each element of an annotation type.
038: * Sure it ought to be called "Annotation", but that clashes with
039: * java.lang.annotation.Annotation.
040: *
041: * @author Scott Seligman
042: * @version 1.11 07/05/05
043: * @since 1.5
044: */
045:
046: public class AnnotationDescImpl implements AnnotationDesc {
047:
048: private final DocEnv env;
049: private final Attribute.Compound annotation;
050:
051: AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
052: this .env = env;
053: this .annotation = annotation;
054: }
055:
056: /**
057: * Returns the annotation type of this annotation.
058: */
059: public AnnotationTypeDoc annotationType() {
060: ClassSymbol atsym = (ClassSymbol) annotation.type.tsym;
061: return (AnnotationTypeDoc) env.getClassDoc(atsym);
062: }
063:
064: /**
065: * Returns this annotation's elements and their values.
066: * Only those explicitly present in the annotation are
067: * included, not those assuming their default values.
068: * Returns an empty array if there are none.
069: */
070: public ElementValuePair[] elementValues() {
071: List<Pair<MethodSymbol, Attribute>> vals = annotation.values;
072: ElementValuePair res[] = new ElementValuePair[vals.length()];
073: int i = 0;
074: for (Pair<MethodSymbol, Attribute> val : vals) {
075: res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
076: }
077: return res;
078: }
079:
080: /**
081: * Returns a string representation of this annotation.
082: * String is of one of the forms:
083: * @com.example.foo(name1=val1, name2=val2)
084: * @com.example.foo(val)
085: * @com.example.foo
086: * Omit parens for marker annotations, and omit "value=" when allowed.
087: */
088: public String toString() {
089: StringBuffer sb = new StringBuffer("@");
090: sb.append(annotation.type.tsym);
091:
092: ElementValuePair vals[] = elementValues();
093: if (vals.length > 0) { // omit parens for marker annotation
094: sb.append('(');
095: boolean first = true;
096: for (ElementValuePair val : vals) {
097: if (!first) {
098: sb.append(", ");
099: }
100: first = false;
101:
102: String name = val.element().name();
103: if (vals.length == 1 && name.equals("value")) {
104: sb.append(val.value());
105: } else {
106: sb.append(val);
107: }
108: }
109: sb.append(')');
110: }
111: return sb.toString();
112: }
113:
114: /**
115: * Represents an association between an annotation type element
116: * and one of its values.
117: */
118: public static class ElementValuePairImpl implements
119: ElementValuePair {
120:
121: private final DocEnv env;
122: private final MethodSymbol meth;
123: private final Attribute value;
124:
125: ElementValuePairImpl(DocEnv env, MethodSymbol meth,
126: Attribute value) {
127: this .env = env;
128: this .meth = meth;
129: this .value = value;
130: }
131:
132: /**
133: * Returns the annotation type element.
134: */
135: public AnnotationTypeElementDoc element() {
136: return env.getAnnotationTypeElementDoc(meth);
137: }
138:
139: /**
140: * Returns the value associated with the annotation type element.
141: */
142: public AnnotationValue value() {
143: return new AnnotationValueImpl(env, value);
144: }
145:
146: /**
147: * Returns a string representation of this pair
148: * of the form "name=value".
149: */
150: public String toString() {
151: return meth.name + "=" + value();
152: }
153: }
154: }
|