Mon, 28 Feb 2011 11:48:53 +0000
7015430: Incorrect thrown type determined for unchecked invocations
Summary: Thrown types do not get updated after 15.12.2.8, and do not get erased as per 15.12.2.6
Reviewed-by: jjg, dlsmith
1 /*
2 * Copyright (c) 2008, 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.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_InvokeDynamic_info;
35 import com.sun.tools.classfile.ConstantPool.CONSTANT_Long_info;
36 import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodHandle_info;
37 import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodType_info;
38 import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
39 import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info;
40 import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info;
41 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
42 import com.sun.tools.classfile.ConstantPool.CPInfo;
43 import java.util.Map;
45 /**
46 * Rewrites a class file using a map of translations.
47 *
48 * <p><b>This is NOT part of any supported API.
49 * If you write code that depends on this, you do so at your own risk.
50 * This code and its internal interfaces are subject to change or
51 * deletion without notice.</b>
52 */
53 public class ClassTranslator
54 implements ConstantPool.Visitor<ConstantPool.CPInfo,Map<Object,Object>> {
55 /**
56 * Create a new ClassFile from {@code cf}, such that for all entries
57 * {@code k -\> v} in {@code translations},
58 * each occurrence of {@code k} in {@code cf} will be replaced by {@code v}.
59 * in
60 * @param cf the class file to be processed
61 * @param translations the set of translations to be applied
62 * @return a copy of {@code} with the values in {@code translations} substituted
63 */
64 public ClassFile translate(ClassFile cf, Map<Object,Object> translations) {
65 ClassFile cf2 = (ClassFile) translations.get(cf);
66 if (cf2 == null) {
67 ConstantPool constant_pool2 = translate(cf.constant_pool, translations);
68 Field[] fields2 = translate(cf.fields, cf.constant_pool, translations);
69 Method[] methods2 = translateMethods(cf.methods, cf.constant_pool, translations);
70 Attributes attributes2 = translateAttributes(cf.attributes, cf.constant_pool,
71 translations);
73 if (constant_pool2 == cf.constant_pool &&
74 fields2 == cf.fields &&
75 methods2 == cf.methods &&
76 attributes2 == cf.attributes)
77 cf2 = cf;
78 else
79 cf2 = new ClassFile(
80 cf.magic,
81 cf.minor_version,
82 cf.major_version,
83 constant_pool2,
84 cf.access_flags,
85 cf.this_class,
86 cf.super_class,
87 cf.interfaces,
88 fields2,
89 methods2,
90 attributes2);
91 translations.put(cf, cf2);
92 }
93 return cf2;
94 }
96 ConstantPool translate(ConstantPool cp, Map<Object,Object> translations) {
97 ConstantPool cp2 = (ConstantPool) translations.get(cp);
98 if (cp2 == null) {
99 ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
100 boolean eq = true;
101 for (int i = 0; i < cp.size(); ) {
102 ConstantPool.CPInfo cpInfo;
103 try {
104 cpInfo = cp.get(i);
105 } catch (ConstantPool.InvalidIndex e) {
106 throw new IllegalStateException(e);
107 }
108 ConstantPool.CPInfo cpInfo2 = translate(cpInfo, translations);
109 eq &= (cpInfo == cpInfo2);
110 pool2[i] = cpInfo2;
111 if (cpInfo.getTag() != cpInfo2.getTag())
112 throw new IllegalStateException();
113 i += cpInfo.size();
114 }
116 if (eq)
117 cp2 = cp;
118 else
119 cp2 = new ConstantPool(pool2);
121 translations.put(cp, cp2);
122 }
123 return cp2;
124 }
126 ConstantPool.CPInfo translate(ConstantPool.CPInfo cpInfo, Map<Object,Object> translations) {
127 ConstantPool.CPInfo cpInfo2 = (ConstantPool.CPInfo) translations.get(cpInfo);
128 if (cpInfo2 == null) {
129 cpInfo2 = cpInfo.accept(this, translations);
130 translations.put(cpInfo, cpInfo2);
131 }
132 return cpInfo2;
133 }
135 Field[] translate(Field[] fields, ConstantPool constant_pool, Map<Object,Object> translations) {
136 Field[] fields2 = (Field[]) translations.get(fields);
137 if (fields2 == null) {
138 fields2 = new Field[fields.length];
139 for (int i = 0; i < fields.length; i++)
140 fields2[i] = translate(fields[i], constant_pool, translations);
141 if (equal(fields, fields2))
142 fields2 = fields;
143 translations.put(fields, fields2);
144 }
145 return fields2;
146 }
148 Field translate(Field field, ConstantPool constant_pool, Map<Object,Object> translations) {
149 Field field2 = (Field) translations.get(field);
150 if (field2 == null) {
151 Attributes attributes2 = translateAttributes(field.attributes, constant_pool,
152 translations);
154 if (attributes2 == field.attributes)
155 field2 = field;
156 else
157 field2 = new Field(
158 field.access_flags,
159 field.name_index,
160 field.descriptor,
161 attributes2);
162 translations.put(field, field2);
163 }
164 return field2;
165 }
167 Method[] translateMethods(Method[] methods, ConstantPool constant_pool, Map<Object,Object> translations) {
168 Method[] methods2 = (Method[]) translations.get(methods);
169 if (methods2 == null) {
170 methods2 = new Method[methods.length];
171 for (int i = 0; i < methods.length; i++)
172 methods2[i] = translate(methods[i], constant_pool, translations);
173 if (equal(methods, methods2))
174 methods2 = methods;
175 translations.put(methods, methods2);
176 }
177 return methods2;
178 }
180 Method translate(Method method, ConstantPool constant_pool, Map<Object,Object> translations) {
181 Method method2 = (Method) translations.get(method);
182 if (method2 == null) {
183 Attributes attributes2 = translateAttributes(method.attributes, constant_pool,
184 translations);
186 if (attributes2 == method.attributes)
187 method2 = method;
188 else
189 method2 = new Method(
190 method.access_flags,
191 method.name_index,
192 method.descriptor,
193 attributes2);
194 translations.put(method, method2);
195 }
196 return method2;
197 }
199 Attributes translateAttributes(Attributes attributes,
200 ConstantPool constant_pool, Map<Object,Object> translations) {
201 Attributes attributes2 = (Attributes) translations.get(attributes);
202 if (attributes2 == null) {
203 Attribute[] attrArray2 = new Attribute[attributes.size()];
204 ConstantPool constant_pool2 = translate(constant_pool, translations);
205 boolean attrsEqual = true;
206 for (int i = 0; i < attributes.size(); i++) {
207 Attribute attr = attributes.get(i);
208 Attribute attr2 = translate(attr, translations);
209 if (attr2 != attr)
210 attrsEqual = false;
211 attrArray2[i] = attr2;
212 }
213 if ((constant_pool2 == constant_pool) && attrsEqual)
214 attributes2 = attributes;
215 else
216 attributes2 = new Attributes(constant_pool2, attrArray2);
217 translations.put(attributes, attributes2);
218 }
219 return attributes2;
220 }
222 Attribute translate(Attribute attribute, Map<Object,Object> translations) {
223 Attribute attribute2 = (Attribute) translations.get(attribute);
224 if (attribute2 == null) {
225 attribute2 = attribute; // don't support translation within attributes yet
226 // (what about Code attribute)
227 translations.put(attribute, attribute2);
228 }
229 return attribute2;
230 }
232 private static <T> boolean equal(T[] a1, T[] a2) {
233 if (a1 == null || a2 == null)
234 return (a1 == a2);
235 if (a1.length != a2.length)
236 return false;
237 for (int i = 0; i < a1.length; i++) {
238 if (a1[i] != a2[i])
239 return false;
240 }
241 return true;
242 }
244 public CPInfo visitClass(CONSTANT_Class_info info, Map<Object, Object> translations) {
245 CONSTANT_Class_info info2 = (CONSTANT_Class_info) translations.get(info);
246 if (info2 == null) {
247 ConstantPool cp2 = translate(info.cp, translations);
248 if (cp2 == info.cp)
249 info2 = info;
250 else
251 info2 = new CONSTANT_Class_info(cp2, info.name_index);
252 translations.put(info, info2);
253 }
254 return info;
255 }
257 public CPInfo visitDouble(CONSTANT_Double_info info, Map<Object, Object> translations) {
258 CONSTANT_Double_info info2 = (CONSTANT_Double_info) translations.get(info);
259 if (info2 == null) {
260 info2 = info;
261 translations.put(info, info2);
262 }
263 return info;
264 }
266 public CPInfo visitFieldref(CONSTANT_Fieldref_info info, Map<Object, Object> translations) {
267 CONSTANT_Fieldref_info info2 = (CONSTANT_Fieldref_info) translations.get(info);
268 if (info2 == null) {
269 ConstantPool cp2 = translate(info.cp, translations);
270 if (cp2 == info.cp)
271 info2 = info;
272 else
273 info2 = new CONSTANT_Fieldref_info(cp2, info.class_index, info.name_and_type_index);
274 translations.put(info, info2);
275 }
276 return info;
277 }
279 public CPInfo visitFloat(CONSTANT_Float_info info, Map<Object, Object> translations) {
280 CONSTANT_Float_info info2 = (CONSTANT_Float_info) translations.get(info);
281 if (info2 == null) {
282 info2 = info;
283 translations.put(info, info2);
284 }
285 return info;
286 }
288 public CPInfo visitInteger(CONSTANT_Integer_info info, Map<Object, Object> translations) {
289 CONSTANT_Integer_info info2 = (CONSTANT_Integer_info) translations.get(info);
290 if (info2 == null) {
291 info2 = info;
292 translations.put(info, info2);
293 }
294 return info;
295 }
297 public CPInfo visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Map<Object, Object> translations) {
298 CONSTANT_InterfaceMethodref_info info2 = (CONSTANT_InterfaceMethodref_info) translations.get(info);
299 if (info2 == null) {
300 ConstantPool cp2 = translate(info.cp, translations);
301 if (cp2 == info.cp)
302 info2 = info;
303 else
304 info2 = new CONSTANT_InterfaceMethodref_info(cp2, info.class_index, info.name_and_type_index);
305 translations.put(info, info2);
306 }
307 return info;
308 }
310 public CPInfo visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Map<Object, Object> translations) {
311 CONSTANT_InvokeDynamic_info info2 = (CONSTANT_InvokeDynamic_info) translations.get(info);
312 if (info2 == null) {
313 ConstantPool cp2 = translate(info.cp, translations);
314 if (cp2 == info.cp) {
315 info2 = info;
316 } else {
317 info2 = new CONSTANT_InvokeDynamic_info(cp2, info.bootstrap_method_attr_index, info.name_and_type_index);
318 }
319 translations.put(info, info2);
320 }
321 return info;
322 }
324 public CPInfo visitLong(CONSTANT_Long_info info, Map<Object, Object> translations) {
325 CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info);
326 if (info2 == null) {
327 info2 = info;
328 translations.put(info, info2);
329 }
330 return info;
331 }
333 public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map<Object, Object> translations) {
334 CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_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_NameAndType_info(cp2, info.name_index, info.type_index);
341 translations.put(info, info2);
342 }
343 return info;
344 }
346 public CPInfo visitMethodref(CONSTANT_Methodref_info info, Map<Object, Object> translations) {
347 CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_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_Methodref_info(cp2, info.class_index, info.name_and_type_index);
354 translations.put(info, info2);
355 }
356 return info;
357 }
359 public CPInfo visitMethodHandle(CONSTANT_MethodHandle_info info, Map<Object, Object> translations) {
360 CONSTANT_MethodHandle_info info2 = (CONSTANT_MethodHandle_info) translations.get(info);
361 if (info2 == null) {
362 ConstantPool cp2 = translate(info.cp, translations);
363 if (cp2 == info.cp) {
364 info2 = info;
365 } else {
366 info2 = new CONSTANT_MethodHandle_info(cp2, info.reference_kind, info.reference_index);
367 }
368 translations.put(info, info2);
369 }
370 return info;
371 }
373 public CPInfo visitMethodType(CONSTANT_MethodType_info info, Map<Object, Object> translations) {
374 CONSTANT_MethodType_info info2 = (CONSTANT_MethodType_info) translations.get(info);
375 if (info2 == null) {
376 ConstantPool cp2 = translate(info.cp, translations);
377 if (cp2 == info.cp) {
378 info2 = info;
379 } else {
380 info2 = new CONSTANT_MethodType_info(cp2, info.descriptor_index);
381 }
382 translations.put(info, info2);
383 }
384 return info;
385 }
387 public CPInfo visitString(CONSTANT_String_info info, Map<Object, Object> translations) {
388 CONSTANT_String_info info2 = (CONSTANT_String_info) translations.get(info);
389 if (info2 == null) {
390 ConstantPool cp2 = translate(info.cp, translations);
391 if (cp2 == info.cp)
392 info2 = info;
393 else
394 info2 = new CONSTANT_String_info(cp2, info.string_index);
395 translations.put(info, info2);
396 }
397 return info;
398 }
400 public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map<Object, Object> translations) {
401 CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info);
402 if (info2 == null) {
403 info2 = info;
404 translations.put(info, info2);
405 }
406 return info;
407 }
409 }