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

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 282
fc634a593812
permissions
-rw-r--r--

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

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@46 98 for (int i = 0; i < cp.size(); i++) {
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@46 110 switch (cpInfo.getTag()) {
jjg@46 111 case ConstantPool.CONSTANT_Double:
jjg@46 112 case ConstantPool.CONSTANT_Long:
jjg@46 113 i += 1;
jjg@46 114 }
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
jjg@46 311 public CPInfo visitLong(CONSTANT_Long_info info, Map<Object, Object> translations) {
jjg@46 312 CONSTANT_Long_info info2 = (CONSTANT_Long_info) translations.get(info);
jjg@46 313 if (info2 == null) {
jjg@46 314 info2 = info;
jjg@46 315 translations.put(info, info2);
jjg@46 316 }
jjg@46 317 return info;
jjg@46 318 }
jjg@46 319
jjg@46 320 public CPInfo visitNameAndType(CONSTANT_NameAndType_info info, Map<Object, Object> translations) {
jjg@46 321 CONSTANT_NameAndType_info info2 = (CONSTANT_NameAndType_info) translations.get(info);
jjg@46 322 if (info2 == null) {
jjg@46 323 ConstantPool cp2 = translate(info.cp, translations);
jjg@46 324 if (cp2 == info.cp)
jjg@46 325 info2 = info;
jjg@46 326 else
jjg@46 327 info2 = new CONSTANT_NameAndType_info(cp2, info.name_index, info.type_index);
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 visitMethodref(CONSTANT_Methodref_info info, Map<Object, Object> translations) {
jjg@46 334 CONSTANT_Methodref_info info2 = (CONSTANT_Methodref_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_Methodref_info(cp2, info.class_index, info.name_and_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 visitString(CONSTANT_String_info info, Map<Object, Object> translations) {
jjg@46 347 CONSTANT_String_info info2 = (CONSTANT_String_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_String_info(cp2, info.string_index);
jjg@46 354 translations.put(info, info2);
jjg@46 355 }
jjg@46 356 return info;
jjg@46 357 }
jjg@46 358
jjg@46 359 public CPInfo visitUtf8(CONSTANT_Utf8_info info, Map<Object, Object> translations) {
jjg@46 360 CONSTANT_Utf8_info info2 = (CONSTANT_Utf8_info) translations.get(info);
jjg@46 361 if (info2 == null) {
jjg@46 362 info2 = info;
jjg@46 363 translations.put(info, info2);
jjg@46 364 }
jjg@46 365 return info;
jjg@46 366 }
jjg@46 367
jjg@46 368 }

mercurial