|
1 /* |
|
2 * Copyright 2007 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 */ |
|
25 |
|
26 package com.sun.tools.javap; |
|
27 |
|
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 |
|
35 /** |
|
36 * A writer for writing annotations as text. |
|
37 * |
|
38 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
39 * you write code that depends on this, you do so at your own risk. |
|
40 * This code and its internal interfaces are subject to change or |
|
41 * deletion without notice.</b> |
|
42 */ |
|
43 public class AnnotationWriter extends BasicWriter { |
|
44 static AnnotationWriter instance(Context context) { |
|
45 AnnotationWriter instance = context.get(AnnotationWriter.class); |
|
46 if (instance == null) |
|
47 instance = new AnnotationWriter(context); |
|
48 return instance; |
|
49 } |
|
50 |
|
51 protected AnnotationWriter(Context context) { |
|
52 super(context); |
|
53 } |
|
54 |
|
55 public void write(Annotation annot) { |
|
56 print("#" + annot.type_index + "("); |
|
57 for (int i = 0; i < annot.num_element_value_pairs; i++) { |
|
58 if (i > 0) |
|
59 print(","); |
|
60 write(annot.element_value_pairs[i]); |
|
61 } |
|
62 print(")"); |
|
63 } |
|
64 |
|
65 public void write(Annotation.element_value_pair pair) { |
|
66 print("#" + pair.element_name_index + ":"); |
|
67 write(pair.value); |
|
68 } |
|
69 |
|
70 public void write(Annotation.element_value value) { |
|
71 ev_writer.write(value); |
|
72 } |
|
73 |
|
74 element_value_Writer ev_writer = new element_value_Writer(); |
|
75 |
|
76 class element_value_Writer implements Annotation.element_value.Visitor<Void,Void> { |
|
77 public void write(Annotation.element_value value) { |
|
78 value.accept(this, null); |
|
79 } |
|
80 |
|
81 public Void visitPrimitive(Primitive_element_value ev, Void p) { |
|
82 print(((char) ev.tag) + "#" + ev.const_value_index); |
|
83 return null; |
|
84 } |
|
85 |
|
86 public Void visitEnum(Enum_element_value ev, Void p) { |
|
87 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index); |
|
88 return null; |
|
89 } |
|
90 |
|
91 public Void visitClass(Class_element_value ev, Void p) { |
|
92 print(((char) ev.tag) + "#" + ev.class_info_index); |
|
93 return null; |
|
94 } |
|
95 |
|
96 public Void visitAnnotation(Annotation_element_value ev, Void p) { |
|
97 print((char) ev.tag); |
|
98 AnnotationWriter.this.write(ev.annotation_value); |
|
99 return null; |
|
100 } |
|
101 |
|
102 public Void visitArray(Array_element_value ev, Void p) { |
|
103 print("["); |
|
104 for (int i = 0; i < ev.num_values; i++) { |
|
105 if (i > 0) |
|
106 print(","); |
|
107 write(ev.values[i]); |
|
108 } |
|
109 print("]"); |
|
110 return null; |
|
111 } |
|
112 |
|
113 } |
|
114 } |