src/share/classes/com/sun/tools/classfile/ClassTranslator.java

Wed, 02 Mar 2011 21:13:55 -0800

author
jjg
date
Wed, 02 Mar 2011 21:13:55 -0800
changeset 904
4baab658f357
parent 826
5cf6c432ef2f
child 1013
8eb952f43b11
permissions
-rw-r--r--

6639645: Modeling type implementing missing interfaces
Reviewed-by: darcy, mcimadamore

jjg@46 1 /*
ksrini@826 2 * Copyright (c) 2008, 2011 Oracle and/or its affiliates. All rights reserved.
jjg@46 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 4 *
jjg@46 5 * This code is free software; you can redistribute it and/or modify it
jjg@46 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@46 10 *
jjg@46 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 15 * accompanied this code).
jjg@46 16 *
jjg@46 17 * You should have received a copy of the GNU General Public License version
jjg@46 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@46 24 */
jjg@46 25
jjg@46 26 package com.sun.tools.classfile;
jjg@46 27
jjg@46 28 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
jjg@46 29 import com.sun.tools.classfile.ConstantPool.CONSTANT_Double_info;
jjg@46 30 import com.sun.tools.classfile.ConstantPool.CONSTANT_Fieldref_info;
jjg@46 31 import com.sun.tools.classfile.ConstantPool.CONSTANT_Float_info;
jjg@46 32 import com.sun.tools.classfile.ConstantPool.CONSTANT_Integer_info;
jjg@46 33 import com.sun.tools.classfile.ConstantPool.CONSTANT_InterfaceMethodref_info;
ksrini@826 34 import com.sun.tools.classfile.ConstantPool.CONSTANT_InvokeDynamic_info;
jjg@46 35 import com.sun.tools.classfile.ConstantPool.CONSTANT_Long_info;
ksrini@826 36 import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodHandle_info;
ksrini@826 37 import com.sun.tools.classfile.ConstantPool.CONSTANT_MethodType_info;
jjg@46 38 import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
jjg@46 39 import com.sun.tools.classfile.ConstantPool.CONSTANT_NameAndType_info;
jjg@46 40 import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info;
jjg@46 41 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
jjg@46 42 import com.sun.tools.classfile.ConstantPool.CPInfo;
jjg@46 43 import java.util.Map;
jjg@46 44
jjg@46 45 /**
jjg@46 46 * Rewrites a class file using a map of translations.
jjg@46 47 *
jjg@581 48 * <p><b>This is NOT part of any supported API.
jjg@581 49 * If you write code that depends on this, you do so at your own risk.
jjg@46 50 * This code and its internal interfaces are subject to change or
jjg@46 51 * deletion without notice.</b>
jjg@46 52 */
jjg@46 53 public class ClassTranslator
jjg@46 54 implements ConstantPool.Visitor<ConstantPool.CPInfo,Map<Object,Object>> {
jjg@46 55 /**
jjg@46 56 * Create a new ClassFile from {@code cf}, such that for all entries
jjg@46 57 * {@code k&nbsp;-\&gt;&nbsp;v} in {@code translations},
jjg@46 58 * each occurrence of {@code k} in {@code cf} will be replaced by {@code v}.
jjg@46 59 * in
jjg@46 60 * @param cf the class file to be processed
jjg@46 61 * @param translations the set of translations to be applied
jjg@46 62 * @return a copy of {@code} with the values in {@code translations} substituted
jjg@46 63 */
jjg@46 64 public ClassFile translate(ClassFile cf, Map<Object,Object> translations) {
jjg@46 65 ClassFile cf2 = (ClassFile) translations.get(cf);
jjg@46 66 if (cf2 == null) {
jjg@46 67 ConstantPool constant_pool2 = translate(cf.constant_pool, translations);
jjg@46 68 Field[] fields2 = translate(cf.fields, cf.constant_pool, translations);
jjg@46 69 Method[] methods2 = translateMethods(cf.methods, cf.constant_pool, translations);
jjg@46 70 Attributes attributes2 = translateAttributes(cf.attributes, cf.constant_pool,
jjg@46 71 translations);
jjg@46 72
jjg@46 73 if (constant_pool2 == cf.constant_pool &&
jjg@46 74 fields2 == cf.fields &&
jjg@46 75 methods2 == cf.methods &&
jjg@46 76 attributes2 == cf.attributes)
jjg@46 77 cf2 = cf;
jjg@46 78 else
jjg@46 79 cf2 = new ClassFile(
jjg@46 80 cf.magic,
jjg@46 81 cf.minor_version,
jjg@46 82 cf.major_version,
jjg@46 83 constant_pool2,
jjg@46 84 cf.access_flags,
jjg@46 85 cf.this_class,
jjg@46 86 cf.super_class,
jjg@46 87 cf.interfaces,
jjg@46 88 fields2,
jjg@46 89 methods2,
jjg@46 90 attributes2);
jjg@46 91 translations.put(cf, cf2);
jjg@46 92 }
jjg@46 93 return cf2;
jjg@46 94 }
jjg@46 95
jjg@46 96 ConstantPool translate(ConstantPool cp, Map<Object,Object> translations) {
jjg@46 97 ConstantPool cp2 = (ConstantPool) translations.get(cp);
jjg@46 98 if (cp2 == null) {
jjg@46 99 ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
jjg@46 100 boolean eq = true;
jjg@282 101 for (int i = 0; i < cp.size(); ) {
jjg@46 102 ConstantPool.CPInfo cpInfo;
jjg@46 103 try {
jjg@46 104 cpInfo = cp.get(i);
jjg@46 105 } catch (ConstantPool.InvalidIndex e) {
jjg@46 106 throw new IllegalStateException(e);
jjg@46 107 }
jjg@46 108 ConstantPool.CPInfo cpInfo2 = translate(cpInfo, translations);
jjg@46 109 eq &= (cpInfo == cpInfo2);
jjg@46 110 pool2[i] = cpInfo2;
jjg@46 111 if (cpInfo.getTag() != cpInfo2.getTag())
jjg@46 112 throw new IllegalStateException();
jjg@282 113 i += cpInfo.size();
jjg@46 114 }
jjg@46 115
jjg@46 116 if (eq)
jjg@46 117 cp2 = cp;
jjg@46 118 else
jjg@46 119 cp2 = new ConstantPool(pool2);
jjg@46 120
jjg@46 121 translations.put(cp, cp2);
jjg@46 122 }
jjg@46 123 return cp2;
jjg@46 124 }
jjg@46 125
jjg@46 126 ConstantPool.CPInfo translate(ConstantPool.CPInfo cpInfo, Map<Object,Object> translations) {
jjg@46 127 ConstantPool.CPInfo cpInfo2 = (ConstantPool.CPInfo) translations.get(cpInfo);
jjg@46 128 if (cpInfo2 == null) {
jjg@46 129 cpInfo2 = cpInfo.accept(this, translations);
jjg@46 130 translations.put(cpInfo, cpInfo2);
jjg@46 131 }
jjg@46 132 return cpInfo2;
jjg@46 133 }
jjg@46 134
jjg@46 135 Field[] translate(Field[] fields, ConstantPool constant_pool, Map<Object,Object> translations) {
jjg@46 136 Field[] fields2 = (Field[]) translations.get(fields);
jjg@46 137 if (fields2 == null) {
jjg@46 138 fields2 = new Field[fields.length];
jjg@46 139 for (int i = 0; i < fields.length; i++)
jjg@46 140 fields2[i] = translate(fields[i], constant_pool, translations);
jjg@46 141 if (equal(fields, fields2))
jjg@46 142 fields2 = fields;
jjg@46 143 translations.put(fields, fields2);
jjg@46 144 }
jjg@46 145 return fields2;
jjg@46 146 }
jjg@46 147
jjg@46 148 Field translate(Field field, ConstantPool constant_pool, Map<Object,Object> translations) {
jjg@46 149 Field field2 = (Field) translations.get(field);
jjg@46 150 if (field2 == null) {
jjg@46 151 Attributes attributes2 = translateAttributes(field.attributes, constant_pool,
jjg@46 152 translations);
jjg@46 153
jjg@46 154 if (attributes2 == field.attributes)
jjg@46 155 field2 = field;
jjg@46 156 else
jjg@46 157 field2 = new Field(
jjg@46 158 field.access_flags,
jjg@46 159 field.name_index,
jjg@46 160 field.descriptor,
jjg@46 161 attributes2);
jjg@46 162 translations.put(field, field2);
jjg@46 163 }
jjg@46 164 return field2;
jjg@46 165 }
jjg@46 166
jjg@46 167 Method[] translateMethods(Method[] methods, ConstantPool constant_pool, Map<Object,Object> translations) {
jjg@46 168 Method[] methods2 = (Method[]) translations.get(methods);
jjg@46 169 if (methods2 == null) {
jjg@46 170 methods2 = new Method[methods.length];
jjg@46 171 for (int i = 0; i < methods.length; i++)
jjg@46 172 methods2[i] = translate(methods[i], constant_pool, translations);
jjg@46 173 if (equal(methods, methods2))
jjg@46 174 methods2 = methods;
jjg@46 175 translations.put(methods, methods2);
jjg@46 176 }
jjg@46 177 return methods2;
jjg@46 178 }
jjg@46 179
jjg@46 180 Method translate(Method method, ConstantPool constant_pool, Map<Object,Object> translations) {
jjg@46 181 Method method2 = (Method) translations.get(method);
jjg@46 182 if (method2 == null) {
jjg@46 183 Attributes attributes2 = translateAttributes(method.attributes, constant_pool,
jjg@46 184 translations);
jjg@46 185
jjg@46 186 if (attributes2 == method.attributes)
jjg@46 187 method2 = method;
jjg@46 188 else
jjg@46 189 method2 = new Method(
jjg@46 190 method.access_flags,
jjg@46 191 method.name_index,
jjg@46 192 method.descriptor,
jjg@46 193 attributes2);
jjg@46 194 translations.put(method, method2);
jjg@46 195 }
jjg@46 196 return method2;
jjg@46 197 }
jjg@46 198
jjg@46 199 Attributes translateAttributes(Attributes attributes,
jjg@46 200 ConstantPool constant_pool, Map<Object,Object> translations) {
jjg@46 201 Attributes attributes2 = (Attributes) translations.get(attributes);
jjg@46 202 if (attributes2 == null) {
jjg@46 203 Attribute[] attrArray2 = new Attribute[attributes.size()];
jjg@46 204 ConstantPool constant_pool2 = translate(constant_pool, translations);
jjg@46 205 boolean attrsEqual = true;
jjg@46 206 for (int i = 0; i < attributes.size(); i++) {
jjg@46 207 Attribute attr = attributes.get(i);
jjg@46 208 Attribute attr2 = translate(attr, translations);
jjg@46 209 if (attr2 != attr)
jjg@46 210 attrsEqual = false;
jjg@46 211 attrArray2[i] = attr2;
jjg@46 212 }
jjg@46 213 if ((constant_pool2 == constant_pool) && attrsEqual)
jjg@46 214 attributes2 = attributes;
jjg@46 215 else
jjg@46 216 attributes2 = new Attributes(constant_pool2, attrArray2);
jjg@46 217 translations.put(attributes, attributes2);
jjg@46 218 }
jjg@46 219 return attributes2;
jjg@46 220 }
jjg@46 221
jjg@46 222 Attribute translate(Attribute attribute, Map<Object,Object> translations) {
jjg@46 223 Attribute attribute2 = (Attribute) translations.get(attribute);
jjg@46 224 if (attribute2 == null) {
jjg@46 225 attribute2 = attribute; // don't support translation within attributes yet
jjg@46 226 // (what about Code attribute)
jjg@46 227 translations.put(attribute, attribute2);
jjg@46 228 }
jjg@46 229 return attribute2;
jjg@46 230 }
jjg@46 231
jjg@46 232 private static <T> boolean equal(T[] a1, T[] a2) {
jjg@46 233 if (a1 == null || a2 == null)
jjg@46 234 return (a1 == a2);
jjg@46 235 if (a1.length != a2.length)
jjg@46 236 return false;
jjg@46 237 for (int i = 0; i < a1.length; i++) {
jjg@46 238 if (a1[i] != a2[i])
jjg@46 239 return false;
jjg@46 240 }
jjg@46 241 return true;
jjg@46 242 }
jjg@46 243
jjg@46 244 public CPInfo visitClass(CONSTANT_Class_info info, Map<Object, Object> translations) {
jjg@46 245 CONSTANT_Class_info info2 = (CONSTANT_Class_info) translations.get(info);
jjg@46 246 if (info2 == null) {
jjg@46 247 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 248 if (cp2 == info.cp)
jjg@46 249 info2 = info;
jjg@46 250 else
jjg@46 251 info2 = new CONSTANT_Class_info(cp2, info.name_index);
jjg@46 252 translations.put(info, info2);
jjg@46 253 }
jjg@46 254 return info;
jjg@46 255 }
jjg@46 256
jjg@46 257 public CPInfo visitDouble(CONSTANT_Double_info info, Map<Object, Object> translations) {
jjg@46 258 CONSTANT_Double_info info2 = (CONSTANT_Double_info) translations.get(info);
jjg@46 259 if (info2 == null) {
jjg@46 260 info2 = info;
jjg@46 261 translations.put(info, info2);
jjg@46 262 }
jjg@46 263 return info;
jjg@46 264 }
jjg@46 265
jjg@46 266 public CPInfo visitFieldref(CONSTANT_Fieldref_info info, Map<Object, Object> translations) {
jjg@46 267 CONSTANT_Fieldref_info info2 = (CONSTANT_Fieldref_info) translations.get(info);
jjg@46 268 if (info2 == null) {
jjg@46 269 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 270 if (cp2 == info.cp)
jjg@46 271 info2 = info;
jjg@46 272 else
jjg@46 273 info2 = new CONSTANT_Fieldref_info(cp2, info.class_index, info.name_and_type_index);
jjg@46 274 translations.put(info, info2);
jjg@46 275 }
jjg@46 276 return info;
jjg@46 277 }
jjg@46 278
jjg@46 279 public CPInfo visitFloat(CONSTANT_Float_info info, Map<Object, Object> translations) {
jjg@46 280 CONSTANT_Float_info info2 = (CONSTANT_Float_info) translations.get(info);
jjg@46 281 if (info2 == null) {
jjg@46 282 info2 = info;
jjg@46 283 translations.put(info, info2);
jjg@46 284 }
jjg@46 285 return info;
jjg@46 286 }
jjg@46 287
jjg@46 288 public CPInfo visitInteger(CONSTANT_Integer_info info, Map<Object, Object> translations) {
jjg@46 289 CONSTANT_Integer_info info2 = (CONSTANT_Integer_info) translations.get(info);
jjg@46 290 if (info2 == null) {
jjg@46 291 info2 = info;
jjg@46 292 translations.put(info, info2);
jjg@46 293 }
jjg@46 294 return info;
jjg@46 295 }
jjg@46 296
jjg@46 297 public CPInfo visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Map<Object, Object> translations) {
jjg@46 298 CONSTANT_InterfaceMethodref_info info2 = (CONSTANT_InterfaceMethodref_info) translations.get(info);
jjg@46 299 if (info2 == null) {
jjg@46 300 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 301 if (cp2 == info.cp)
jjg@46 302 info2 = info;
jjg@46 303 else
jjg@46 304 info2 = new CONSTANT_InterfaceMethodref_info(cp2, info.class_index, info.name_and_type_index);
jjg@46 305 translations.put(info, info2);
jjg@46 306 }
jjg@46 307 return info;
jjg@46 308 }
jjg@46 309
ksrini@826 310 public CPInfo visitInvokeDynamic(CONSTANT_InvokeDynamic_info info, Map<Object, Object> translations) {
ksrini@826 311 CONSTANT_InvokeDynamic_info info2 = (CONSTANT_InvokeDynamic_info) translations.get(info);
ksrini@826 312 if (info2 == null) {
ksrini@826 313 ConstantPool cp2 = translate(info.cp, translations);
ksrini@826 314 if (cp2 == info.cp) {
ksrini@826 315 info2 = info;
ksrini@826 316 } else {
ksrini@826 317 info2 = new CONSTANT_InvokeDynamic_info(cp2, info.bootstrap_method_attr_index, info.name_and_type_index);
ksrini@826 318 }
ksrini@826 319 translations.put(info, info2);
ksrini@826 320 }
ksrini@826 321 return info;
ksrini@826 322 }
ksrini@826 323
jjg@46 324 public CPInfo visitLong(CONSTANT_Long_info info, Map<Object, Object> translations) {
jjg@46 325 CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info);
jjg@46 326 if (info2 == null) {
jjg@46 327 info2 = info;
jjg@46 328 translations.put(info, info2);
jjg@46 329 }
jjg@46 330 return info;
jjg@46 331 }
jjg@46 332
jjg@46 333 public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map<Object, Object> translations) {
jjg@46 334 CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_info) translations.get(info);
jjg@46 335 if (info2 == null) {
jjg@46 336 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 337 if (cp2 == info.cp)
jjg@46 338 info2 = info;
jjg@46 339 else
jjg@46 340 info2 = new CONSTANT_NameAndType_info(cp2, info.name_index, info.type_index);
jjg@46 341 translations.put(info, info2);
jjg@46 342 }
jjg@46 343 return info;
jjg@46 344 }
jjg@46 345
jjg@46 346 public CPInfo visitMethodref(CONSTANT_Methodref_info info, Map<Object, Object> translations) {
jjg@46 347 CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_info) translations.get(info);
jjg@46 348 if (info2 == null) {
jjg@46 349 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 350 if (cp2 == info.cp)
jjg@46 351 info2 = info;
jjg@46 352 else
jjg@46 353 info2 = new CONSTANT_Methodref_info(cp2, info.class_index, info.name_and_type_index);
jjg@46 354 translations.put(info, info2);
jjg@46 355 }
jjg@46 356 return info;
jjg@46 357 }
jjg@46 358
ksrini@826 359 public CPInfo visitMethodHandle(CONSTANT_MethodHandle_info info, Map<Object, Object> translations) {
ksrini@826 360 CONSTANT_MethodHandle_info info2 = (CONSTANT_MethodHandle_info) translations.get(info);
ksrini@826 361 if (info2 == null) {
ksrini@826 362 ConstantPool cp2 = translate(info.cp, translations);
ksrini@826 363 if (cp2 == info.cp) {
ksrini@826 364 info2 = info;
ksrini@826 365 } else {
ksrini@826 366 info2 = new CONSTANT_MethodHandle_info(cp2, info.reference_kind, info.reference_index);
ksrini@826 367 }
ksrini@826 368 translations.put(info, info2);
ksrini@826 369 }
ksrini@826 370 return info;
ksrini@826 371 }
ksrini@826 372
ksrini@826 373 public CPInfo visitMethodType(CONSTANT_MethodType_info info, Map<Object, Object> translations) {
ksrini@826 374 CONSTANT_MethodType_info info2 = (CONSTANT_MethodType_info) translations.get(info);
ksrini@826 375 if (info2 == null) {
ksrini@826 376 ConstantPool cp2 = translate(info.cp, translations);
ksrini@826 377 if (cp2 == info.cp) {
ksrini@826 378 info2 = info;
ksrini@826 379 } else {
ksrini@826 380 info2 = new CONSTANT_MethodType_info(cp2, info.descriptor_index);
ksrini@826 381 }
ksrini@826 382 translations.put(info, info2);
ksrini@826 383 }
ksrini@826 384 return info;
ksrini@826 385 }
ksrini@826 386
jjg@46 387 public CPInfo visitString(CONSTANT_String_info info, Map<Object, Object> translations) {
jjg@46 388 CONSTANT_String_info info2 = (CONSTANT_String_info) translations.get(info);
jjg@46 389 if (info2 == null) {
jjg@46 390 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 391 if (cp2 == info.cp)
jjg@46 392 info2 = info;
jjg@46 393 else
jjg@46 394 info2 = new CONSTANT_String_info(cp2, info.string_index);
jjg@46 395 translations.put(info, info2);
jjg@46 396 }
jjg@46 397 return info;
jjg@46 398 }
jjg@46 399
jjg@46 400 public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map<Object, Object> translations) {
jjg@46 401 CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info);
jjg@46 402 if (info2 == null) {
jjg@46 403 info2 = info;
jjg@46 404 translations.put(info, info2);
jjg@46 405 }
jjg@46 406 return info;
jjg@46 407 }
jjg@46 408
jjg@46 409 }

mercurial