Wed, 06 Apr 2011 20:33:44 -0700
7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes
1 /*
2 * Copyright (c) 2007, 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.javap;
28 import com.sun.tools.classfile.Annotation;
29 import com.sun.tools.classfile.Annotation.Annotation_element_value;
30 import com.sun.tools.classfile.Annotation.Array_element_value;
31 import com.sun.tools.classfile.Annotation.Class_element_value;
32 import com.sun.tools.classfile.Annotation.Enum_element_value;
33 import com.sun.tools.classfile.Annotation.Primitive_element_value;
34 import com.sun.tools.classfile.ConstantPool;
35 import com.sun.tools.classfile.ConstantPoolException;
36 import com.sun.tools.classfile.Descriptor;
37 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
39 /**
40 * A writer for writing annotations as text.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 */
47 public class AnnotationWriter extends BasicWriter {
48 static AnnotationWriter instance(Context context) {
49 AnnotationWriter instance = context.get(AnnotationWriter.class);
50 if (instance == null)
51 instance = new AnnotationWriter(context);
52 return instance;
53 }
55 protected AnnotationWriter(Context context) {
56 super(context);
57 classWriter = ClassWriter.instance(context);
58 constantWriter = ConstantWriter.instance(context);
59 }
61 public void write(Annotation annot) {
62 write(annot, false);
63 }
65 public void write(Annotation annot, boolean resolveIndices) {
66 writeDescriptor(annot.type_index, resolveIndices);
67 boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
68 if (showParens)
69 print("(");
70 for (int i = 0; i < annot.num_element_value_pairs; i++) {
71 if (i > 0)
72 print(",");
73 write(annot.element_value_pairs[i], resolveIndices);
74 }
75 if (showParens)
76 print(")");
77 }
79 public void write(Annotation.element_value_pair pair) {
80 write(pair, false);
81 }
83 public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
84 writeIndex(pair.element_name_index, resolveIndices);
85 print("=");
86 write(pair.value, resolveIndices);
87 }
89 public void write(Annotation.element_value value) {
90 write(value, false);
91 }
93 public void write(Annotation.element_value value, boolean resolveIndices) {
94 ev_writer.write(value, resolveIndices);
95 }
97 private void writeDescriptor(int index, boolean resolveIndices) {
98 if (resolveIndices) {
99 try {
100 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
101 Descriptor d = new Descriptor(index);
102 print(d.getFieldType(constant_pool));
103 return;
104 } catch (ConstantPoolException ignore) {
105 } catch (InvalidDescriptor ignore) {
106 }
107 }
109 print("#" + index);
110 }
112 private void writeIndex(int index, boolean resolveIndices) {
113 if (resolveIndices) {
114 print(constantWriter.stringValue(index));
115 } else
116 print("#" + index);
117 }
119 element_value_Writer ev_writer = new element_value_Writer();
121 class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
122 public void write(Annotation.element_value value, boolean resolveIndices) {
123 value.accept(this, resolveIndices);
124 }
126 public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
127 if (resolveIndices)
128 writeIndex(ev.const_value_index, resolveIndices);
129 else
130 print(((char) ev.tag) + "#" + ev.const_value_index);
131 return null;
132 }
134 public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
135 if (resolveIndices) {
136 writeIndex(ev.type_name_index, resolveIndices);
137 print(".");
138 writeIndex(ev.const_name_index, resolveIndices);
139 } else
140 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
141 return null;
142 }
144 public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
145 if (resolveIndices) {
146 writeIndex(ev.class_info_index, resolveIndices);
147 print(".class");
148 } else
149 print(((char) ev.tag) + "#" + ev.class_info_index);
150 return null;
151 }
153 public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
154 print((char) ev.tag);
155 AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
156 return null;
157 }
159 public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
160 print("[");
161 for (int i = 0; i < ev.num_values; i++) {
162 if (i > 0)
163 print(",");
164 write(ev.values[i], resolveIndices);
165 }
166 print("]");
167 return null;
168 }
170 }
172 private ClassWriter classWriter;
173 private ConstantWriter constantWriter;
174 }