Thu, 30 Jul 2009 09:18:55 -0700
4880672: javap does not output inner interfaces of an interface
Reviewed-by: mcimadamore
1 /*
2 * Copyright 2007-2008 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.javap;
28 import com.sun.tools.classfile.Annotation;
29 import com.sun.tools.classfile.ExtendedAnnotation;
30 import com.sun.tools.classfile.Annotation.Annotation_element_value;
31 import com.sun.tools.classfile.Annotation.Array_element_value;
32 import com.sun.tools.classfile.Annotation.Class_element_value;
33 import com.sun.tools.classfile.Annotation.Enum_element_value;
34 import com.sun.tools.classfile.Annotation.Primitive_element_value;
35 import com.sun.tools.classfile.ConstantPool;
36 import com.sun.tools.classfile.ConstantPoolException;
37 import com.sun.tools.classfile.Descriptor;
38 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
40 /**
41 * A writer for writing annotations as text.
42 *
43 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
44 * you write code that depends on this, you do so at your own risk.
45 * This code and its internal interfaces are subject to change or
46 * deletion without notice.</b>
47 */
48 public class AnnotationWriter extends BasicWriter {
49 static AnnotationWriter instance(Context context) {
50 AnnotationWriter instance = context.get(AnnotationWriter.class);
51 if (instance == null)
52 instance = new AnnotationWriter(context);
53 return instance;
54 }
56 protected AnnotationWriter(Context context) {
57 super(context);
58 classWriter = ClassWriter.instance(context);
59 constantWriter = ConstantWriter.instance(context);
60 }
62 public void write(Annotation annot) {
63 write(annot, false);
64 }
66 public void write(Annotation annot, boolean resolveIndices) {
67 writeDescriptor(annot.type_index, resolveIndices);
68 boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
69 if (showParens)
70 print("(");
71 for (int i = 0; i < annot.num_element_value_pairs; i++) {
72 if (i > 0)
73 print(",");
74 write(annot.element_value_pairs[i], resolveIndices);
75 }
76 if (showParens)
77 print(")");
78 }
80 public void write(ExtendedAnnotation annot) {
81 write(annot, true, false);
82 }
84 public void write(ExtendedAnnotation annot, boolean showOffsets, boolean resolveIndices) {
85 write(annot.annotation, resolveIndices);
86 print(": ");
87 write(annot.position, showOffsets);
88 }
90 public void write(ExtendedAnnotation.Position pos, boolean showOffsets) {
91 print(pos.type);
93 switch (pos.type) {
94 // type case
95 case TYPECAST:
96 case TYPECAST_GENERIC_OR_ARRAY:
97 // object creation
98 case INSTANCEOF:
99 case INSTANCEOF_GENERIC_OR_ARRAY:
100 // new expression
101 case NEW:
102 case NEW_GENERIC_OR_ARRAY:
103 case NEW_TYPE_ARGUMENT:
104 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
105 if (showOffsets) {
106 print(", offset=");
107 print(pos.offset);
108 }
109 break;
110 // local variable
111 case LOCAL_VARIABLE:
112 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
113 print(", {");
114 for (int i = 0; i < pos.lvarOffset.length; ++i) {
115 if (i != 0) print("; ");
116 if (showOffsets) {
117 print(", start_pc=");
118 print(pos.lvarOffset[i]);
119 }
120 print(", length=");
121 print(pos.lvarLength[i]);
122 print(", index=");
123 print(pos.lvarIndex[i]);
124 }
125 print("}");
126 break;
127 // method receiver
128 case METHOD_RECEIVER:
129 // Do nothing
130 break;
131 // type parameters
132 case CLASS_TYPE_PARAMETER:
133 case METHOD_TYPE_PARAMETER:
134 print(", param_index=");
135 print(pos.parameter_index);
136 break;
137 // type parameters bound
138 case CLASS_TYPE_PARAMETER_BOUND:
139 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
140 case METHOD_TYPE_PARAMETER_BOUND:
141 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
142 print(", param_index=");
143 print(pos.parameter_index);
144 print(", bound_index=");
145 print(pos.bound_index);
146 break;
147 // wildcard
148 case WILDCARD_BOUND:
149 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
150 print(", wild_card=");
151 print(pos.wildcard_position);
152 break;
153 // Class extends and implements clauses
154 case CLASS_EXTENDS:
155 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
156 print(", type_index=");
157 print(pos.type_index);
158 break;
159 // throws
160 case THROWS:
161 print(", type_index=");
162 print(pos.type_index);
163 break;
164 case CLASS_LITERAL:
165 if (showOffsets) {
166 print(", offset=");
167 print(pos.offset);
168 }
169 break;
170 // method parameter: not specified
171 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
172 print(", param_index=");
173 print(pos.parameter_index);
174 break;
175 // method type argument: wasn't specified
176 case METHOD_TYPE_ARGUMENT:
177 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
178 if (showOffsets) {
179 print(", offset=");
180 print(pos.offset);
181 }
182 print(", type_index=");
183 print(pos.type_index);
184 break;
185 // We don't need to worry abut these
186 case METHOD_RETURN_GENERIC_OR_ARRAY:
187 case FIELD_GENERIC_OR_ARRAY:
188 break;
189 case UNKNOWN:
190 break;
191 default:
192 throw new AssertionError("unknown type: " + pos.type);
193 }
195 // Append location data for generics/arrays.
196 if (pos.type.hasLocation()) {
197 print(", location=");
198 print(pos.location);
199 }
200 }
202 public void write(Annotation.element_value_pair pair) {
203 write(pair, false);
204 }
206 public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
207 writeIndex(pair.element_name_index, resolveIndices);
208 print("=");
209 write(pair.value, resolveIndices);
210 }
212 public void write(Annotation.element_value value) {
213 write(value, false);
214 }
216 public void write(Annotation.element_value value, boolean resolveIndices) {
217 ev_writer.write(value, resolveIndices);
218 }
220 private void writeDescriptor(int index, boolean resolveIndices) {
221 if (resolveIndices) {
222 try {
223 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
224 Descriptor d = new Descriptor(index);
225 print(d.getFieldType(constant_pool));
226 return;
227 } catch (ConstantPoolException ignore) {
228 } catch (InvalidDescriptor ignore) {
229 }
230 }
232 print("#" + index);
233 }
235 private void writeIndex(int index, boolean resolveIndices) {
236 if (resolveIndices) {
237 print(constantWriter.stringValue(index));
238 } else
239 print("#" + index);
240 }
242 element_value_Writer ev_writer = new element_value_Writer();
244 class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
245 public void write(Annotation.element_value value, boolean resolveIndices) {
246 value.accept(this, resolveIndices);
247 }
249 public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
250 if (resolveIndices)
251 writeIndex(ev.const_value_index, resolveIndices);
252 else
253 print(((char) ev.tag) + "#" + ev.const_value_index);
254 return null;
255 }
257 public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
258 if (resolveIndices) {
259 writeIndex(ev.type_name_index, resolveIndices);
260 print(".");
261 writeIndex(ev.const_name_index, resolveIndices);
262 } else
263 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
264 return null;
265 }
267 public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
268 if (resolveIndices) {
269 writeIndex(ev.class_info_index, resolveIndices);
270 print(".class");
271 } else
272 print(((char) ev.tag) + "#" + ev.class_info_index);
273 return null;
274 }
276 public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
277 print((char) ev.tag);
278 AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
279 return null;
280 }
282 public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
283 print("[");
284 for (int i = 0; i < ev.num_values; i++) {
285 if (i > 0)
286 print(",");
287 write(ev.values[i], resolveIndices);
288 }
289 print("]");
290 return null;
291 }
293 }
295 private ClassWriter classWriter;
296 private ConstantWriter constantWriter;
297 }