Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2003, 2012, 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;
28 import com.sun.javadoc.*;
30 import com.sun.tools.javac.code.Attribute;
31 import com.sun.tools.javac.code.Symbol.*;
32 import com.sun.tools.javac.util.List;
33 import com.sun.tools.javac.util.Pair;
36 /**
37 * Represents an annotation.
38 * An annotation associates a value with each element of an annotation type.
39 * Sure it ought to be called "Annotation", but that clashes with
40 * java.lang.annotation.Annotation.
41 *
42 * @author Scott Seligman
43 * @since 1.5
44 */
46 public class AnnotationDescImpl implements AnnotationDesc {
48 private final DocEnv env;
49 private final Attribute.Compound annotation;
52 AnnotationDescImpl(DocEnv env, Attribute.Compound annotation) {
53 this.env = env;
54 this.annotation = annotation;
55 }
57 /**
58 * Returns the annotation type of this annotation.
59 */
60 public AnnotationTypeDoc annotationType() {
61 ClassSymbol atsym = (ClassSymbol)annotation.type.tsym;
62 if (annotation.type.isErroneous()) {
63 env.warning(null, "javadoc.class_not_found", annotation.type.toString());
64 return new AnnotationTypeDocImpl(env, atsym);
65 } else {
66 return (AnnotationTypeDoc)env.getClassDoc(atsym);
67 }
68 }
70 /**
71 * Returns this annotation's elements and their values.
72 * Only those explicitly present in the annotation are
73 * included, not those assuming their default values.
74 * Returns an empty array if there are none.
75 */
76 public ElementValuePair[] elementValues() {
77 List<Pair<MethodSymbol,Attribute>> vals = annotation.values;
78 ElementValuePair res[] = new ElementValuePair[vals.length()];
79 int i = 0;
80 for (Pair<MethodSymbol,Attribute> val : vals) {
81 res[i++] = new ElementValuePairImpl(env, val.fst, val.snd);
82 }
83 return res;
84 }
86 /**
87 * Returns a string representation of this annotation.
88 * String is of one of the forms:
89 * @com.example.foo(name1=val1, name2=val2)
90 * @com.example.foo(val)
91 * @com.example.foo
92 * Omit parens for marker annotations, and omit "value=" when allowed.
93 */
94 @Override
95 public String toString() {
96 StringBuilder sb = new StringBuilder("@");
97 sb.append(annotation.type.tsym);
99 ElementValuePair vals[] = elementValues();
100 if (vals.length > 0) { // omit parens for marker annotation
101 sb.append('(');
102 boolean first = true;
103 for (ElementValuePair val : vals) {
104 if (!first) {
105 sb.append(", ");
106 }
107 first = false;
109 String name = val.element().name();
110 if (vals.length == 1 && name.equals("value")) {
111 sb.append(val.value());
112 } else {
113 sb.append(val);
114 }
115 }
116 sb.append(')');
117 }
118 return sb.toString();
119 }
122 /**
123 * Represents an association between an annotation type element
124 * and one of its values.
125 */
126 public static class ElementValuePairImpl implements ElementValuePair {
128 private final DocEnv env;
129 private final MethodSymbol meth;
130 private final Attribute value;
132 ElementValuePairImpl(DocEnv env, MethodSymbol meth, Attribute value) {
133 this.env = env;
134 this.meth = meth;
135 this.value = value;
136 }
138 /**
139 * Returns the annotation type element.
140 */
141 public AnnotationTypeElementDoc element() {
142 return env.getAnnotationTypeElementDoc(meth);
143 }
145 /**
146 * Returns the value associated with the annotation type element.
147 */
148 public AnnotationValue value() {
149 return new AnnotationValueImpl(env, value);
150 }
152 /**
153 * Returns a string representation of this pair
154 * of the form "name=value".
155 */
156 @Override
157 public String toString() {
158 return meth.name + "=" + value();
159 }
160 }
161 }