src/share/classes/com/sun/tools/javap/ClassWriter.java

Thu, 24 Jul 2008 19:06:57 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 19:06:57 +0100
changeset 80
5c9cdeb740f2
parent 65
0d4aa3c00af5
child 87
fd1d361ae294
permissions
-rw-r--r--

6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg

jjg@46 1 /*
xdono@54 2 * Copyright 2007-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.javap;
jjg@46 27
jjg@46 28 import java.util.Collection;
jjg@46 29 import java.util.List;
jjg@46 30
jjg@46 31 import com.sun.tools.classfile.AccessFlags;
jjg@46 32 import com.sun.tools.classfile.Attribute;
jjg@46 33 import com.sun.tools.classfile.Attributes;
jjg@46 34 import com.sun.tools.classfile.ClassFile;
jjg@46 35 import com.sun.tools.classfile.Code_attribute;
jjg@46 36 import com.sun.tools.classfile.ConstantPool;
jjg@46 37 import com.sun.tools.classfile.ConstantPoolException;
jjg@46 38 import com.sun.tools.classfile.Descriptor;
jjg@46 39 import com.sun.tools.classfile.DescriptorException;
jjg@46 40 import com.sun.tools.classfile.Exceptions_attribute;
jjg@46 41 import com.sun.tools.classfile.Field;
jjg@46 42 import com.sun.tools.classfile.Method;
jjg@46 43 import com.sun.tools.classfile.Signature;
jjg@46 44 import com.sun.tools.classfile.Signature_attribute;
jjg@46 45 import com.sun.tools.classfile.SourceFile_attribute;
jjg@46 46 import com.sun.tools.classfile.Type;
jjg@46 47
jjg@46 48 import static com.sun.tools.classfile.AccessFlags.*;
jjg@46 49
jjg@46 50 /*
jjg@46 51 * The main javap class to write the contents of a class file as text.
jjg@46 52 *
jjg@46 53 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 54 * you write code that depends on this, you do so at your own risk.
jjg@46 55 * This code and its internal interfaces are subject to change or
jjg@46 56 * deletion without notice.</b>
jjg@46 57 */
jjg@46 58 public class ClassWriter extends BasicWriter {
jjg@46 59 static ClassWriter instance(Context context) {
jjg@46 60 ClassWriter instance = context.get(ClassWriter.class);
jjg@46 61 if (instance == null)
jjg@46 62 instance = new ClassWriter(context);
jjg@46 63 return instance;
jjg@46 64 }
jjg@46 65
jjg@46 66 protected ClassWriter(Context context) {
jjg@46 67 super(context);
jjg@46 68 context.put(ClassWriter.class, this);
jjg@46 69 options = Options.instance(context);
jjg@46 70 attrWriter = AttributeWriter.instance(context);
jjg@46 71 codeWriter = CodeWriter.instance(context);
jjg@46 72 constantWriter = ConstantWriter.instance(context);
jjg@46 73 }
jjg@46 74
jjg@46 75 ClassFile getClassFile() {
jjg@46 76 return classFile;
jjg@46 77 }
jjg@46 78
jjg@46 79 Method getMethod() {
jjg@46 80 return method;
jjg@46 81 }
jjg@46 82
jjg@46 83 public void write(ClassFile cf) {
jjg@46 84 classFile = cf;
jjg@46 85 constant_pool = classFile.constant_pool;
jjg@46 86
jjg@46 87 Attribute sfa = cf.getAttribute(Attribute.SourceFile);
jjg@46 88 if (sfa instanceof SourceFile_attribute) {
jjg@46 89 println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
jjg@46 90 }
jjg@46 91
jjg@46 92 String name = getJavaName(classFile);
jjg@46 93 AccessFlags flags = cf.access_flags;
jjg@46 94
jjg@46 95 writeModifiers(flags.getClassModifiers());
jjg@46 96
jjg@46 97 if (classFile.isClass())
jjg@46 98 print("class ");
jjg@46 99 else if (classFile.isInterface())
jjg@46 100 print("interface ");
jjg@46 101
jjg@46 102 print(name);
jjg@46 103
jjg@46 104 Signature_attribute sigAttr = getSignature(cf.attributes);
jjg@46 105 if (sigAttr == null) {
jjg@46 106 // use info from class file header
jjg@65 107 if (classFile.isClass() && classFile.super_class != 0 ) {
jjg@65 108 String sn = getJavaSuperclassName(cf);
jjg@65 109 print(" extends ");
jjg@65 110 print(sn);
jjg@46 111 }
jjg@46 112 for (int i = 0; i < classFile.interfaces.length; i++) {
jjg@46 113 print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ",");
jjg@46 114 print(getJavaInterfaceName(classFile, i));
jjg@46 115 }
jjg@46 116 } else {
jjg@46 117 try {
jjg@46 118 Type t = sigAttr.getParsedSignature().getType(constant_pool);
jjg@46 119 // The signature parser cannot disambiguate between a
jjg@46 120 // FieldType and a ClassSignatureType that only contains a superclass type.
jjg@46 121 if (t instanceof Type.ClassSigType)
jjg@46 122 print(t);
jjg@65 123 else {
jjg@46 124 print(" extends ");
jjg@46 125 print(t);
jjg@46 126 }
jjg@46 127 } catch (ConstantPoolException e) {
jjg@46 128 print(report(e));
jjg@46 129 }
jjg@46 130 }
jjg@46 131
jjg@46 132 if (options.verbose) {
jjg@46 133 println();
jjg@46 134 attrWriter.write(cf, cf.attributes, constant_pool);
jjg@46 135 println(" minor version: " + cf.minor_version);
jjg@46 136 println(" major version: " + cf.major_version);
jjg@46 137 if (!options.compat)
jjg@46 138 writeList(" flags: ", flags.getClassFlags(), NEWLINE);
jjg@46 139 constantWriter.writeConstantPool();
jjg@46 140 println();
jjg@46 141 } else {
jjg@46 142 if (!options.compat)
jjg@46 143 print(" ");
jjg@46 144 }
jjg@46 145
jjg@46 146 println("{");
jjg@46 147 writeFields();
jjg@46 148 writeMethods();
jjg@46 149 println("}");
jjg@46 150 println();
jjg@46 151 }
jjg@46 152
jjg@46 153 void writeFields() {
jjg@46 154 for (Field f: classFile.fields) {
jjg@46 155 writeField(f);
jjg@46 156 }
jjg@46 157 }
jjg@46 158
jjg@46 159 void writeField(Field f) {
jjg@46 160 if (!options.checkAccess(f.access_flags))
jjg@46 161 return;
jjg@46 162
jjg@46 163 if (!(options.showLineAndLocalVariableTables
jjg@46 164 || options.showDisassembled
jjg@46 165 || options.verbose
jjg@46 166 || options.showInternalSignatures
jjg@46 167 || options.showAllAttrs)) {
jjg@46 168 print(" ");
jjg@46 169 }
jjg@46 170
jjg@46 171 AccessFlags flags = f.access_flags;
jjg@46 172 writeModifiers(flags.getFieldModifiers());
jjg@46 173 Signature_attribute sigAttr = getSignature(f.attributes);
jjg@46 174 if (sigAttr == null)
jjg@46 175 print(getFieldType(f.descriptor));
jjg@46 176 else {
jjg@46 177 try {
jjg@46 178 Type t = sigAttr.getParsedSignature().getType(constant_pool);
jjg@46 179 print(t);
jjg@46 180 } catch (ConstantPoolException e) {
jjg@46 181 // report error?
jjg@46 182 // fall back on non-generic descriptor
jjg@46 183 print(getFieldType(f.descriptor));
jjg@46 184 }
jjg@46 185 }
jjg@46 186 print(" ");
jjg@46 187 print(getFieldName(f));
jjg@46 188 print(";");
jjg@46 189 println();
jjg@46 190
jjg@46 191 if (options.showInternalSignatures)
jjg@46 192 println(" Signature: " + getValue(f.descriptor));
jjg@46 193
jjg@46 194 if (options.verbose && !options.compat)
jjg@46 195 writeList(" flags: ", flags.getFieldFlags(), NEWLINE);
jjg@46 196
jjg@46 197 if (options.showAllAttrs) {
jjg@46 198 for (Attribute attr: f.attributes)
jjg@46 199 attrWriter.write(f, attr, constant_pool);
jjg@46 200 println();
jjg@46 201 }
jjg@46 202
jjg@46 203 if (options.showDisassembled || options.showLineAndLocalVariableTables)
jjg@46 204 println();
jjg@46 205 }
jjg@46 206
jjg@46 207 void writeMethods() {
jjg@46 208 for (Method m: classFile.methods)
jjg@46 209 writeMethod(m);
jjg@46 210 }
jjg@46 211
jjg@46 212 void writeMethod(Method m) {
jjg@46 213 if (!options.checkAccess(m.access_flags))
jjg@46 214 return;
jjg@46 215
jjg@46 216 method = m;
jjg@46 217
jjg@46 218 if (!(options.showLineAndLocalVariableTables
jjg@46 219 || options.showDisassembled
jjg@46 220 || options.verbose
jjg@46 221 || options.showInternalSignatures
jjg@46 222 || options.showAllAttrs)) {
jjg@46 223 print(" ");
jjg@46 224 }
jjg@46 225
jjg@46 226 AccessFlags flags = m.access_flags;
jjg@46 227
jjg@46 228 Descriptor d;
jjg@46 229 Type.MethodType methodType;
jjg@46 230 List<? extends Type> methodExceptions;
jjg@46 231
jjg@46 232 Signature_attribute sigAttr = getSignature(m.attributes);
jjg@46 233 if (sigAttr == null) {
jjg@46 234 d = m.descriptor;
jjg@46 235 methodType = null;
jjg@46 236 methodExceptions = null;
jjg@46 237 } else {
jjg@46 238 Signature methodSig = sigAttr.getParsedSignature();
jjg@46 239 d = methodSig;
jjg@46 240 try {
jjg@46 241 methodType = (Type.MethodType) methodSig.getType(constant_pool);
jjg@46 242 methodExceptions = methodType.throwsTypes;
jjg@46 243 if (methodExceptions != null && methodExceptions.size() == 0)
jjg@46 244 methodExceptions = null;
jjg@46 245 } catch (ConstantPoolException e) {
jjg@46 246 // report error?
jjg@46 247 // fall back on standard descriptor
jjg@46 248 methodType = null;
jjg@46 249 methodExceptions = null;
jjg@46 250 }
jjg@46 251 }
jjg@46 252
jjg@46 253 writeModifiers(flags.getMethodModifiers());
jjg@46 254 if (methodType != null) {
jjg@46 255 writeListIfNotEmpty("<", methodType.typeArgTypes, "> ");
jjg@46 256 }
jjg@46 257 if (getName(m).equals("<init>")) {
jjg@46 258 print(getJavaName(classFile));
jjg@46 259 print(getParameterTypes(d, flags));
jjg@46 260 } else if (getName(m).equals("<clinit>")) {
jjg@46 261 print("{}");
jjg@46 262 } else {
jjg@46 263 print(getReturnType(d));
jjg@46 264 print(" ");
jjg@46 265 print(getName(m));
jjg@46 266 print(getParameterTypes(d, flags));
jjg@46 267 }
jjg@46 268
jjg@46 269 Attribute e_attr = m.attributes.get(Attribute.Exceptions);
jjg@46 270 if (e_attr != null) { // if there are generic exceptions, there must be erased exceptions
jjg@46 271 if (e_attr instanceof Exceptions_attribute) {
jjg@46 272 Exceptions_attribute exceptions = (Exceptions_attribute) e_attr;
jjg@46 273 if (options.compat) { // Bug XXXXXXX whitespace
jjg@46 274 if (!(options.showLineAndLocalVariableTables
jjg@46 275 || options.showDisassembled
jjg@46 276 || options.verbose
jjg@46 277 || options.showInternalSignatures
jjg@46 278 || options.showAllAttrs)) {
jjg@46 279 print(" ");
jjg@46 280 }
jjg@46 281 print(" ");
jjg@46 282 }
jjg@46 283 print(" throws ");
jjg@46 284 if (methodExceptions != null) { // use generic list if available
jjg@46 285 writeList("", methodExceptions, "");
jjg@46 286 } else {
jjg@46 287 for (int i = 0; i < exceptions.number_of_exceptions; i++) {
jjg@46 288 if (i > 0)
jjg@46 289 print(", ");
jjg@52 290 print(getJavaException(exceptions, i));
jjg@46 291 }
jjg@46 292 }
jjg@46 293 } else {
jjg@46 294 report("Unexpected or invalid value for Exceptions attribute");
jjg@46 295 }
jjg@46 296 }
jjg@46 297
jjg@46 298 print(";");
jjg@46 299 println();
jjg@46 300
jjg@46 301 if (options.showInternalSignatures)
jjg@46 302 println(" Signature: " + getValue(m.descriptor));
jjg@46 303
jjg@46 304 if (options.verbose && !options.compat)
jjg@46 305 writeList(" flags: ", flags.getMethodFlags(), NEWLINE);
jjg@46 306
jjg@46 307 Code_attribute code = null;
jjg@46 308 Attribute c_attr = m.attributes.get(Attribute.Code);
jjg@46 309 if (c_attr != null) {
jjg@46 310 if (c_attr instanceof Code_attribute)
jjg@46 311 code = (Code_attribute) c_attr;
jjg@46 312 else
jjg@46 313 report("Unexpected or invalid value for Code attribute");
jjg@46 314 }
jjg@46 315
jjg@46 316 if (options.showDisassembled && !options.showAllAttrs) {
jjg@46 317 if (code != null) {
jjg@46 318 println(" Code:");
jjg@46 319 codeWriter.writeInstrs(code);
jjg@46 320 codeWriter.writeExceptionTable(code);
jjg@46 321 }
jjg@46 322 println();
jjg@46 323 }
jjg@46 324
jjg@46 325 if (options.showLineAndLocalVariableTables) {
jjg@46 326 if (code != null)
jjg@46 327 attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool);
jjg@46 328 println();
jjg@46 329 if (code != null)
jjg@46 330 attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool);
jjg@46 331 println();
jjg@46 332 println();
jjg@46 333 }
jjg@46 334
jjg@46 335 if (options.showAllAttrs) {
jjg@46 336 Attribute[] attrs = m.attributes.attrs;
jjg@46 337 for (Attribute attr: attrs)
jjg@46 338 attrWriter.write(m, attr, constant_pool);
jjg@46 339
jjg@46 340 // // the following condition is to mimic old javap
jjg@46 341 // if (!(attrs.length > 0 &&
jjg@46 342 // attrs[attrs.length - 1] instanceof Exceptions_attribute))
jjg@46 343 println();
jjg@46 344 }
jjg@46 345 }
jjg@46 346
jjg@46 347 void writeModifiers(Collection<String> items) {
jjg@46 348 for (Object item: items) {
jjg@46 349 print(item);
jjg@46 350 print(" ");
jjg@46 351 }
jjg@46 352 }
jjg@46 353
jjg@46 354 void writeList(String prefix, Collection<?> items, String suffix) {
jjg@46 355 print(prefix);
jjg@46 356 String sep = "";
jjg@46 357 for (Object item: items) {
jjg@46 358 print(sep);
jjg@46 359 print(item);
jjg@46 360 sep = ", ";
jjg@46 361 }
jjg@46 362 print(suffix);
jjg@46 363 }
jjg@46 364
jjg@46 365 void writeListIfNotEmpty(String prefix, List<?> items, String suffix) {
jjg@46 366 if (items != null && items.size() > 0)
jjg@46 367 writeList(prefix, items, suffix);
jjg@46 368 }
jjg@46 369
jjg@46 370 Signature_attribute getSignature(Attributes attributes) {
jjg@46 371 if (options.compat) // javap does not recognize recent attributes
jjg@46 372 return null;
jjg@46 373 return (Signature_attribute) attributes.get(Attribute.Signature);
jjg@46 374 }
jjg@46 375
jjg@46 376 String adjustVarargs(AccessFlags flags, String params) {
jjg@46 377 if (flags.is(ACC_VARARGS) && !options.compat) {
jjg@46 378 int i = params.lastIndexOf("[]");
jjg@46 379 if (i > 0)
jjg@46 380 return params.substring(0, i) + "..." + params.substring(i+2);
jjg@46 381 }
jjg@46 382
jjg@46 383 return params;
jjg@46 384 }
jjg@46 385
jjg@46 386 String getJavaName(ClassFile cf) {
jjg@46 387 try {
jjg@46 388 return getJavaName(cf.getName());
jjg@46 389 } catch (ConstantPoolException e) {
jjg@46 390 return report(e);
jjg@46 391 }
jjg@46 392 }
jjg@46 393
jjg@46 394 String getJavaSuperclassName(ClassFile cf) {
jjg@46 395 try {
jjg@46 396 return getJavaName(cf.getSuperclassName());
jjg@46 397 } catch (ConstantPoolException e) {
jjg@46 398 return report(e);
jjg@46 399 }
jjg@46 400 }
jjg@46 401
jjg@46 402 String getJavaInterfaceName(ClassFile cf, int index) {
jjg@46 403 try {
jjg@46 404 return getJavaName(cf.getInterfaceName(index));
jjg@46 405 } catch (ConstantPoolException e) {
jjg@46 406 return report(e);
jjg@46 407 }
jjg@46 408 }
jjg@46 409
jjg@46 410 String getFieldType(Descriptor d) {
jjg@46 411 try {
jjg@46 412 return d.getFieldType(constant_pool);
jjg@46 413 } catch (ConstantPoolException e) {
jjg@46 414 return report(e);
jjg@46 415 } catch (DescriptorException e) {
jjg@46 416 return report(e);
jjg@46 417 }
jjg@46 418 }
jjg@46 419
jjg@46 420 String getReturnType(Descriptor d) {
jjg@46 421 try {
jjg@46 422 return d.getReturnType(constant_pool);
jjg@46 423 } catch (ConstantPoolException e) {
jjg@46 424 return report(e);
jjg@46 425 } catch (DescriptorException e) {
jjg@46 426 return report(e);
jjg@46 427 }
jjg@46 428 }
jjg@46 429
jjg@46 430 String getParameterTypes(Descriptor d, AccessFlags flags) {
jjg@46 431 try {
jjg@46 432 return adjustVarargs(flags, d.getParameterTypes(constant_pool));
jjg@46 433 } catch (ConstantPoolException e) {
jjg@46 434 return report(e);
jjg@46 435 } catch (DescriptorException e) {
jjg@46 436 return report(e);
jjg@46 437 }
jjg@46 438 }
jjg@46 439
jjg@52 440 String getJavaException(Exceptions_attribute attr, int index) {
jjg@52 441 try {
jjg@52 442 return getJavaName(attr.getException(index, constant_pool));
jjg@52 443 } catch (ConstantPoolException e) {
jjg@52 444 return report(e);
jjg@52 445 }
jjg@52 446 }
jjg@52 447
jjg@46 448 String getValue(Descriptor d) {
jjg@46 449 try {
jjg@46 450 return d.getValue(constant_pool);
jjg@46 451 } catch (ConstantPoolException e) {
jjg@46 452 return report(e);
jjg@46 453 }
jjg@46 454 }
jjg@46 455
jjg@46 456 String getFieldName(Field f) {
jjg@46 457 try {
jjg@46 458 return f.getName(constant_pool);
jjg@46 459 } catch (ConstantPoolException e) {
jjg@46 460 return report(e);
jjg@46 461 }
jjg@46 462 }
jjg@46 463
jjg@46 464 String getName(Method m) {
jjg@46 465 try {
jjg@46 466 return m.getName(constant_pool);
jjg@46 467 } catch (ConstantPoolException e) {
jjg@46 468 return report(e);
jjg@46 469 }
jjg@46 470 }
jjg@46 471
jjg@46 472 static String getJavaName(String name) {
jjg@46 473 return name.replace('/', '.');
jjg@46 474 }
jjg@46 475
jjg@46 476 String getSourceFile(SourceFile_attribute attr) {
jjg@46 477 try {
jjg@46 478 return attr.getSourceFile(constant_pool);
jjg@46 479 } catch (ConstantPoolException e) {
jjg@46 480 return report(e);
jjg@46 481 }
jjg@46 482 }
jjg@46 483
jjg@46 484 private Options options;
jjg@46 485 private AttributeWriter attrWriter;
jjg@46 486 private CodeWriter codeWriter;
jjg@46 487 private ConstantWriter constantWriter;
jjg@46 488 private ClassFile classFile;
jjg@46 489 private ConstantPool constant_pool;
jjg@46 490 private Method method;
jjg@46 491 private static final String NEWLINE = System.getProperty("line.separator", "\n");
jjg@46 492 }

mercurial