Mon, 04 Aug 2008 17:54:15 -0700
4884240: additional option required for javap
Reviewed-by: ksrini
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@88 | 28 | import java.net.URI; |
jjg@46 | 29 | import java.util.Collection; |
jjg@46 | 30 | import java.util.List; |
jjg@46 | 31 | |
jjg@46 | 32 | import com.sun.tools.classfile.AccessFlags; |
jjg@46 | 33 | import com.sun.tools.classfile.Attribute; |
jjg@46 | 34 | import com.sun.tools.classfile.Attributes; |
jjg@46 | 35 | import com.sun.tools.classfile.ClassFile; |
jjg@46 | 36 | import com.sun.tools.classfile.Code_attribute; |
jjg@46 | 37 | import com.sun.tools.classfile.ConstantPool; |
jjg@46 | 38 | import com.sun.tools.classfile.ConstantPoolException; |
jjg@87 | 39 | import com.sun.tools.classfile.ConstantValue_attribute; |
jjg@46 | 40 | import com.sun.tools.classfile.Descriptor; |
jjg@46 | 41 | import com.sun.tools.classfile.DescriptorException; |
jjg@46 | 42 | import com.sun.tools.classfile.Exceptions_attribute; |
jjg@46 | 43 | import com.sun.tools.classfile.Field; |
jjg@46 | 44 | import com.sun.tools.classfile.Method; |
jjg@46 | 45 | import com.sun.tools.classfile.Signature; |
jjg@46 | 46 | import com.sun.tools.classfile.Signature_attribute; |
jjg@46 | 47 | import com.sun.tools.classfile.SourceFile_attribute; |
jjg@46 | 48 | import com.sun.tools.classfile.Type; |
jjg@46 | 49 | |
jjg@88 | 50 | import java.text.DateFormat; |
jjg@88 | 51 | import java.util.Date; |
jjg@46 | 52 | import static com.sun.tools.classfile.AccessFlags.*; |
jjg@46 | 53 | |
jjg@46 | 54 | /* |
jjg@46 | 55 | * The main javap class to write the contents of a class file as text. |
jjg@46 | 56 | * |
jjg@46 | 57 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
jjg@46 | 58 | * you write code that depends on this, you do so at your own risk. |
jjg@46 | 59 | * This code and its internal interfaces are subject to change or |
jjg@46 | 60 | * deletion without notice.</b> |
jjg@46 | 61 | */ |
jjg@46 | 62 | public class ClassWriter extends BasicWriter { |
jjg@46 | 63 | static ClassWriter instance(Context context) { |
jjg@46 | 64 | ClassWriter instance = context.get(ClassWriter.class); |
jjg@46 | 65 | if (instance == null) |
jjg@46 | 66 | instance = new ClassWriter(context); |
jjg@46 | 67 | return instance; |
jjg@46 | 68 | } |
jjg@46 | 69 | |
jjg@46 | 70 | protected ClassWriter(Context context) { |
jjg@46 | 71 | super(context); |
jjg@46 | 72 | context.put(ClassWriter.class, this); |
jjg@46 | 73 | options = Options.instance(context); |
jjg@46 | 74 | attrWriter = AttributeWriter.instance(context); |
jjg@46 | 75 | codeWriter = CodeWriter.instance(context); |
jjg@46 | 76 | constantWriter = ConstantWriter.instance(context); |
jjg@46 | 77 | } |
jjg@46 | 78 | |
jjg@88 | 79 | void setDigest(String name, byte[] digest) { |
jjg@88 | 80 | this.digestName = name; |
jjg@88 | 81 | this.digest = digest; |
jjg@88 | 82 | } |
jjg@88 | 83 | |
jjg@88 | 84 | void setFile(URI uri) { |
jjg@88 | 85 | this.uri = uri; |
jjg@88 | 86 | } |
jjg@88 | 87 | |
jjg@88 | 88 | void setFileSize(int size) { |
jjg@88 | 89 | this.size = size; |
jjg@88 | 90 | } |
jjg@88 | 91 | |
jjg@88 | 92 | void setLastModified(long lastModified) { |
jjg@88 | 93 | this.lastModified = lastModified; |
jjg@88 | 94 | } |
jjg@88 | 95 | |
jjg@46 | 96 | ClassFile getClassFile() { |
jjg@46 | 97 | return classFile; |
jjg@46 | 98 | } |
jjg@46 | 99 | |
jjg@46 | 100 | Method getMethod() { |
jjg@46 | 101 | return method; |
jjg@46 | 102 | } |
jjg@46 | 103 | |
jjg@46 | 104 | public void write(ClassFile cf) { |
jjg@46 | 105 | classFile = cf; |
jjg@46 | 106 | constant_pool = classFile.constant_pool; |
jjg@46 | 107 | |
jjg@88 | 108 | if ((options.sysInfo || options.verbose) && !options.compat) { |
jjg@88 | 109 | if (uri != null) { |
jjg@88 | 110 | if (uri.getScheme().equals("file")) |
jjg@88 | 111 | println("Classfile " + uri.getPath()); |
jjg@88 | 112 | else |
jjg@88 | 113 | println("Classfile " + uri); |
jjg@88 | 114 | } |
jjg@88 | 115 | if (lastModified != -1) { |
jjg@88 | 116 | Date lm = new Date(lastModified); |
jjg@88 | 117 | DateFormat df = DateFormat.getDateInstance(); |
jjg@88 | 118 | if (size > 0) { |
jjg@88 | 119 | println("Last modified " + df.format(lm) + "; size " + size + " bytes"); |
jjg@88 | 120 | } else { |
jjg@88 | 121 | println("Last modified " + df.format(lm)); |
jjg@88 | 122 | } |
jjg@88 | 123 | } else if (size > 0) { |
jjg@88 | 124 | println("Size " + size + " bytes"); |
jjg@88 | 125 | } |
jjg@88 | 126 | if (digestName != null && digest != null) { |
jjg@88 | 127 | StringBuilder sb = new StringBuilder(); |
jjg@88 | 128 | for (byte b: digest) |
jjg@88 | 129 | sb.append(String.format("%02x", b)); |
jjg@88 | 130 | println(digestName + " checksum " + sb); |
jjg@88 | 131 | } |
jjg@88 | 132 | } |
jjg@88 | 133 | |
jjg@46 | 134 | Attribute sfa = cf.getAttribute(Attribute.SourceFile); |
jjg@46 | 135 | if (sfa instanceof SourceFile_attribute) { |
jjg@46 | 136 | println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\""); |
jjg@46 | 137 | } |
jjg@46 | 138 | |
jjg@46 | 139 | String name = getJavaName(classFile); |
jjg@46 | 140 | AccessFlags flags = cf.access_flags; |
jjg@46 | 141 | |
jjg@46 | 142 | writeModifiers(flags.getClassModifiers()); |
jjg@46 | 143 | |
jjg@46 | 144 | if (classFile.isClass()) |
jjg@46 | 145 | print("class "); |
jjg@46 | 146 | else if (classFile.isInterface()) |
jjg@46 | 147 | print("interface "); |
jjg@46 | 148 | |
jjg@46 | 149 | print(name); |
jjg@46 | 150 | |
jjg@46 | 151 | Signature_attribute sigAttr = getSignature(cf.attributes); |
jjg@46 | 152 | if (sigAttr == null) { |
jjg@46 | 153 | // use info from class file header |
jjg@65 | 154 | if (classFile.isClass() && classFile.super_class != 0 ) { |
jjg@65 | 155 | String sn = getJavaSuperclassName(cf); |
jjg@65 | 156 | print(" extends "); |
jjg@65 | 157 | print(sn); |
jjg@46 | 158 | } |
jjg@46 | 159 | for (int i = 0; i < classFile.interfaces.length; i++) { |
jjg@46 | 160 | print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ","); |
jjg@46 | 161 | print(getJavaInterfaceName(classFile, i)); |
jjg@46 | 162 | } |
jjg@46 | 163 | } else { |
jjg@46 | 164 | try { |
jjg@46 | 165 | Type t = sigAttr.getParsedSignature().getType(constant_pool); |
jjg@46 | 166 | // The signature parser cannot disambiguate between a |
jjg@46 | 167 | // FieldType and a ClassSignatureType that only contains a superclass type. |
jjg@46 | 168 | if (t instanceof Type.ClassSigType) |
jjg@46 | 169 | print(t); |
jjg@65 | 170 | else { |
jjg@46 | 171 | print(" extends "); |
jjg@46 | 172 | print(t); |
jjg@46 | 173 | } |
jjg@46 | 174 | } catch (ConstantPoolException e) { |
jjg@46 | 175 | print(report(e)); |
jjg@46 | 176 | } |
jjg@46 | 177 | } |
jjg@46 | 178 | |
jjg@46 | 179 | if (options.verbose) { |
jjg@46 | 180 | println(); |
jjg@46 | 181 | attrWriter.write(cf, cf.attributes, constant_pool); |
jjg@46 | 182 | println(" minor version: " + cf.minor_version); |
jjg@46 | 183 | println(" major version: " + cf.major_version); |
jjg@46 | 184 | if (!options.compat) |
jjg@46 | 185 | writeList(" flags: ", flags.getClassFlags(), NEWLINE); |
jjg@46 | 186 | constantWriter.writeConstantPool(); |
jjg@46 | 187 | println(); |
jjg@46 | 188 | } else { |
jjg@46 | 189 | if (!options.compat) |
jjg@46 | 190 | print(" "); |
jjg@46 | 191 | } |
jjg@46 | 192 | |
jjg@46 | 193 | println("{"); |
jjg@46 | 194 | writeFields(); |
jjg@46 | 195 | writeMethods(); |
jjg@46 | 196 | println("}"); |
jjg@46 | 197 | println(); |
jjg@46 | 198 | } |
jjg@46 | 199 | |
jjg@46 | 200 | void writeFields() { |
jjg@46 | 201 | for (Field f: classFile.fields) { |
jjg@46 | 202 | writeField(f); |
jjg@46 | 203 | } |
jjg@46 | 204 | } |
jjg@46 | 205 | |
jjg@46 | 206 | void writeField(Field f) { |
jjg@46 | 207 | if (!options.checkAccess(f.access_flags)) |
jjg@46 | 208 | return; |
jjg@46 | 209 | |
jjg@46 | 210 | if (!(options.showLineAndLocalVariableTables |
jjg@46 | 211 | || options.showDisassembled |
jjg@46 | 212 | || options.verbose |
jjg@46 | 213 | || options.showInternalSignatures |
jjg@46 | 214 | || options.showAllAttrs)) { |
jjg@46 | 215 | print(" "); |
jjg@46 | 216 | } |
jjg@46 | 217 | |
jjg@46 | 218 | AccessFlags flags = f.access_flags; |
jjg@46 | 219 | writeModifiers(flags.getFieldModifiers()); |
jjg@46 | 220 | Signature_attribute sigAttr = getSignature(f.attributes); |
jjg@46 | 221 | if (sigAttr == null) |
jjg@46 | 222 | print(getFieldType(f.descriptor)); |
jjg@46 | 223 | else { |
jjg@46 | 224 | try { |
jjg@46 | 225 | Type t = sigAttr.getParsedSignature().getType(constant_pool); |
jjg@46 | 226 | print(t); |
jjg@46 | 227 | } catch (ConstantPoolException e) { |
jjg@46 | 228 | // report error? |
jjg@46 | 229 | // fall back on non-generic descriptor |
jjg@46 | 230 | print(getFieldType(f.descriptor)); |
jjg@46 | 231 | } |
jjg@46 | 232 | } |
jjg@46 | 233 | print(" "); |
jjg@46 | 234 | print(getFieldName(f)); |
jjg@87 | 235 | if (options.showConstants && !options.compat) { // BUG 4111861 print static final field contents |
jjg@87 | 236 | Attribute a = f.attributes.get(Attribute.ConstantValue); |
jjg@87 | 237 | if (a instanceof ConstantValue_attribute) { |
jjg@87 | 238 | print(" = "); |
jjg@87 | 239 | ConstantValue_attribute cv = (ConstantValue_attribute) a; |
jjg@87 | 240 | print(getConstantValue(f.descriptor, cv.constantvalue_index)); |
jjg@87 | 241 | } |
jjg@87 | 242 | } |
jjg@46 | 243 | print(";"); |
jjg@46 | 244 | println(); |
jjg@46 | 245 | |
jjg@46 | 246 | if (options.showInternalSignatures) |
jjg@46 | 247 | println(" Signature: " + getValue(f.descriptor)); |
jjg@46 | 248 | |
jjg@46 | 249 | if (options.verbose && !options.compat) |
jjg@46 | 250 | writeList(" flags: ", flags.getFieldFlags(), NEWLINE); |
jjg@46 | 251 | |
jjg@46 | 252 | if (options.showAllAttrs) { |
jjg@46 | 253 | for (Attribute attr: f.attributes) |
jjg@46 | 254 | attrWriter.write(f, attr, constant_pool); |
jjg@46 | 255 | println(); |
jjg@46 | 256 | } |
jjg@46 | 257 | |
jjg@46 | 258 | if (options.showDisassembled || options.showLineAndLocalVariableTables) |
jjg@46 | 259 | println(); |
jjg@46 | 260 | } |
jjg@46 | 261 | |
jjg@46 | 262 | void writeMethods() { |
jjg@46 | 263 | for (Method m: classFile.methods) |
jjg@46 | 264 | writeMethod(m); |
jjg@46 | 265 | } |
jjg@46 | 266 | |
jjg@46 | 267 | void writeMethod(Method m) { |
jjg@46 | 268 | if (!options.checkAccess(m.access_flags)) |
jjg@46 | 269 | return; |
jjg@46 | 270 | |
jjg@46 | 271 | method = m; |
jjg@46 | 272 | |
jjg@46 | 273 | if (!(options.showLineAndLocalVariableTables |
jjg@46 | 274 | || options.showDisassembled |
jjg@46 | 275 | || options.verbose |
jjg@46 | 276 | || options.showInternalSignatures |
jjg@46 | 277 | || options.showAllAttrs)) { |
jjg@46 | 278 | print(" "); |
jjg@46 | 279 | } |
jjg@46 | 280 | |
jjg@46 | 281 | AccessFlags flags = m.access_flags; |
jjg@46 | 282 | |
jjg@46 | 283 | Descriptor d; |
jjg@46 | 284 | Type.MethodType methodType; |
jjg@46 | 285 | List<? extends Type> methodExceptions; |
jjg@46 | 286 | |
jjg@46 | 287 | Signature_attribute sigAttr = getSignature(m.attributes); |
jjg@46 | 288 | if (sigAttr == null) { |
jjg@46 | 289 | d = m.descriptor; |
jjg@46 | 290 | methodType = null; |
jjg@46 | 291 | methodExceptions = null; |
jjg@46 | 292 | } else { |
jjg@46 | 293 | Signature methodSig = sigAttr.getParsedSignature(); |
jjg@46 | 294 | d = methodSig; |
jjg@46 | 295 | try { |
jjg@46 | 296 | methodType = (Type.MethodType) methodSig.getType(constant_pool); |
jjg@46 | 297 | methodExceptions = methodType.throwsTypes; |
jjg@46 | 298 | if (methodExceptions != null && methodExceptions.size() == 0) |
jjg@46 | 299 | methodExceptions = null; |
jjg@46 | 300 | } catch (ConstantPoolException e) { |
jjg@46 | 301 | // report error? |
jjg@46 | 302 | // fall back on standard descriptor |
jjg@46 | 303 | methodType = null; |
jjg@46 | 304 | methodExceptions = null; |
jjg@46 | 305 | } |
jjg@46 | 306 | } |
jjg@46 | 307 | |
jjg@46 | 308 | writeModifiers(flags.getMethodModifiers()); |
jjg@46 | 309 | if (methodType != null) { |
jjg@46 | 310 | writeListIfNotEmpty("<", methodType.typeArgTypes, "> "); |
jjg@46 | 311 | } |
jjg@46 | 312 | if (getName(m).equals("<init>")) { |
jjg@46 | 313 | print(getJavaName(classFile)); |
jjg@46 | 314 | print(getParameterTypes(d, flags)); |
jjg@46 | 315 | } else if (getName(m).equals("<clinit>")) { |
jjg@46 | 316 | print("{}"); |
jjg@46 | 317 | } else { |
jjg@46 | 318 | print(getReturnType(d)); |
jjg@46 | 319 | print(" "); |
jjg@46 | 320 | print(getName(m)); |
jjg@46 | 321 | print(getParameterTypes(d, flags)); |
jjg@46 | 322 | } |
jjg@46 | 323 | |
jjg@46 | 324 | Attribute e_attr = m.attributes.get(Attribute.Exceptions); |
jjg@46 | 325 | if (e_attr != null) { // if there are generic exceptions, there must be erased exceptions |
jjg@46 | 326 | if (e_attr instanceof Exceptions_attribute) { |
jjg@46 | 327 | Exceptions_attribute exceptions = (Exceptions_attribute) e_attr; |
jjg@46 | 328 | if (options.compat) { // Bug XXXXXXX whitespace |
jjg@46 | 329 | if (!(options.showLineAndLocalVariableTables |
jjg@46 | 330 | || options.showDisassembled |
jjg@46 | 331 | || options.verbose |
jjg@46 | 332 | || options.showInternalSignatures |
jjg@46 | 333 | || options.showAllAttrs)) { |
jjg@46 | 334 | print(" "); |
jjg@46 | 335 | } |
jjg@46 | 336 | print(" "); |
jjg@46 | 337 | } |
jjg@46 | 338 | print(" throws "); |
jjg@46 | 339 | if (methodExceptions != null) { // use generic list if available |
jjg@46 | 340 | writeList("", methodExceptions, ""); |
jjg@46 | 341 | } else { |
jjg@46 | 342 | for (int i = 0; i < exceptions.number_of_exceptions; i++) { |
jjg@46 | 343 | if (i > 0) |
jjg@46 | 344 | print(", "); |
jjg@52 | 345 | print(getJavaException(exceptions, i)); |
jjg@46 | 346 | } |
jjg@46 | 347 | } |
jjg@46 | 348 | } else { |
jjg@46 | 349 | report("Unexpected or invalid value for Exceptions attribute"); |
jjg@46 | 350 | } |
jjg@46 | 351 | } |
jjg@46 | 352 | |
jjg@46 | 353 | print(";"); |
jjg@46 | 354 | println(); |
jjg@46 | 355 | |
jjg@46 | 356 | if (options.showInternalSignatures) |
jjg@46 | 357 | println(" Signature: " + getValue(m.descriptor)); |
jjg@46 | 358 | |
jjg@46 | 359 | if (options.verbose && !options.compat) |
jjg@46 | 360 | writeList(" flags: ", flags.getMethodFlags(), NEWLINE); |
jjg@46 | 361 | |
jjg@46 | 362 | Code_attribute code = null; |
jjg@46 | 363 | Attribute c_attr = m.attributes.get(Attribute.Code); |
jjg@46 | 364 | if (c_attr != null) { |
jjg@46 | 365 | if (c_attr instanceof Code_attribute) |
jjg@46 | 366 | code = (Code_attribute) c_attr; |
jjg@46 | 367 | else |
jjg@46 | 368 | report("Unexpected or invalid value for Code attribute"); |
jjg@46 | 369 | } |
jjg@46 | 370 | |
jjg@46 | 371 | if (options.showDisassembled && !options.showAllAttrs) { |
jjg@46 | 372 | if (code != null) { |
jjg@46 | 373 | println(" Code:"); |
jjg@46 | 374 | codeWriter.writeInstrs(code); |
jjg@46 | 375 | codeWriter.writeExceptionTable(code); |
jjg@46 | 376 | } |
jjg@46 | 377 | println(); |
jjg@46 | 378 | } |
jjg@46 | 379 | |
jjg@46 | 380 | if (options.showLineAndLocalVariableTables) { |
jjg@46 | 381 | if (code != null) |
jjg@46 | 382 | attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool); |
jjg@46 | 383 | println(); |
jjg@46 | 384 | if (code != null) |
jjg@46 | 385 | attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool); |
jjg@46 | 386 | println(); |
jjg@46 | 387 | println(); |
jjg@46 | 388 | } |
jjg@46 | 389 | |
jjg@46 | 390 | if (options.showAllAttrs) { |
jjg@46 | 391 | Attribute[] attrs = m.attributes.attrs; |
jjg@46 | 392 | for (Attribute attr: attrs) |
jjg@46 | 393 | attrWriter.write(m, attr, constant_pool); |
jjg@46 | 394 | |
jjg@46 | 395 | // // the following condition is to mimic old javap |
jjg@46 | 396 | // if (!(attrs.length > 0 && |
jjg@46 | 397 | // attrs[attrs.length - 1] instanceof Exceptions_attribute)) |
jjg@46 | 398 | println(); |
jjg@46 | 399 | } |
jjg@46 | 400 | } |
jjg@46 | 401 | |
jjg@46 | 402 | void writeModifiers(Collection<String> items) { |
jjg@46 | 403 | for (Object item: items) { |
jjg@46 | 404 | print(item); |
jjg@46 | 405 | print(" "); |
jjg@46 | 406 | } |
jjg@46 | 407 | } |
jjg@46 | 408 | |
jjg@46 | 409 | void writeList(String prefix, Collection<?> items, String suffix) { |
jjg@46 | 410 | print(prefix); |
jjg@46 | 411 | String sep = ""; |
jjg@46 | 412 | for (Object item: items) { |
jjg@46 | 413 | print(sep); |
jjg@46 | 414 | print(item); |
jjg@46 | 415 | sep = ", "; |
jjg@46 | 416 | } |
jjg@46 | 417 | print(suffix); |
jjg@46 | 418 | } |
jjg@46 | 419 | |
jjg@46 | 420 | void writeListIfNotEmpty(String prefix, List<?> items, String suffix) { |
jjg@46 | 421 | if (items != null && items.size() > 0) |
jjg@46 | 422 | writeList(prefix, items, suffix); |
jjg@46 | 423 | } |
jjg@46 | 424 | |
jjg@46 | 425 | Signature_attribute getSignature(Attributes attributes) { |
jjg@46 | 426 | if (options.compat) // javap does not recognize recent attributes |
jjg@46 | 427 | return null; |
jjg@46 | 428 | return (Signature_attribute) attributes.get(Attribute.Signature); |
jjg@46 | 429 | } |
jjg@46 | 430 | |
jjg@46 | 431 | String adjustVarargs(AccessFlags flags, String params) { |
jjg@46 | 432 | if (flags.is(ACC_VARARGS) && !options.compat) { |
jjg@46 | 433 | int i = params.lastIndexOf("[]"); |
jjg@46 | 434 | if (i > 0) |
jjg@46 | 435 | return params.substring(0, i) + "..." + params.substring(i+2); |
jjg@46 | 436 | } |
jjg@46 | 437 | |
jjg@46 | 438 | return params; |
jjg@46 | 439 | } |
jjg@46 | 440 | |
jjg@46 | 441 | String getJavaName(ClassFile cf) { |
jjg@46 | 442 | try { |
jjg@46 | 443 | return getJavaName(cf.getName()); |
jjg@46 | 444 | } catch (ConstantPoolException e) { |
jjg@46 | 445 | return report(e); |
jjg@46 | 446 | } |
jjg@46 | 447 | } |
jjg@46 | 448 | |
jjg@46 | 449 | String getJavaSuperclassName(ClassFile cf) { |
jjg@46 | 450 | try { |
jjg@46 | 451 | return getJavaName(cf.getSuperclassName()); |
jjg@46 | 452 | } catch (ConstantPoolException e) { |
jjg@46 | 453 | return report(e); |
jjg@46 | 454 | } |
jjg@46 | 455 | } |
jjg@46 | 456 | |
jjg@46 | 457 | String getJavaInterfaceName(ClassFile cf, int index) { |
jjg@46 | 458 | try { |
jjg@46 | 459 | return getJavaName(cf.getInterfaceName(index)); |
jjg@46 | 460 | } catch (ConstantPoolException e) { |
jjg@46 | 461 | return report(e); |
jjg@46 | 462 | } |
jjg@46 | 463 | } |
jjg@46 | 464 | |
jjg@46 | 465 | String getFieldType(Descriptor d) { |
jjg@46 | 466 | try { |
jjg@46 | 467 | return d.getFieldType(constant_pool); |
jjg@46 | 468 | } catch (ConstantPoolException e) { |
jjg@46 | 469 | return report(e); |
jjg@46 | 470 | } catch (DescriptorException e) { |
jjg@46 | 471 | return report(e); |
jjg@46 | 472 | } |
jjg@46 | 473 | } |
jjg@46 | 474 | |
jjg@46 | 475 | String getReturnType(Descriptor d) { |
jjg@46 | 476 | try { |
jjg@46 | 477 | return d.getReturnType(constant_pool); |
jjg@46 | 478 | } catch (ConstantPoolException e) { |
jjg@46 | 479 | return report(e); |
jjg@46 | 480 | } catch (DescriptorException e) { |
jjg@46 | 481 | return report(e); |
jjg@46 | 482 | } |
jjg@46 | 483 | } |
jjg@46 | 484 | |
jjg@46 | 485 | String getParameterTypes(Descriptor d, AccessFlags flags) { |
jjg@46 | 486 | try { |
jjg@46 | 487 | return adjustVarargs(flags, d.getParameterTypes(constant_pool)); |
jjg@46 | 488 | } catch (ConstantPoolException e) { |
jjg@46 | 489 | return report(e); |
jjg@46 | 490 | } catch (DescriptorException e) { |
jjg@46 | 491 | return report(e); |
jjg@46 | 492 | } |
jjg@46 | 493 | } |
jjg@46 | 494 | |
jjg@52 | 495 | String getJavaException(Exceptions_attribute attr, int index) { |
jjg@52 | 496 | try { |
jjg@52 | 497 | return getJavaName(attr.getException(index, constant_pool)); |
jjg@52 | 498 | } catch (ConstantPoolException e) { |
jjg@52 | 499 | return report(e); |
jjg@52 | 500 | } |
jjg@52 | 501 | } |
jjg@52 | 502 | |
jjg@46 | 503 | String getValue(Descriptor d) { |
jjg@46 | 504 | try { |
jjg@46 | 505 | return d.getValue(constant_pool); |
jjg@46 | 506 | } catch (ConstantPoolException e) { |
jjg@46 | 507 | return report(e); |
jjg@46 | 508 | } |
jjg@46 | 509 | } |
jjg@46 | 510 | |
jjg@46 | 511 | String getFieldName(Field f) { |
jjg@46 | 512 | try { |
jjg@46 | 513 | return f.getName(constant_pool); |
jjg@46 | 514 | } catch (ConstantPoolException e) { |
jjg@46 | 515 | return report(e); |
jjg@46 | 516 | } |
jjg@46 | 517 | } |
jjg@46 | 518 | |
jjg@46 | 519 | String getName(Method m) { |
jjg@46 | 520 | try { |
jjg@46 | 521 | return m.getName(constant_pool); |
jjg@46 | 522 | } catch (ConstantPoolException e) { |
jjg@46 | 523 | return report(e); |
jjg@46 | 524 | } |
jjg@46 | 525 | } |
jjg@46 | 526 | |
jjg@46 | 527 | static String getJavaName(String name) { |
jjg@46 | 528 | return name.replace('/', '.'); |
jjg@46 | 529 | } |
jjg@46 | 530 | |
jjg@46 | 531 | String getSourceFile(SourceFile_attribute attr) { |
jjg@46 | 532 | try { |
jjg@46 | 533 | return attr.getSourceFile(constant_pool); |
jjg@46 | 534 | } catch (ConstantPoolException e) { |
jjg@46 | 535 | return report(e); |
jjg@46 | 536 | } |
jjg@46 | 537 | } |
jjg@46 | 538 | |
jjg@87 | 539 | /** |
jjg@87 | 540 | * Get the value of an entry in the constant pool as a Java constant. |
jjg@87 | 541 | * Characters and booleans are represented by CONSTANT_Intgere entries. |
jjg@87 | 542 | * Character and string values are processed to escape characters outside |
jjg@87 | 543 | * the basic printable ASCII set. |
jjg@87 | 544 | * @param d the descriptor, giving the expected type of the constant |
jjg@87 | 545 | * @param index the index of the value in the constant pool |
jjg@87 | 546 | * @return a printable string containing the value of the constant. |
jjg@87 | 547 | */ |
jjg@87 | 548 | String getConstantValue(Descriptor d, int index) { |
jjg@87 | 549 | try { |
jjg@87 | 550 | ConstantPool.CPInfo cpInfo = constant_pool.get(index); |
jjg@87 | 551 | |
jjg@87 | 552 | switch (cpInfo.getTag()) { |
jjg@87 | 553 | case ConstantPool.CONSTANT_Integer: { |
jjg@87 | 554 | ConstantPool.CONSTANT_Integer_info info = |
jjg@87 | 555 | (ConstantPool.CONSTANT_Integer_info) cpInfo; |
jjg@87 | 556 | String t = d.getValue(constant_pool); |
jjg@87 | 557 | if (t.equals("C")) { // character |
jjg@87 | 558 | return getConstantCharValue((char) info.value); |
jjg@87 | 559 | } else if (t.equals("Z")) { // boolean |
jjg@87 | 560 | return String.valueOf(info.value == 1); |
jjg@87 | 561 | } else { // other: assume integer |
jjg@87 | 562 | return String.valueOf(info.value); |
jjg@87 | 563 | } |
jjg@87 | 564 | } |
jjg@87 | 565 | |
jjg@87 | 566 | case ConstantPool.CONSTANT_String: { |
jjg@87 | 567 | ConstantPool.CONSTANT_String_info info = |
jjg@87 | 568 | (ConstantPool.CONSTANT_String_info) cpInfo; |
jjg@87 | 569 | return getConstantStringValue(info.getString()); |
jjg@87 | 570 | } |
jjg@87 | 571 | |
jjg@87 | 572 | default: |
jjg@87 | 573 | return constantWriter.stringValue(cpInfo); |
jjg@87 | 574 | } |
jjg@87 | 575 | } catch (ConstantPoolException e) { |
jjg@87 | 576 | return "#" + index; |
jjg@87 | 577 | } |
jjg@87 | 578 | } |
jjg@87 | 579 | |
jjg@87 | 580 | private String getConstantCharValue(char c) { |
jjg@87 | 581 | StringBuilder sb = new StringBuilder(); |
jjg@87 | 582 | sb.append('\''); |
jjg@87 | 583 | sb.append(esc(c, '\'')); |
jjg@87 | 584 | sb.append('\''); |
jjg@87 | 585 | return sb.toString(); |
jjg@87 | 586 | } |
jjg@87 | 587 | |
jjg@87 | 588 | private String getConstantStringValue(String s) { |
jjg@87 | 589 | StringBuilder sb = new StringBuilder(); |
jjg@87 | 590 | sb.append("\""); |
jjg@87 | 591 | for (int i = 0; i < s.length(); i++) { |
jjg@87 | 592 | sb.append(esc(s.charAt(i), '"')); |
jjg@87 | 593 | } |
jjg@87 | 594 | sb.append("\""); |
jjg@87 | 595 | return sb.toString(); |
jjg@87 | 596 | } |
jjg@87 | 597 | |
jjg@87 | 598 | private String esc(char c, char quote) { |
jjg@87 | 599 | if (32 <= c && c <= 126 && c != quote) |
jjg@87 | 600 | return String.valueOf(c); |
jjg@87 | 601 | else switch (c) { |
jjg@87 | 602 | case '\b': return "\\b"; |
jjg@87 | 603 | case '\n': return "\\n"; |
jjg@87 | 604 | case '\t': return "\\t"; |
jjg@87 | 605 | case '\f': return "\\f"; |
jjg@87 | 606 | case '\r': return "\\r"; |
jjg@87 | 607 | case '\\': return "\\\\"; |
jjg@87 | 608 | case '\'': return "\\'"; |
jjg@87 | 609 | case '\"': return "\\\""; |
jjg@87 | 610 | default: return String.format("\\u%04x", (int) c); |
jjg@87 | 611 | } |
jjg@87 | 612 | } |
jjg@87 | 613 | |
jjg@46 | 614 | private Options options; |
jjg@46 | 615 | private AttributeWriter attrWriter; |
jjg@46 | 616 | private CodeWriter codeWriter; |
jjg@46 | 617 | private ConstantWriter constantWriter; |
jjg@46 | 618 | private ClassFile classFile; |
jjg@88 | 619 | private URI uri; |
jjg@88 | 620 | private long lastModified; |
jjg@88 | 621 | private String digestName; |
jjg@88 | 622 | private byte[] digest; |
jjg@88 | 623 | private int size; |
jjg@46 | 624 | private ConstantPool constant_pool; |
jjg@46 | 625 | private Method method; |
jjg@46 | 626 | private static final String NEWLINE = System.getProperty("line.separator", "\n"); |
jjg@46 | 627 | } |