Wed, 17 Nov 2010 15:07:43 -0800
7000973: isBogus needs to be called on the to-be-returned entry, not on the current entry
Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com
jjg@46 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2007, 2008, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@46 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * 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@581 | 37 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 38 | * If 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@354 | 65 | print("#" + info.name_index); |
jjg@348 | 66 | tab(); |
jjg@348 | 67 | println("// " + stringValue(info)); |
jjg@46 | 68 | return 1; |
jjg@46 | 69 | } |
jjg@46 | 70 | |
jjg@46 | 71 | public Integer visitDouble(CONSTANT_Double_info info, Void p) { |
jjg@354 | 72 | println(stringValue(info)); |
jjg@46 | 73 | return 2; |
jjg@46 | 74 | } |
jjg@46 | 75 | |
jjg@46 | 76 | public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) { |
jjg@354 | 77 | print("#" + info.class_index + ".#" + info.name_and_type_index); |
jjg@348 | 78 | tab(); |
jjg@348 | 79 | println("// " + stringValue(info)); |
jjg@46 | 80 | return 1; |
jjg@46 | 81 | } |
jjg@46 | 82 | |
jjg@46 | 83 | public Integer visitFloat(CONSTANT_Float_info info, Void p) { |
jjg@354 | 84 | println(stringValue(info)); |
jjg@46 | 85 | return 1; |
jjg@46 | 86 | } |
jjg@46 | 87 | |
jjg@46 | 88 | public Integer visitInteger(CONSTANT_Integer_info info, Void p) { |
jjg@354 | 89 | println(stringValue(info)); |
jjg@46 | 90 | return 1; |
jjg@46 | 91 | } |
jjg@46 | 92 | |
jjg@46 | 93 | public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) { |
jjg@354 | 94 | print("#" + info.class_index + ".#" + info.name_and_type_index); |
jjg@348 | 95 | tab(); |
jjg@348 | 96 | println("// " + stringValue(info)); |
jjg@46 | 97 | return 1; |
jjg@46 | 98 | } |
jjg@46 | 99 | |
jjg@46 | 100 | public Integer visitLong(CONSTANT_Long_info info, Void p) { |
jjg@354 | 101 | println(stringValue(info)); |
jjg@46 | 102 | return 2; |
jjg@46 | 103 | } |
jjg@46 | 104 | |
jjg@46 | 105 | public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) { |
jjg@354 | 106 | print("#" + info.name_index + ":#" + info.type_index); |
jjg@348 | 107 | tab(); |
jjg@348 | 108 | println("// " + stringValue(info)); |
jjg@46 | 109 | return 1; |
jjg@46 | 110 | } |
jjg@46 | 111 | |
jjg@46 | 112 | public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) { |
jjg@354 | 113 | print("#" + info.class_index + ".#" + info.name_and_type_index); |
jjg@348 | 114 | tab(); |
jjg@348 | 115 | println("// " + stringValue(info)); |
jjg@46 | 116 | return 1; |
jjg@46 | 117 | } |
jjg@46 | 118 | |
jjg@46 | 119 | public Integer visitString(CONSTANT_String_info info, Void p) { |
jjg@354 | 120 | print("#" + info.string_index); |
jjg@348 | 121 | tab(); |
jjg@348 | 122 | println("// " + stringValue(info)); |
jjg@46 | 123 | return 1; |
jjg@46 | 124 | } |
jjg@46 | 125 | |
jjg@46 | 126 | public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) { |
jjg@354 | 127 | println(stringValue(info)); |
jjg@46 | 128 | return 1; |
jjg@46 | 129 | } |
jjg@46 | 130 | |
jjg@46 | 131 | }; |
jjg@348 | 132 | println("Constant pool:"); |
jjg@348 | 133 | indent(+1); |
jjg@348 | 134 | int width = String.valueOf(constant_pool.size()).length() + 1; |
jjg@46 | 135 | int cpx = 1; |
jjg@46 | 136 | while (cpx < constant_pool.size()) { |
jjg@355 | 137 | print(String.format("%" + width + "s", ("#" + cpx))); |
jjg@46 | 138 | try { |
jjg@46 | 139 | CPInfo cpInfo = constant_pool.get(cpx); |
jjg@355 | 140 | print(String.format(" = %-18s ", cpTagName(cpInfo))); |
jjg@46 | 141 | cpx += cpInfo.accept(v, null); |
jjg@46 | 142 | } catch (ConstantPool.InvalidIndex ex) { |
jjg@348 | 143 | // should not happen |
jjg@46 | 144 | } |
jjg@46 | 145 | } |
jjg@348 | 146 | indent(-1); |
jjg@46 | 147 | } |
jjg@46 | 148 | |
jjg@300 | 149 | protected void write(int cpx) { |
jjg@46 | 150 | ClassFile classFile = classWriter.getClassFile(); |
jjg@46 | 151 | if (cpx == 0) { |
jjg@46 | 152 | print("#0"); |
jjg@46 | 153 | return; |
jjg@46 | 154 | } |
jjg@46 | 155 | |
jjg@46 | 156 | CPInfo cpInfo; |
jjg@46 | 157 | try { |
jjg@46 | 158 | cpInfo = classFile.constant_pool.get(cpx); |
jjg@46 | 159 | } catch (ConstantPoolException e) { |
jjg@46 | 160 | print("#" + cpx); |
jjg@46 | 161 | return; |
jjg@46 | 162 | } |
jjg@46 | 163 | |
jjg@46 | 164 | int tag = cpInfo.getTag(); |
jjg@46 | 165 | switch (tag) { |
jjg@46 | 166 | case CONSTANT_Methodref: |
jjg@46 | 167 | case CONSTANT_InterfaceMethodref: |
jjg@46 | 168 | case CONSTANT_Fieldref: |
jjg@46 | 169 | // simplify references within this class |
jjg@46 | 170 | CPRefInfo ref = (CPRefInfo) cpInfo; |
jjg@46 | 171 | try { |
jjg@46 | 172 | if (ref.class_index == classFile.this_class) |
jjg@46 | 173 | cpInfo = classFile.constant_pool.get(ref.name_and_type_index); |
jjg@46 | 174 | } catch (ConstantPool.InvalidIndex e) { |
jjg@46 | 175 | // ignore, for now |
jjg@46 | 176 | } |
jjg@46 | 177 | } |
jjg@46 | 178 | print(tagName(tag) + " " + stringValue(cpInfo)); |
jjg@46 | 179 | } |
jjg@46 | 180 | |
jjg@355 | 181 | String cpTagName(CPInfo cpInfo) { |
jjg@355 | 182 | String n = cpInfo.getClass().getSimpleName(); |
jjg@355 | 183 | return n.replace("CONSTANT_", "").replace("_info", ""); |
jjg@355 | 184 | } |
jjg@355 | 185 | |
jjg@46 | 186 | String tagName(int tag) { |
jjg@46 | 187 | switch (tag) { |
jjg@46 | 188 | case CONSTANT_Utf8: |
jjg@355 | 189 | return "Utf8"; |
jjg@46 | 190 | case CONSTANT_Integer: |
jjg@46 | 191 | return "int"; |
jjg@46 | 192 | case CONSTANT_Float: |
jjg@46 | 193 | return "float"; |
jjg@46 | 194 | case CONSTANT_Long: |
jjg@46 | 195 | return "long"; |
jjg@46 | 196 | case CONSTANT_Double: |
jjg@46 | 197 | return "double"; |
jjg@46 | 198 | case CONSTANT_Class: |
jjg@46 | 199 | return "class"; |
jjg@46 | 200 | case CONSTANT_String: |
jjg@46 | 201 | return "String"; |
jjg@46 | 202 | case CONSTANT_Fieldref: |
jjg@46 | 203 | return "Field"; |
jjg@46 | 204 | case CONSTANT_Methodref: |
jjg@46 | 205 | return "Method"; |
jjg@46 | 206 | case CONSTANT_InterfaceMethodref: |
jjg@46 | 207 | return "InterfaceMethod"; |
jjg@46 | 208 | case CONSTANT_NameAndType: |
jjg@46 | 209 | return "NameAndType"; |
jjg@46 | 210 | default: |
jjg@355 | 211 | return "(unknown tag)"; |
jjg@46 | 212 | } |
jjg@46 | 213 | } |
jjg@46 | 214 | |
jjg@46 | 215 | String stringValue(int constant_pool_index) { |
jjg@46 | 216 | ClassFile classFile = classWriter.getClassFile(); |
jjg@46 | 217 | try { |
jjg@46 | 218 | return stringValue(classFile.constant_pool.get(constant_pool_index)); |
jjg@46 | 219 | } catch (ConstantPool.InvalidIndex e) { |
jjg@46 | 220 | return report(e); |
jjg@46 | 221 | } |
jjg@46 | 222 | } |
jjg@46 | 223 | |
jjg@46 | 224 | String stringValue(CPInfo cpInfo) { |
jjg@46 | 225 | return stringValueVisitor.visit(cpInfo); |
jjg@46 | 226 | } |
jjg@46 | 227 | |
jjg@46 | 228 | StringValueVisitor stringValueVisitor = new StringValueVisitor(); |
jjg@46 | 229 | |
jjg@46 | 230 | private class StringValueVisitor implements ConstantPool.Visitor<String, Void> { |
jjg@46 | 231 | public String visit(CPInfo info) { |
jjg@46 | 232 | return info.accept(this, null); |
jjg@46 | 233 | } |
jjg@46 | 234 | |
jjg@46 | 235 | public String visitClass(CONSTANT_Class_info info, Void p) { |
jjg@46 | 236 | return getCheckedName(info); |
jjg@46 | 237 | } |
jjg@46 | 238 | |
jjg@46 | 239 | String getCheckedName(CONSTANT_Class_info info) { |
jjg@46 | 240 | try { |
jjg@46 | 241 | return checkName(info.getName()); |
jjg@46 | 242 | } catch (ConstantPoolException e) { |
jjg@46 | 243 | return report(e); |
jjg@46 | 244 | } |
jjg@46 | 245 | } |
jjg@46 | 246 | |
jjg@46 | 247 | public String visitDouble(CONSTANT_Double_info info, Void p) { |
jjg@46 | 248 | return info.value + "d"; |
jjg@46 | 249 | } |
jjg@46 | 250 | |
jjg@46 | 251 | public String visitFieldref(CONSTANT_Fieldref_info info, Void p) { |
jjg@46 | 252 | return visitRef(info, p); |
jjg@46 | 253 | } |
jjg@46 | 254 | |
jjg@46 | 255 | public String visitFloat(CONSTANT_Float_info info, Void p) { |
jjg@46 | 256 | return info.value + "f"; |
jjg@46 | 257 | } |
jjg@46 | 258 | |
jjg@46 | 259 | public String visitInteger(CONSTANT_Integer_info info, Void p) { |
jjg@46 | 260 | return String.valueOf(info.value); |
jjg@46 | 261 | } |
jjg@46 | 262 | |
jjg@46 | 263 | public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) { |
jjg@46 | 264 | return visitRef(info, p); |
jjg@46 | 265 | } |
jjg@46 | 266 | |
jjg@46 | 267 | public String visitLong(CONSTANT_Long_info info, Void p) { |
jjg@46 | 268 | return info.value + "l"; |
jjg@46 | 269 | } |
jjg@46 | 270 | |
jjg@46 | 271 | public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) { |
jjg@46 | 272 | return getCheckedName(info) + ":" + getType(info); |
jjg@46 | 273 | } |
jjg@46 | 274 | |
jjg@46 | 275 | String getCheckedName(CONSTANT_NameAndType_info info) { |
jjg@46 | 276 | try { |
jjg@46 | 277 | return checkName(info.getName()); |
jjg@46 | 278 | } catch (ConstantPoolException e) { |
jjg@46 | 279 | return report(e); |
jjg@46 | 280 | } |
jjg@46 | 281 | } |
jjg@46 | 282 | |
jjg@46 | 283 | String getType(CONSTANT_NameAndType_info info) { |
jjg@46 | 284 | try { |
jjg@46 | 285 | return info.getType(); |
jjg@46 | 286 | } catch (ConstantPoolException e) { |
jjg@46 | 287 | return report(e); |
jjg@46 | 288 | } |
jjg@46 | 289 | } |
jjg@46 | 290 | |
jjg@46 | 291 | public String visitMethodref(CONSTANT_Methodref_info info, Void p) { |
jjg@46 | 292 | return visitRef(info, p); |
jjg@46 | 293 | } |
jjg@46 | 294 | |
jjg@46 | 295 | public String visitString(CONSTANT_String_info info, Void p) { |
jjg@46 | 296 | try { |
jjg@46 | 297 | ClassFile classFile = classWriter.getClassFile(); |
jjg@46 | 298 | int string_index = info.string_index; |
jjg@46 | 299 | return stringValue(classFile.constant_pool.getUTF8Info(string_index)); |
jjg@46 | 300 | } catch (ConstantPoolException e) { |
jjg@46 | 301 | return report(e); |
jjg@46 | 302 | } |
jjg@46 | 303 | } |
jjg@46 | 304 | |
jjg@46 | 305 | public String visitUtf8(CONSTANT_Utf8_info info, Void p) { |
jjg@46 | 306 | String s = info.value; |
jjg@46 | 307 | StringBuilder sb = new StringBuilder(); |
jjg@46 | 308 | for (int i = 0; i < s.length(); i++) { |
jjg@46 | 309 | char c = s.charAt(i); |
jjg@46 | 310 | switch (c) { |
jjg@46 | 311 | case '\t': |
jjg@46 | 312 | sb.append('\\').append('t'); |
jjg@46 | 313 | break; |
jjg@46 | 314 | case '\n': |
jjg@46 | 315 | sb.append('\\').append('n'); |
jjg@46 | 316 | break; |
jjg@46 | 317 | case '\r': |
jjg@46 | 318 | sb.append('\\').append('r'); |
jjg@46 | 319 | break; |
jjg@46 | 320 | case '\"': |
jjg@46 | 321 | sb.append('\\').append('\"'); |
jjg@46 | 322 | break; |
jjg@46 | 323 | default: |
jjg@46 | 324 | sb.append(c); |
jjg@46 | 325 | } |
jjg@46 | 326 | } |
jjg@46 | 327 | return sb.toString(); |
jjg@46 | 328 | } |
jjg@46 | 329 | |
jjg@46 | 330 | String visitRef(CPRefInfo info, Void p) { |
jjg@46 | 331 | String cn = getCheckedClassName(info); |
jjg@46 | 332 | String nat; |
jjg@46 | 333 | try { |
jjg@46 | 334 | nat = stringValue(info.getNameAndTypeInfo()); |
jjg@46 | 335 | } catch (ConstantPoolException e) { |
jjg@46 | 336 | nat = report(e); |
jjg@46 | 337 | } |
jjg@46 | 338 | return cn + "." + nat; |
jjg@46 | 339 | } |
jjg@46 | 340 | |
jjg@46 | 341 | String getCheckedClassName(CPRefInfo info) { |
jjg@46 | 342 | try { |
jjg@46 | 343 | return checkName(info.getClassName()); |
jjg@46 | 344 | } catch (ConstantPoolException e) { |
jjg@46 | 345 | return report(e); |
jjg@46 | 346 | } |
jjg@46 | 347 | } |
jjg@46 | 348 | } |
jjg@46 | 349 | |
jjg@46 | 350 | |
jjg@46 | 351 | /* If name is a valid binary name, return it; otherwise quote it. */ |
jjg@46 | 352 | private static String checkName(String name) { |
jjg@46 | 353 | if (name == null) |
jjg@46 | 354 | return "null"; |
jjg@46 | 355 | |
jjg@46 | 356 | int len = name.length(); |
jjg@46 | 357 | if (len == 0) |
jjg@46 | 358 | return "\"\""; |
jjg@46 | 359 | |
jjg@46 | 360 | int cc = '/'; |
jjg@46 | 361 | int cp; |
jjg@46 | 362 | for (int k = 0; k < len; k += Character.charCount(cp)) { |
jjg@46 | 363 | cp = name.codePointAt(k); |
jjg@46 | 364 | if ((cc == '/' && !Character.isJavaIdentifierStart(cp)) |
jjg@46 | 365 | || (cp != '/' && !Character.isJavaIdentifierPart(cp))) { |
jrose@267 | 366 | return "\"" + addEscapes(name) + "\""; |
jjg@46 | 367 | } |
jjg@46 | 368 | cc = cp; |
jjg@46 | 369 | } |
jjg@46 | 370 | |
jjg@46 | 371 | return name; |
jjg@46 | 372 | } |
jjg@46 | 373 | |
jrose@267 | 374 | /* If name requires escapes, put them in, so it can be a string body. */ |
jrose@267 | 375 | private static String addEscapes(String name) { |
jrose@267 | 376 | String esc = "\\\"\n\t"; |
jrose@267 | 377 | String rep = "\\\"nt"; |
jrose@267 | 378 | StringBuilder buf = null; |
jrose@267 | 379 | int nextk = 0; |
jrose@267 | 380 | int len = name.length(); |
jrose@267 | 381 | for (int k = 0; k < len; k++) { |
jrose@267 | 382 | char cp = name.charAt(k); |
jrose@267 | 383 | int n = esc.indexOf(cp); |
jrose@267 | 384 | if (n >= 0) { |
jrose@267 | 385 | if (buf == null) |
jrose@267 | 386 | buf = new StringBuilder(len * 2); |
jrose@267 | 387 | if (nextk < k) |
jrose@267 | 388 | buf.append(name, nextk, k); |
jrose@267 | 389 | buf.append('\\'); |
jrose@267 | 390 | buf.append(rep.charAt(n)); |
jrose@267 | 391 | nextk = k+1; |
jrose@267 | 392 | } |
jrose@267 | 393 | } |
jrose@267 | 394 | if (buf == null) |
jrose@267 | 395 | return name; |
jrose@267 | 396 | if (nextk < len) |
jrose@267 | 397 | buf.append(name, nextk, len); |
jrose@267 | 398 | return buf.toString(); |
jrose@267 | 399 | } |
jrose@267 | 400 | |
jjg@46 | 401 | private ClassWriter classWriter; |
jjg@46 | 402 | private Options options; |
jjg@46 | 403 | } |