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

Thu, 30 Jul 2009 09:18:55 -0700

author
jjg
date
Thu, 30 Jul 2009 09:18:55 -0700
changeset 346
e33efb09ed75
parent 300
ed989c347b3c
child 348
743f17b55b44
permissions
-rw-r--r--

4880672: javap does not output inner interfaces of an interface
Reviewed-by: mcimadamore

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 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@300 43 public 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@300 57 protected void writeConstantPool() {
jjg@300 58 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
jjg@300 59 writeConstantPool(constant_pool);
jjg@300 60 }
jjg@300 61
jjg@300 62 protected void writeConstantPool(ConstantPool constant_pool) {
jjg@46 63 ConstantPool.Visitor<Integer, Void> v = new ConstantPool.Visitor<Integer,Void>() {
jjg@46 64 public Integer visitClass(CONSTANT_Class_info info, Void p) {
jjg@46 65 println("#" + info.name_index + ";\t// " + stringValue(info));
jjg@46 66 return 1;
jjg@46 67 }
jjg@46 68
jjg@46 69 public Integer visitDouble(CONSTANT_Double_info info, Void p) {
jjg@46 70 println(stringValue(info) + ";");
jjg@46 71 return 2;
jjg@46 72 }
jjg@46 73
jjg@46 74 public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) {
jjg@46 75 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 76 return 1;
jjg@46 77 }
jjg@46 78
jjg@46 79 public Integer visitFloat(CONSTANT_Float_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 visitInteger(CONSTANT_Integer_info info, Void p) {
jjg@46 85 println(stringValue(info) + ";");
jjg@46 86 return 1;
jjg@46 87 }
jjg@46 88
jjg@46 89 public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
jjg@46 90 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 91 return 1;
jjg@46 92 }
jjg@46 93
jjg@46 94 public Integer visitLong(CONSTANT_Long_info info, Void p) {
jjg@46 95 println(stringValue(info) + ";");
jjg@46 96 return 2;
jjg@46 97 }
jjg@46 98
jjg@46 99 public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
jjg@46 100 String tab = (options.compat ? "" : "\t"); // BUG 6622232 javap gets whitespace confused
jjg@46 101 println("#" + info.name_index + ":#" + info.type_index + ";" + tab + "// " + stringValue(info));
jjg@46 102 return 1;
jjg@46 103 }
jjg@46 104
jjg@46 105 public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) {
jjg@46 106 println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t// " + stringValue(info));
jjg@46 107 return 1;
jjg@46 108 }
jjg@46 109
jjg@46 110 public Integer visitString(CONSTANT_String_info info, Void p) {
jjg@46 111 println("#" + info.string_index + ";\t// " + stringValue(info));
jjg@46 112 return 1;
jjg@46 113 }
jjg@46 114
jjg@46 115 public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) {
jjg@46 116 println(stringValue(info) + ";");
jjg@46 117 return 1;
jjg@46 118 }
jjg@46 119
jjg@46 120 };
jjg@46 121 println(" Constant pool:");
jjg@46 122 int cpx = 1;
jjg@46 123 while (cpx < constant_pool.size()) {
jjg@46 124 try {
jjg@46 125 CPInfo cpInfo = constant_pool.get(cpx);
jjg@46 126 print("const #" + cpx + " = " + tagName(cpInfo.getTag()) + "\t");
jjg@46 127 cpx += cpInfo.accept(v, null);
jjg@46 128 } catch (ConstantPool.InvalidIndex ex) {
jjg@46 129 print("const #" + cpx); // should not happen
jjg@46 130 }
jjg@46 131 }
jjg@46 132 }
jjg@46 133
jjg@300 134 protected void write(int cpx) {
jjg@46 135 ClassFile classFile = classWriter.getClassFile();
jjg@46 136 if (cpx == 0) {
jjg@46 137 print("#0");
jjg@46 138 return;
jjg@46 139 }
jjg@46 140
jjg@46 141 CPInfo cpInfo;
jjg@46 142 try {
jjg@46 143 cpInfo = classFile.constant_pool.get(cpx);
jjg@46 144 } catch (ConstantPoolException e) {
jjg@46 145 print("#" + cpx);
jjg@46 146 return;
jjg@46 147 }
jjg@46 148
jjg@46 149 int tag = cpInfo.getTag();
jjg@46 150 switch (tag) {
jjg@46 151 case CONSTANT_Methodref:
jjg@46 152 case CONSTANT_InterfaceMethodref:
jjg@46 153 case CONSTANT_Fieldref:
jjg@46 154 // simplify references within this class
jjg@46 155 CPRefInfo ref = (CPRefInfo) cpInfo;
jjg@46 156 try {
jjg@46 157 if (ref.class_index == classFile.this_class)
jjg@46 158 cpInfo = classFile.constant_pool.get(ref.name_and_type_index);
jjg@46 159 } catch (ConstantPool.InvalidIndex e) {
jjg@46 160 // ignore, for now
jjg@46 161 }
jjg@46 162 }
jjg@46 163 print(tagName(tag) + " " + stringValue(cpInfo));
jjg@46 164 }
jjg@46 165
jjg@46 166 String tagName(int tag) {
jjg@46 167 switch (tag) {
jjg@46 168 case CONSTANT_Utf8:
jjg@46 169 return "Asciz";
jjg@46 170 case CONSTANT_Integer:
jjg@46 171 return "int";
jjg@46 172 case CONSTANT_Float:
jjg@46 173 return "float";
jjg@46 174 case CONSTANT_Long:
jjg@46 175 return "long";
jjg@46 176 case CONSTANT_Double:
jjg@46 177 return "double";
jjg@46 178 case CONSTANT_Class:
jjg@46 179 return "class";
jjg@46 180 case CONSTANT_String:
jjg@46 181 return "String";
jjg@46 182 case CONSTANT_Fieldref:
jjg@46 183 return "Field";
jjg@46 184 case CONSTANT_Methodref:
jjg@46 185 return "Method";
jjg@46 186 case CONSTANT_InterfaceMethodref:
jjg@46 187 return "InterfaceMethod";
jjg@46 188 case CONSTANT_NameAndType:
jjg@46 189 return "NameAndType";
jjg@46 190 default:
jjg@46 191 return "unknown tag";
jjg@46 192 }
jjg@46 193 }
jjg@46 194
jjg@46 195 String stringValue(int constant_pool_index) {
jjg@46 196 ClassFile classFile = classWriter.getClassFile();
jjg@46 197 try {
jjg@46 198 return stringValue(classFile.constant_pool.get(constant_pool_index));
jjg@46 199 } catch (ConstantPool.InvalidIndex e) {
jjg@46 200 return report(e);
jjg@46 201 }
jjg@46 202 }
jjg@46 203
jjg@46 204 String stringValue(CPInfo cpInfo) {
jjg@46 205 return stringValueVisitor.visit(cpInfo);
jjg@46 206 }
jjg@46 207
jjg@46 208 StringValueVisitor stringValueVisitor = new StringValueVisitor();
jjg@46 209
jjg@46 210 private class StringValueVisitor implements ConstantPool.Visitor<String, Void> {
jjg@46 211 public String visit(CPInfo info) {
jjg@46 212 return info.accept(this, null);
jjg@46 213 }
jjg@46 214
jjg@46 215 public String visitClass(CONSTANT_Class_info info, Void p) {
jjg@46 216 return getCheckedName(info);
jjg@46 217 }
jjg@46 218
jjg@46 219 String getCheckedName(CONSTANT_Class_info info) {
jjg@46 220 try {
jjg@46 221 return checkName(info.getName());
jjg@46 222 } catch (ConstantPoolException e) {
jjg@46 223 return report(e);
jjg@46 224 }
jjg@46 225 }
jjg@46 226
jjg@46 227 public String visitDouble(CONSTANT_Double_info info, Void p) {
jjg@46 228 return info.value + "d";
jjg@46 229 }
jjg@46 230
jjg@46 231 public String visitFieldref(CONSTANT_Fieldref_info info, Void p) {
jjg@46 232 return visitRef(info, p);
jjg@46 233 }
jjg@46 234
jjg@46 235 public String visitFloat(CONSTANT_Float_info info, Void p) {
jjg@46 236 return info.value + "f";
jjg@46 237 }
jjg@46 238
jjg@46 239 public String visitInteger(CONSTANT_Integer_info info, Void p) {
jjg@46 240 return String.valueOf(info.value);
jjg@46 241 }
jjg@46 242
jjg@46 243 public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
jjg@46 244 return visitRef(info, p);
jjg@46 245 }
jjg@46 246
jjg@46 247 public String visitLong(CONSTANT_Long_info info, Void p) {
jjg@46 248 return info.value + "l";
jjg@46 249 }
jjg@46 250
jjg@46 251 public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
jjg@46 252 return getCheckedName(info) + ":" + getType(info);
jjg@46 253 }
jjg@46 254
jjg@46 255 String getCheckedName(CONSTANT_NameAndType_info info) {
jjg@46 256 try {
jjg@46 257 return checkName(info.getName());
jjg@46 258 } catch (ConstantPoolException e) {
jjg@46 259 return report(e);
jjg@46 260 }
jjg@46 261 }
jjg@46 262
jjg@46 263 String getType(CONSTANT_NameAndType_info info) {
jjg@46 264 try {
jjg@46 265 return info.getType();
jjg@46 266 } catch (ConstantPoolException e) {
jjg@46 267 return report(e);
jjg@46 268 }
jjg@46 269 }
jjg@46 270
jjg@46 271 public String visitMethodref(CONSTANT_Methodref_info info, Void p) {
jjg@46 272 return visitRef(info, p);
jjg@46 273 }
jjg@46 274
jjg@46 275 public String visitString(CONSTANT_String_info info, Void p) {
jjg@46 276 try {
jjg@46 277 ClassFile classFile = classWriter.getClassFile();
jjg@46 278 int string_index = info.string_index;
jjg@46 279 return stringValue(classFile.constant_pool.getUTF8Info(string_index));
jjg@46 280 } catch (ConstantPoolException e) {
jjg@46 281 return report(e);
jjg@46 282 }
jjg@46 283 }
jjg@46 284
jjg@46 285 public String visitUtf8(CONSTANT_Utf8_info info, Void p) {
jjg@46 286 String s = info.value;
jjg@46 287 StringBuilder sb = new StringBuilder();
jjg@46 288 for (int i = 0; i < s.length(); i++) {
jjg@46 289 char c = s.charAt(i);
jjg@46 290 switch (c) {
jjg@46 291 case '\t':
jjg@46 292 sb.append('\\').append('t');
jjg@46 293 break;
jjg@46 294 case '\n':
jjg@46 295 sb.append('\\').append('n');
jjg@46 296 break;
jjg@46 297 case '\r':
jjg@46 298 sb.append('\\').append('r');
jjg@46 299 break;
jjg@46 300 case '\"':
jjg@46 301 sb.append('\\').append('\"');
jjg@46 302 break;
jjg@46 303 default:
jjg@46 304 sb.append(c);
jjg@46 305 }
jjg@46 306 }
jjg@46 307 return sb.toString();
jjg@46 308 }
jjg@46 309
jjg@46 310 String visitRef(CPRefInfo info, Void p) {
jjg@46 311 String cn = getCheckedClassName(info);
jjg@46 312 String nat;
jjg@46 313 try {
jjg@46 314 nat = stringValue(info.getNameAndTypeInfo());
jjg@46 315 } catch (ConstantPoolException e) {
jjg@46 316 nat = report(e);
jjg@46 317 }
jjg@46 318 return cn + "." + nat;
jjg@46 319 }
jjg@46 320
jjg@46 321 String getCheckedClassName(CPRefInfo info) {
jjg@46 322 try {
jjg@46 323 return checkName(info.getClassName());
jjg@46 324 } catch (ConstantPoolException e) {
jjg@46 325 return report(e);
jjg@46 326 }
jjg@46 327 }
jjg@46 328 }
jjg@46 329
jjg@46 330
jjg@46 331 /* If name is a valid binary name, return it; otherwise quote it. */
jjg@46 332 private static String checkName(String name) {
jjg@46 333 if (name == null)
jjg@46 334 return "null";
jjg@46 335
jjg@46 336 int len = name.length();
jjg@46 337 if (len == 0)
jjg@46 338 return "\"\"";
jjg@46 339
jjg@46 340 int cc = '/';
jjg@46 341 int cp;
jjg@46 342 for (int k = 0; k < len; k += Character.charCount(cp)) {
jjg@46 343 cp = name.codePointAt(k);
jjg@46 344 if ((cc == '/' && !Character.isJavaIdentifierStart(cp))
jjg@46 345 || (cp != '/' && !Character.isJavaIdentifierPart(cp))) {
jrose@267 346 return "\"" + addEscapes(name) + "\"";
jjg@46 347 }
jjg@46 348 cc = cp;
jjg@46 349 }
jjg@46 350
jjg@46 351 return name;
jjg@46 352 }
jjg@46 353
jrose@267 354 /* If name requires escapes, put them in, so it can be a string body. */
jrose@267 355 private static String addEscapes(String name) {
jrose@267 356 String esc = "\\\"\n\t";
jrose@267 357 String rep = "\\\"nt";
jrose@267 358 StringBuilder buf = null;
jrose@267 359 int nextk = 0;
jrose@267 360 int len = name.length();
jrose@267 361 for (int k = 0; k < len; k++) {
jrose@267 362 char cp = name.charAt(k);
jrose@267 363 int n = esc.indexOf(cp);
jrose@267 364 if (n >= 0) {
jrose@267 365 if (buf == null)
jrose@267 366 buf = new StringBuilder(len * 2);
jrose@267 367 if (nextk < k)
jrose@267 368 buf.append(name, nextk, k);
jrose@267 369 buf.append('\\');
jrose@267 370 buf.append(rep.charAt(n));
jrose@267 371 nextk = k+1;
jrose@267 372 }
jrose@267 373 }
jrose@267 374 if (buf == null)
jrose@267 375 return name;
jrose@267 376 if (nextk < len)
jrose@267 377 buf.append(name, nextk, len);
jrose@267 378 return buf.toString();
jrose@267 379 }
jrose@267 380
jjg@46 381 private ClassWriter classWriter;
jjg@46 382 private Options options;
jjg@46 383 }

mercurial