Tue, 03 Jun 2008 13:26:47 -0700
4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini
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 */
26 package com.sun.tools.classfile;
28 import java.io.IOException;
30 /**
31 * See JVMS3, section 4.8.16.
32 *
33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
34 * you write code that depends on this, you do so at your own risk.
35 * This code and its internal interfaces are subject to change or
36 * deletion without notice.</b>
37 */
38 public class Annotation {
39 static class InvalidAnnotation extends AttributeException {
40 InvalidAnnotation(String msg) {
41 super(msg);
42 }
43 }
45 Annotation(ClassReader cr) throws IOException, InvalidAnnotation {
46 type_index = cr.readUnsignedShort();
47 num_element_value_pairs = cr.readUnsignedShort();
48 element_value_pairs = new element_value_pair[num_element_value_pairs];
49 for (int i = 0; i < element_value_pairs.length; i++)
50 element_value_pairs[i] = new element_value_pair(cr);
51 }
53 public Annotation(ConstantPool constant_pool,
54 int type_index,
55 element_value_pair[] element_value_pairs) {
56 this.type_index = type_index;
57 num_element_value_pairs = element_value_pairs.length;
58 this.element_value_pairs = element_value_pairs;
59 }
61 public int length() {
62 int n = 2 /*type_index*/ + 2 /*num_element_value_pairs*/;
63 for (element_value_pair pair: element_value_pairs)
64 n += pair.length();
65 return n;
66 }
68 public final int type_index;
69 public final int num_element_value_pairs;
70 public final element_value_pair element_value_pairs[];
72 /**
73 * See JVMS3, section 4.8.16.1.
74 */
75 public static abstract class element_value {
76 public static element_value read(ClassReader cr)
77 throws IOException, InvalidAnnotation {
78 int tag = cr.readUnsignedByte();
79 switch (tag) {
80 case 'B':
81 case 'C':
82 case 'D':
83 case 'F':
84 case 'I':
85 case 'J':
86 case 'S':
87 case 'Z':
88 case 's':
89 return new Primitive_element_value(cr, tag);
91 case 'e':
92 return new Enum_element_value(cr, tag);
94 case 'c':
95 return new Class_element_value(cr, tag);
97 case '@':
98 return new Annotation_element_value(cr, tag);
100 case '[':
101 return new Array_element_value(cr, tag);
103 default:
104 throw new InvalidAnnotation("unrecognized tag: " + tag);
105 }
106 }
108 protected element_value(int tag) {
109 this.tag = tag;
110 }
112 public abstract int length();
114 public abstract <R,P> R accept(Visitor<R,P> visitor, P p);
116 public interface Visitor<R,P> {
117 R visitPrimitive(Primitive_element_value ev, P p);
118 R visitEnum(Enum_element_value ev, P p);
119 R visitClass(Class_element_value ev, P p);
120 R visitAnnotation(Annotation_element_value ev, P p);
121 R visitArray(Array_element_value ev, P p);
122 }
124 public final int tag;
125 }
127 public static class Primitive_element_value extends element_value {
128 Primitive_element_value(ClassReader cr, int tag) throws IOException {
129 super(tag);
130 const_value_index = cr.readUnsignedShort();
131 }
133 @Override
134 public int length() {
135 return 2;
136 }
138 public <R,P> R accept(Visitor<R,P> visitor, P p) {
139 return visitor.visitPrimitive(this, p);
140 }
142 public final int const_value_index;
144 }
146 public static class Enum_element_value extends element_value {
147 Enum_element_value(ClassReader cr, int tag) throws IOException {
148 super(tag);
149 type_name_index = cr.readUnsignedShort();
150 const_name_index = cr.readUnsignedShort();
151 }
153 @Override
154 public int length() {
155 return 4;
156 }
158 public <R,P> R accept(Visitor<R,P> visitor, P p) {
159 return visitor.visitEnum(this, p);
160 }
162 public final int type_name_index;
163 public final int const_name_index;
164 }
166 public static class Class_element_value extends element_value {
167 Class_element_value(ClassReader cr, int tag) throws IOException {
168 super(tag);
169 class_info_index = cr.readUnsignedShort();
170 }
172 @Override
173 public int length() {
174 return 2;
175 }
177 public <R,P> R accept(Visitor<R,P> visitor, P p) {
178 return visitor.visitClass(this, p);
179 }
181 public final int class_info_index;
182 }
184 public static class Annotation_element_value extends element_value {
185 Annotation_element_value(ClassReader cr, int tag)
186 throws IOException, InvalidAnnotation {
187 super(tag);
188 annotation_value = new Annotation(cr);
189 }
191 @Override
192 public int length() {
193 return annotation_value.length();
194 }
196 public <R,P> R accept(Visitor<R,P> visitor, P p) {
197 return visitor.visitAnnotation(this, p);
198 }
200 public final Annotation annotation_value;
201 }
203 public static class Array_element_value extends element_value {
204 Array_element_value(ClassReader cr, int tag)
205 throws IOException, InvalidAnnotation {
206 super(tag);
207 num_values = cr.readUnsignedShort();
208 values = new element_value[num_values];
209 for (int i = 0; i < values.length; i++)
210 values[i] = element_value.read(cr);
211 }
213 @Override
214 public int length() {
215 int n = 2;
216 for (int i = 0; i < values.length; i++)
217 n += values[i].length();
218 return n;
219 }
221 public <R,P> R accept(Visitor<R,P> visitor, P p) {
222 return visitor.visitArray(this, p);
223 }
225 public final int num_values;
226 public final element_value[] values;
227 }
229 public static class element_value_pair {
230 element_value_pair(ClassReader cr)
231 throws IOException, InvalidAnnotation {
232 element_name_index = cr.readUnsignedShort();
233 value = element_value.read(cr);
234 }
236 public int length() {
237 return 2 + value.length();
238 }
240 public final int element_name_index;
241 public final element_value value;
242 }
243 }