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

Tue, 19 May 2009 11:43:50 -0700

author
jjg
date
Tue, 19 May 2009 11:43:50 -0700
changeset 282
fc634a593812
parent 46
7708bd6d800d
child 554
9d9f26857129
permissions
-rw-r--r--

6841419: classfile: add constant pool iterator
Reviewed-by: mcimadamore

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

mercurial