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

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

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
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 2007 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 com.sun.tools.classfile.ClassFile;
jjg@46 29 import com.sun.tools.classfile.ConstantPool;
jjg@46 30 import com.sun.tools.classfile.ConstantPoolException;
jjg@46 31
jjg@46 32 import static com.sun.tools.classfile.ConstantPool.*;
jjg@46 33
jjg@46 34 /*
jjg@46 35 * Write a constant pool entry.
jjg@46 36 *
jjg@46 37 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 38 * you write code that depends on this, you do so at your own risk.
jjg@46 39 * This code and its internal interfaces are subject to change or
jjg@46 40 * deletion without notice.</b>
jjg@46 41 */
jjg@46 42 public class ConstantWriter extends BasicWriter {
jjg@46 43 static ConstantWriter instance(Context context) {
jjg@46 44 ConstantWriter instance = context.get(ConstantWriter.class);
jjg@46 45 if (instance == null)
jjg@46 46 instance = new ConstantWriter(context);
jjg@46 47 return instance;
jjg@46 48 }
jjg@46 49
jjg@46 50 protected ConstantWriter(Context context) {
jjg@46 51 super(context);
jjg@46 52 context.put(ConstantWriter.class, this);
jjg@46 53 classWriter = ClassWriter.instance(context);
jjg@46 54 options = Options.instance(context);
jjg@46 55 }
jjg@46 56
jjg@46 57 void writeConstantPool() {
jjg@46 58 ConstantPool.Visitor<Integer, Void> v = new ConstantPool.Visitor<Integer,Void>() {
jjg@46 59 public Integer visitClass(CONSTANT_Class_info info, Void p) {
jjg@46 60 println("#" + info.name_index + ";\t// " + stringValue(info));
jjg@46 61 return 1;
jjg@46 62 }
jjg@46 63
jjg@46 64 public Integer visitDouble(CONSTANT_Double_info info, Void p) {
jjg@46 65 println(stringValue(info) + ";");
jjg@46 66 return 2;
jjg@46 67 }
jjg@46 68
jjg@46 69 public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) {
jjg@46 70 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 71 return 1;
jjg@46 72 }
jjg@46 73
jjg@46 74 public Integer visitFloat(CONSTANT_Float_info info, Void p) {
jjg@46 75 println(stringValue(info) + ";");
jjg@46 76 return 1;
jjg@46 77 }
jjg@46 78
jjg@46 79 public Integer visitInteger(CONSTANT_Integer_info info, Void p) {
jjg@46 80 println(stringValue(info) + ";");
jjg@46 81 return 1;
jjg@46 82 }
jjg@46 83
jjg@46 84 public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
jjg@46 85 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 86 return 1;
jjg@46 87 }
jjg@46 88
jjg@46 89 public Integer visitLong(CONSTANT_Long_info info, Void p) {
jjg@46 90 println(stringValue(info) + ";");
jjg@46 91 return 2;
jjg@46 92 }
jjg@46 93
jjg@46 94 public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
jjg@46 95 String tab = (options.compat ? "" : "\t"); // BUG 6622232 javap gets whitespace confused
jjg@46 96 println("#" + info.name_index + ":#" + info.type_index + ";" + tab + "// " + stringValue(info));
jjg@46 97 return 1;
jjg@46 98 }
jjg@46 99
jjg@46 100 public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) {
jjg@46 101 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 102 return 1;
jjg@46 103 }
jjg@46 104
jjg@46 105 public Integer visitString(CONSTANT_String_info info, Void p) {
jjg@46 106 println("#" + info.string_index + ";\t// " + stringValue(info));
jjg@46 107 return 1;
jjg@46 108 }
jjg@46 109
jjg@46 110 public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) {
jjg@46 111 println(stringValue(info) + ";");
jjg@46 112 return 1;
jjg@46 113 }
jjg@46 114
jjg@46 115 };
jjg@46 116 println(" Constant pool:");
jjg@46 117 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
jjg@46 118 int cpx = 1;
jjg@46 119 while (cpx < constant_pool.size()) {
jjg@46 120 try {
jjg@46 121 CPInfo cpInfo = constant_pool.get(cpx);
jjg@46 122 print("const #" + cpx + " = " + tagName(cpInfo.getTag()) + "\t");
jjg@46 123 cpx += cpInfo.accept(v, null);
jjg@46 124 } catch (ConstantPool.InvalidIndex ex) {
jjg@46 125 print("const #" + cpx); // should not happen
jjg@46 126 }
jjg@46 127 }
jjg@46 128 }
jjg@46 129
jjg@46 130 void write(int cpx) {
jjg@46 131 ClassFile classFile = classWriter.getClassFile();
jjg@46 132 if (cpx == 0) {
jjg@46 133 print("#0");
jjg@46 134 return;
jjg@46 135 }
jjg@46 136
jjg@46 137 CPInfo cpInfo;
jjg@46 138 try {
jjg@46 139 cpInfo = classFile.constant_pool.get(cpx);
jjg@46 140 } catch (ConstantPoolException e) {
jjg@46 141 print("#" + cpx);
jjg@46 142 return;
jjg@46 143 }
jjg@46 144
jjg@46 145 int tag = cpInfo.getTag();
jjg@46 146 switch (tag) {
jjg@46 147 case CONSTANT_Methodref:
jjg@46 148 case CONSTANT_InterfaceMethodref:
jjg@46 149 case CONSTANT_Fieldref:
jjg@46 150 // simplify references within this class
jjg@46 151 CPRefInfo ref = (CPRefInfo) cpInfo;
jjg@46 152 try {
jjg@46 153 if (ref.class_index == classFile.this_class)
jjg@46 154 cpInfo = classFile.constant_pool.get(ref.name_and_type_index);
jjg@46 155 } catch (ConstantPool.InvalidIndex e) {
jjg@46 156 // ignore, for now
jjg@46 157 }
jjg@46 158 }
jjg@46 159 print(tagName(tag) + " " + stringValue(cpInfo));
jjg@46 160 }
jjg@46 161
jjg@46 162 String tagName(int tag) {
jjg@46 163 switch (tag) {
jjg@46 164 case CONSTANT_Utf8:
jjg@46 165 return "Asciz";
jjg@46 166 case CONSTANT_Integer:
jjg@46 167 return "int";
jjg@46 168 case CONSTANT_Float:
jjg@46 169 return "float";
jjg@46 170 case CONSTANT_Long:
jjg@46 171 return "long";
jjg@46 172 case CONSTANT_Double:
jjg@46 173 return "double";
jjg@46 174 case CONSTANT_Class:
jjg@46 175 return "class";
jjg@46 176 case CONSTANT_String:
jjg@46 177 return "String";
jjg@46 178 case CONSTANT_Fieldref:
jjg@46 179 return "Field";
jjg@46 180 case CONSTANT_Methodref:
jjg@46 181 return "Method";
jjg@46 182 case CONSTANT_InterfaceMethodref:
jjg@46 183 return "InterfaceMethod";
jjg@46 184 case CONSTANT_NameAndType:
jjg@46 185 return "NameAndType";
jjg@46 186 default:
jjg@46 187 return "unknown tag";
jjg@46 188 }
jjg@46 189 }
jjg@46 190
jjg@46 191 String stringValue(int constant_pool_index) {
jjg@46 192 ClassFile classFile = classWriter.getClassFile();
jjg@46 193 try {
jjg@46 194 return stringValue(classFile.constant_pool.get(constant_pool_index));
jjg@46 195 } catch (ConstantPool.InvalidIndex e) {
jjg@46 196 return report(e);
jjg@46 197 }
jjg@46 198 }
jjg@46 199
jjg@46 200 String stringValue(CPInfo cpInfo) {
jjg@46 201 return stringValueVisitor.visit(cpInfo);
jjg@46 202 }
jjg@46 203
jjg@46 204 StringValueVisitor stringValueVisitor = new StringValueVisitor();
jjg@46 205
jjg@46 206 private class StringValueVisitor implements ConstantPool.Visitor<String, Void> {
jjg@46 207 public String visit(CPInfo info) {
jjg@46 208 return info.accept(this, null);
jjg@46 209 }
jjg@46 210
jjg@46 211 public String visitClass(CONSTANT_Class_info info, Void p) {
jjg@46 212 return getCheckedName(info);
jjg@46 213 }
jjg@46 214
jjg@46 215 String getCheckedName(CONSTANT_Class_info info) {
jjg@46 216 try {
jjg@46 217 return checkName(info.getName());
jjg@46 218 } catch (ConstantPoolException e) {
jjg@46 219 return report(e);
jjg@46 220 }
jjg@46 221 }
jjg@46 222
jjg@46 223 public String visitDouble(CONSTANT_Double_info info, Void p) {
jjg@46 224 return info.value + "d";
jjg@46 225 }
jjg@46 226
jjg@46 227 public String visitFieldref(CONSTANT_Fieldref_info info, Void p) {
jjg@46 228 return visitRef(info, p);
jjg@46 229 }
jjg@46 230
jjg@46 231 public String visitFloat(CONSTANT_Float_info info, Void p) {
jjg@46 232 return info.value + "f";
jjg@46 233 }
jjg@46 234
jjg@46 235 public String visitInteger(CONSTANT_Integer_info info, Void p) {
jjg@46 236 return String.valueOf(info.value);
jjg@46 237 }
jjg@46 238
jjg@46 239 public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
jjg@46 240 return visitRef(info, p);
jjg@46 241 }
jjg@46 242
jjg@46 243 public String visitLong(CONSTANT_Long_info info, Void p) {
jjg@46 244 return info.value + "l";
jjg@46 245 }
jjg@46 246
jjg@46 247 public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
jjg@46 248 return getCheckedName(info) + ":" + getType(info);
jjg@46 249 }
jjg@46 250
jjg@46 251 String getCheckedName(CONSTANT_NameAndType_info info) {
jjg@46 252 try {
jjg@46 253 return checkName(info.getName());
jjg@46 254 } catch (ConstantPoolException e) {
jjg@46 255 return report(e);
jjg@46 256 }
jjg@46 257 }
jjg@46 258
jjg@46 259 String getType(CONSTANT_NameAndType_info info) {
jjg@46 260 try {
jjg@46 261 return info.getType();
jjg@46 262 } catch (ConstantPoolException e) {
jjg@46 263 return report(e);
jjg@46 264 }
jjg@46 265 }
jjg@46 266
jjg@46 267 public String visitMethodref(CONSTANT_Methodref_info info, Void p) {
jjg@46 268 return visitRef(info, p);
jjg@46 269 }
jjg@46 270
jjg@46 271 public String visitString(CONSTANT_String_info info, Void p) {
jjg@46 272 try {
jjg@46 273 ClassFile classFile = classWriter.getClassFile();
jjg@46 274 int string_index = info.string_index;
jjg@46 275 return stringValue(classFile.constant_pool.getUTF8Info(string_index));
jjg@46 276 } catch (ConstantPoolException e) {
jjg@46 277 return report(e);
jjg@46 278 }
jjg@46 279 }
jjg@46 280
jjg@46 281 public String visitUtf8(CONSTANT_Utf8_info info, Void p) {
jjg@46 282 String s = info.value;
jjg@46 283 StringBuilder sb = new StringBuilder();
jjg@46 284 for (int i = 0; i < s.length(); i++) {
jjg@46 285 char c = s.charAt(i);
jjg@46 286 switch (c) {
jjg@46 287 case '\t':
jjg@46 288 sb.append('\\').append('t');
jjg@46 289 break;
jjg@46 290 case '\n':
jjg@46 291 sb.append('\\').append('n');
jjg@46 292 break;
jjg@46 293 case '\r':
jjg@46 294 sb.append('\\').append('r');
jjg@46 295 break;
jjg@46 296 case '\"':
jjg@46 297 sb.append('\\').append('\"');
jjg@46 298 break;
jjg@46 299 default:
jjg@46 300 sb.append(c);
jjg@46 301 }
jjg@46 302 }
jjg@46 303 return sb.toString();
jjg@46 304 }
jjg@46 305
jjg@46 306 String visitRef(CPRefInfo info, Void p) {
jjg@46 307 String cn = getCheckedClassName(info);
jjg@46 308 String nat;
jjg@46 309 try {
jjg@46 310 nat = stringValue(info.getNameAndTypeInfo());
jjg@46 311 } catch (ConstantPoolException e) {
jjg@46 312 nat = report(e);
jjg@46 313 }
jjg@46 314 return cn + "." + nat;
jjg@46 315 }
jjg@46 316
jjg@46 317 String getCheckedClassName(CPRefInfo info) {
jjg@46 318 try {
jjg@46 319 return checkName(info.getClassName());
jjg@46 320 } catch (ConstantPoolException e) {
jjg@46 321 return report(e);
jjg@46 322 }
jjg@46 323 }
jjg@46 324 }
jjg@46 325
jjg@46 326
jjg@46 327 /* If name is a valid binary name, return it; otherwise quote it. */
jjg@46 328 private static String checkName(String name) {
jjg@46 329 if (name == null)
jjg@46 330 return "null";
jjg@46 331
jjg@46 332 int len = name.length();
jjg@46 333 if (len == 0)
jjg@46 334 return "\"\"";
jjg@46 335
jjg@46 336 int cc = '/';
jjg@46 337 int cp;
jjg@46 338 for (int k = 0; k < len; k += Character.charCount(cp)) {
jjg@46 339 cp = name.codePointAt(k);
jjg@46 340 if ((cc == '/' && !Character.isJavaIdentifierStart(cp))
jjg@46 341 || (cp != '/' && !Character.isJavaIdentifierPart(cp))) {
jjg@46 342 return "\"" + name + "\"";
jjg@46 343 }
jjg@46 344 cc = cp;
jjg@46 345 }
jjg@46 346
jjg@46 347 return name;
jjg@46 348 }
jjg@46 349
jjg@46 350 private ClassWriter classWriter;
jjg@46 351 private Options options;
jjg@46 352 }

mercurial