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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1013
8eb952f43b11
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial