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 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.classfile;
28 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
29 import com.sun.tools.classfile.ConstantPool.CONSTANT_Double_info;
30 import com.sun.tools.classfile.ConstantPool.CONSTANT_Fieldref_info;
31 import com.sun.tools.classfile.ConstantPool.CONSTANT_Float_info;
32 import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info;
33 import com.sun.tools.classfile.ConstantPool.CONSTANT_InterfaceMethodref_info;
34 import com.sun.tools.classfile.ConstantPool.CONSTANT_Long_info;
35 import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
36 import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info;
37 import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info;
38 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
39 import com.sun.tools.classfile.ConstantPool.CPInfo;
40 import java.util.Map;
42 /**
43 * Rewrites a class file using a map of translations.
44 *
45 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
46 * you write code that depends on this, you do so at your own risk.
47 * This code and its internal interfaces are subject to change or
48 * deletion without notice.</b>
49 */
50 public class ClassTranslator
51 implements ConstantPool.Visitor<ConstantPool.CPInfo,Map<Object,Object>> {
52 /**
53 * Create a new ClassFile from {@code cf}, such that for all entries
54 * {@code k -\> v} in {@code translations},
55 * each occurrence of {@code k} in {@code cf} will be replaced by {@code v}.
56 * in
57 * @param cf the class file to be processed
58 * @param translations the set of translations to be applied
59 * @return a copy of {@code} with the values in {@code translations} substituted
60 */
61 public ClassFile translate(ClassFile cf, Map<Object,Object> translations) {
62 ClassFile cf2 = (ClassFile) translations.get(cf);
63 if (cf2 == null) {
64 ConstantPool constant_pool2 = translate(cf.constant_pool, translations);
65 Field[] fields2 = translate(cf.fields, cf.constant_pool, translations);
66 Method[] methods2 = translateMethods(cf.methods, cf.constant_pool, translations);
67 Attributes attributes2 = translateAttributes(cf.attributes, cf.constant_pool,
68 translations);
70 if (constant_pool2 == cf.constant_pool &&
71 fields2 == cf.fields &&
72 methods2 == cf.methods &&
73 attributes2 == cf.attributes)
74 cf2 = cf;
75 else
76 cf2 = new ClassFile(
77 cf.magic,
78 cf.minor_version,
79 cf.major_version,
80 constant_pool2,
81 cf.access_flags,
82 cf.this_class,
83 cf.super_class,
84 cf.interfaces,
85 fields2,
86 methods2,
87 attributes2);
88 translations.put(cf, cf2);
89 }
90 return cf2;
91 }
93 ConstantPool translate(ConstantPool cp, Map<Object,Object> translations) {
94 ConstantPool cp2 = (ConstantPool) translations.get(cp);
95 if (cp2 == null) {
96 ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
97 boolean eq = true;
98 for (int i = 0; i < cp.size(); i++) {
99 ConstantPool.CPInfo cpInfo;
100 try {
101 cpInfo = cp.get(i);
102 } catch (ConstantPool.InvalidIndex e) {
103 throw new IllegalStateException(e);
104 }
105 ConstantPool.CPInfo cpInfo2 = translate(cpInfo, translations);
106 eq &= (cpInfo == cpInfo2);
107 pool2[i] = cpInfo2;
108 if (cpInfo.getTag() != cpInfo2.getTag())
109 throw new IllegalStateException();
110 switch (cpInfo.getTag()) {
111 case ConstantPool.CONSTANT_Double:
112 case ConstantPool.CONSTANT_Long:
113 i += 1;
114 }
115 }
117 if (eq)
118 cp2 = cp;
119 else
120 cp2 = new ConstantPool(pool2);
122 translations.put(cp, cp2);
123 }
124 return cp2;
125 }
127 ConstantPool.CPInfo translate(ConstantPool.CPInfo cpInfo, Map<Object,Object> translations) {
128 ConstantPool.CPInfo cpInfo2 = (ConstantPool.CPInfo) translations.get(cpInfo);
129 if (cpInfo2 == null) {
130 cpInfo2 = cpInfo.accept(this, translations);
131 translations.put(cpInfo, cpInfo2);
132 }
133 return cpInfo2;
134 }
136 Field[] translate(Field[] fields, ConstantPool constant_pool, Map<Object,Object> translations) {
137 Field[] fields2 = (Field[]) translations.get(fields);
138 if (fields2 == null) {
139 fields2 = new Field[fields.length];
140 for (int i = 0; i < fields.length; i++)
141 fields2[i] = translate(fields[i], constant_pool, translations);
142 if (equal(fields, fields2))
143 fields2 = fields;
144 translations.put(fields, fields2);
145 }
146 return fields2;
147 }
149 Field translate(Field field, ConstantPool constant_pool, Map<Object,Object> translations) {
150 Field field2 = (Field) translations.get(field);
151 if (field2 == null) {
152 Attributes attributes2 = translateAttributes(field.attributes, constant_pool,
153 translations);
155 if (attributes2 == field.attributes)
156 field2 = field;
157 else
158 field2 = new Field(
159 field.access_flags,
160 field.name_index,
161 field.descriptor,
162 attributes2);
163 translations.put(field, field2);
164 }
165 return field2;
166 }
168 Method[] translateMethods(Method[] methods, ConstantPool constant_pool, Map<Object,Object> translations) {
169 Method[] methods2 = (Method[]) translations.get(methods);
170 if (methods2 == null) {
171 methods2 = new Method[methods.length];
172 for (int i = 0; i < methods.length; i++)
173 methods2[i] = translate(methods[i], constant_pool, translations);
174 if (equal(methods, methods2))
175 methods2 = methods;
176 translations.put(methods, methods2);
177 }
178 return methods2;
179 }
181 Method translate(Method method, ConstantPool constant_pool, Map<Object,Object> translations) {
182 Method method2 = (Method) translations.get(method);
183 if (method2 == null) {
184 Attributes attributes2 = translateAttributes(method.attributes, constant_pool,
185 translations);
187 if (attributes2 == method.attributes)
188 method2 = method;
189 else
190 method2 = new Method(
191 method.access_flags,
192 method.name_index,
193 method.descriptor,
194 attributes2);
195 translations.put(method, method2);
196 }
197 return method2;
198 }
200 Attributes translateAttributes(Attributes attributes,
201 ConstantPool constant_pool, Map<Object,Object> translations) {
202 Attributes attributes2 = (Attributes) translations.get(attributes);
203 if (attributes2 == null) {
204 Attribute[] attrArray2 = new Attribute[attributes.size()];
205 ConstantPool constant_pool2 = translate(constant_pool, translations);
206 boolean attrsEqual = true;
207 for (int i = 0; i < attributes.size(); i++) {
208 Attribute attr = attributes.get(i);
209 Attribute attr2 = translate(attr, translations);
210 if (attr2 != attr)
211 attrsEqual = false;
212 attrArray2[i] = attr2;
213 }
214 if ((constant_pool2 == constant_pool) && attrsEqual)
215 attributes2 = attributes;
216 else
217 attributes2 = new Attributes(constant_pool2, attrArray2);
218 translations.put(attributes, attributes2);
219 }
220 return attributes2;
221 }
223 Attribute translate(Attribute attribute, Map<Object,Object> translations) {
224 Attribute attribute2 = (Attribute) translations.get(attribute);
225 if (attribute2 == null) {
226 attribute2 = attribute; // don't support translation within attributes yet
227 // (what about Code attribute)
228 translations.put(attribute, attribute2);
229 }
230 return attribute2;
231 }
233 private static <T> boolean equal(T[] a1, T[] a2) {
234 if (a1 == null || a2 == null)
235 return (a1 == a2);
236 if (a1.length != a2.length)
237 return false;
238 for (int i = 0; i < a1.length; i++) {
239 if (a1[i] != a2[i])
240 return false;
241 }
242 return true;
243 }
245 public CPInfo visitClass(CONSTANT_Class_info info, Map<Object, Object> translations) {
246 CONSTANT_Class_info info2 = (CONSTANT_Class_info) translations.get(info);
247 if (info2 == null) {
248 ConstantPool cp2 = translate(info.cp, translations);
249 if (cp2 == info.cp)
250 info2 = info;
251 else
252 info2 = new CONSTANT_Class_info(cp2, info.name_index);
253 translations.put(info, info2);
254 }
255 return info;
256 }
258 public CPInfo visitDouble(CONSTANT_Double_info info, Map<Object, Object> translations) {
259 CONSTANT_Double_info info2 = (CONSTANT_Double_info) translations.get(info);
260 if (info2 == null) {
261 info2 = info;
262 translations.put(info, info2);
263 }
264 return info;
265 }
267 public CPInfo visitFieldref(CONSTANT_Fieldref_info info, Map<Object, Object> translations) {
268 CONSTANT_Fieldref_info info2 = (CONSTANT_Fieldref_info) translations.get(info);
269 if (info2 == null) {
270 ConstantPool cp2 = translate(info.cp, translations);
271 if (cp2 == info.cp)
272 info2 = info;
273 else
274 info2 = new CONSTANT_Fieldref_info(cp2, info.class_index, info.name_and_type_index);
275 translations.put(info, info2);
276 }
277 return info;
278 }
280 public CPInfo visitFloat(CONSTANT_Float_info info, Map<Object, Object> translations) {
281 CONSTANT_Float_info info2 = (CONSTANT_Float_info) translations.get(info);
282 if (info2 == null) {
283 info2 = info;
284 translations.put(info, info2);
285 }
286 return info;
287 }
289 public CPInfo visitInteger(CONSTANT_Integer_info info, Map<Object, Object> translations) {
290 CONSTANT_Integer_info info2 = (CONSTANT_Integer_info) translations.get(info);
291 if (info2 == null) {
292 info2 = info;
293 translations.put(info, info2);
294 }
295 return info;
296 }
298 public CPInfo visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Map<Object, Object> translations) {
299 CONSTANT_InterfaceMethodref_info info2 = (CONSTANT_InterfaceMethodref_info) translations.get(info);
300 if (info2 == null) {
301 ConstantPool cp2 = translate(info.cp, translations);
302 if (cp2 == info.cp)
303 info2 = info;
304 else
305 info2 = new CONSTANT_InterfaceMethodref_info(cp2, info.class_index, info.name_and_type_index);
306 translations.put(info, info2);
307 }
308 return info;
309 }
311 public CPInfo visitLong(CONSTANT_Long_info info, Map<Object, Object> translations) {
312 CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info);
313 if (info2 == null) {
314 info2 = info;
315 translations.put(info, info2);
316 }
317 return info;
318 }
320 public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map<Object, Object> translations) {
321 CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_info) translations.get(info);
322 if (info2 == null) {
323 ConstantPool cp2 = translate(info.cp, translations);
324 if (cp2 == info.cp)
325 info2 = info;
326 else
327 info2 = new CONSTANT_NameAndType_info(cp2, info.name_index, info.type_index);
328 translations.put(info, info2);
329 }
330 return info;
331 }
333 public CPInfo visitMethodref(CONSTANT_Methodref_info info, Map<Object, Object> translations) {
334 CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_info) translations.get(info);
335 if (info2 == null) {
336 ConstantPool cp2 = translate(info.cp, translations);
337 if (cp2 == info.cp)
338 info2 = info;
339 else
340 info2 = new CONSTANT_Methodref_info(cp2, info.class_index, info.name_and_type_index);
341 translations.put(info, info2);
342 }
343 return info;
344 }
346 public CPInfo visitString(CONSTANT_String_info info, Map<Object, Object> translations) {
347 CONSTANT_String_info info2 = (CONSTANT_String_info) translations.get(info);
348 if (info2 == null) {
349 ConstantPool cp2 = translate(info.cp, translations);
350 if (cp2 == info.cp)
351 info2 = info;
352 else
353 info2 = new CONSTANT_String_info(cp2, info.string_index);
354 translations.put(info, info2);
355 }
356 return info;
357 }
359 public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map<Object, Object> translations) {
360 CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info);
361 if (info2 == null) {
362 info2 = info;
363 translations.put(info, info2);
364 }
365 return info;
366 }
368 }