jjg@46: /* xdono@54: * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. jjg@46: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@46: * jjg@46: * This code is free software; you can redistribute it and/or modify it jjg@46: * under the terms of the GNU General Public License version 2 only, as jjg@46: * published by the Free Software Foundation. Sun designates this jjg@46: * particular file as subject to the "Classpath" exception as provided jjg@46: * by Sun in the LICENSE file that accompanied this code. jjg@46: * jjg@46: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@46: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@46: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@46: * version 2 for more details (a copy is included in the LICENSE file that jjg@46: * accompanied this code). jjg@46: * jjg@46: * You should have received a copy of the GNU General Public License version jjg@46: * 2 along with this work; if not, write to the Free Software Foundation, jjg@46: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@46: * jjg@46: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@46: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@46: * have any questions. jjg@46: */ jjg@46: jjg@46: package com.sun.tools.javap; jjg@46: jjg@46: import com.sun.tools.classfile.ClassFile; jjg@46: import com.sun.tools.classfile.ConstantPool; jjg@46: import com.sun.tools.classfile.ConstantPoolException; jjg@46: jjg@46: import static com.sun.tools.classfile.ConstantPool.*; jjg@46: jjg@46: /* jjg@46: * Write a constant pool entry. jjg@46: * jjg@46: *

This is NOT part of any API supported by Sun Microsystems. If jjg@46: * you write code that depends on this, you do so at your own risk. jjg@46: * This code and its internal interfaces are subject to change or jjg@46: * deletion without notice. jjg@46: */ jjg@46: public class ConstantWriter extends BasicWriter { jjg@300: public static ConstantWriter instance(Context context) { jjg@46: ConstantWriter instance = context.get(ConstantWriter.class); jjg@46: if (instance == null) jjg@46: instance = new ConstantWriter(context); jjg@46: return instance; jjg@46: } jjg@46: jjg@46: protected ConstantWriter(Context context) { jjg@46: super(context); jjg@46: context.put(ConstantWriter.class, this); jjg@46: classWriter = ClassWriter.instance(context); jjg@46: options = Options.instance(context); jjg@46: } jjg@46: jjg@300: protected void writeConstantPool() { jjg@300: ConstantPool constant_pool = classWriter.getClassFile().constant_pool; jjg@300: writeConstantPool(constant_pool); jjg@300: } jjg@300: jjg@300: protected void writeConstantPool(ConstantPool constant_pool) { jjg@46: ConstantPool.Visitor v = new ConstantPool.Visitor() { jjg@46: public Integer visitClass(CONSTANT_Class_info info, Void p) { jjg@348: print("#" + info.name_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitDouble(CONSTANT_Double_info info, Void p) { jjg@46: println(stringValue(info) + ";"); jjg@46: return 2; jjg@46: } jjg@46: jjg@46: public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) { jjg@348: print("#" + info.class_index + ".#" + info.name_and_type_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitFloat(CONSTANT_Float_info info, Void p) { jjg@46: println(stringValue(info) + ";"); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitInteger(CONSTANT_Integer_info info, Void p) { jjg@46: println(stringValue(info) + ";"); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) { jjg@348: print("#" + info.class_index + ".#" + info.name_and_type_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitLong(CONSTANT_Long_info info, Void p) { jjg@46: println(stringValue(info) + ";"); jjg@46: return 2; jjg@46: } jjg@46: jjg@46: public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) { jjg@348: print("#" + info.name_index + ":#" + info.type_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) { jjg@348: print("#" + info.class_index + ".#" + info.name_and_type_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitString(CONSTANT_String_info info, Void p) { jjg@348: print("#" + info.string_index + ";"); jjg@348: tab(); jjg@348: println("// " + stringValue(info)); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) { jjg@46: println(stringValue(info) + ";"); jjg@46: return 1; jjg@46: } jjg@46: jjg@46: }; jjg@348: println("Constant pool:"); jjg@348: indent(+1); jjg@348: int width = String.valueOf(constant_pool.size()).length() + 1; jjg@46: int cpx = 1; jjg@46: while (cpx < constant_pool.size()) { jjg@348: print(String.format("const %" + width + "s", ("#" + cpx))); jjg@46: try { jjg@46: CPInfo cpInfo = constant_pool.get(cpx); jjg@348: print(String.format(" = %-15s ", tagName(cpInfo.getTag()))); jjg@46: cpx += cpInfo.accept(v, null); jjg@46: } catch (ConstantPool.InvalidIndex ex) { jjg@348: // should not happen jjg@46: } jjg@46: } jjg@348: indent(-1); jjg@46: } jjg@46: jjg@300: protected void write(int cpx) { jjg@46: ClassFile classFile = classWriter.getClassFile(); jjg@46: if (cpx == 0) { jjg@46: print("#0"); jjg@46: return; jjg@46: } jjg@46: jjg@46: CPInfo cpInfo; jjg@46: try { jjg@46: cpInfo = classFile.constant_pool.get(cpx); jjg@46: } catch (ConstantPoolException e) { jjg@46: print("#" + cpx); jjg@46: return; jjg@46: } jjg@46: jjg@46: int tag = cpInfo.getTag(); jjg@46: switch (tag) { jjg@46: case CONSTANT_Methodref: jjg@46: case CONSTANT_InterfaceMethodref: jjg@46: case CONSTANT_Fieldref: jjg@46: // simplify references within this class jjg@46: CPRefInfo ref = (CPRefInfo) cpInfo; jjg@46: try { jjg@46: if (ref.class_index == classFile.this_class) jjg@46: cpInfo = classFile.constant_pool.get(ref.name_and_type_index); jjg@46: } catch (ConstantPool.InvalidIndex e) { jjg@46: // ignore, for now jjg@46: } jjg@46: } jjg@46: print(tagName(tag) + " " + stringValue(cpInfo)); jjg@46: } jjg@46: jjg@46: String tagName(int tag) { jjg@46: switch (tag) { jjg@46: case CONSTANT_Utf8: jjg@46: return "Asciz"; jjg@46: case CONSTANT_Integer: jjg@46: return "int"; jjg@46: case CONSTANT_Float: jjg@46: return "float"; jjg@46: case CONSTANT_Long: jjg@46: return "long"; jjg@46: case CONSTANT_Double: jjg@46: return "double"; jjg@46: case CONSTANT_Class: jjg@46: return "class"; jjg@46: case CONSTANT_String: jjg@46: return "String"; jjg@46: case CONSTANT_Fieldref: jjg@46: return "Field"; jjg@46: case CONSTANT_Methodref: jjg@46: return "Method"; jjg@46: case CONSTANT_InterfaceMethodref: jjg@46: return "InterfaceMethod"; jjg@46: case CONSTANT_NameAndType: jjg@46: return "NameAndType"; jjg@46: default: jjg@46: return "unknown tag"; jjg@46: } jjg@46: } jjg@46: jjg@46: String stringValue(int constant_pool_index) { jjg@46: ClassFile classFile = classWriter.getClassFile(); jjg@46: try { jjg@46: return stringValue(classFile.constant_pool.get(constant_pool_index)); jjg@46: } catch (ConstantPool.InvalidIndex e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: jjg@46: String stringValue(CPInfo cpInfo) { jjg@46: return stringValueVisitor.visit(cpInfo); jjg@46: } jjg@46: jjg@46: StringValueVisitor stringValueVisitor = new StringValueVisitor(); jjg@46: jjg@46: private class StringValueVisitor implements ConstantPool.Visitor { jjg@46: public String visit(CPInfo info) { jjg@46: return info.accept(this, null); jjg@46: } jjg@46: jjg@46: public String visitClass(CONSTANT_Class_info info, Void p) { jjg@46: return getCheckedName(info); jjg@46: } jjg@46: jjg@46: String getCheckedName(CONSTANT_Class_info info) { jjg@46: try { jjg@46: return checkName(info.getName()); jjg@46: } catch (ConstantPoolException e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: jjg@46: public String visitDouble(CONSTANT_Double_info info, Void p) { jjg@46: return info.value + "d"; jjg@46: } jjg@46: jjg@46: public String visitFieldref(CONSTANT_Fieldref_info info, Void p) { jjg@46: return visitRef(info, p); jjg@46: } jjg@46: jjg@46: public String visitFloat(CONSTANT_Float_info info, Void p) { jjg@46: return info.value + "f"; jjg@46: } jjg@46: jjg@46: public String visitInteger(CONSTANT_Integer_info info, Void p) { jjg@46: return String.valueOf(info.value); jjg@46: } jjg@46: jjg@46: public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) { jjg@46: return visitRef(info, p); jjg@46: } jjg@46: jjg@46: public String visitLong(CONSTANT_Long_info info, Void p) { jjg@46: return info.value + "l"; jjg@46: } jjg@46: jjg@46: public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) { jjg@46: return getCheckedName(info) + ":" + getType(info); jjg@46: } jjg@46: jjg@46: String getCheckedName(CONSTANT_NameAndType_info info) { jjg@46: try { jjg@46: return checkName(info.getName()); jjg@46: } catch (ConstantPoolException e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: jjg@46: String getType(CONSTANT_NameAndType_info info) { jjg@46: try { jjg@46: return info.getType(); jjg@46: } catch (ConstantPoolException e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: jjg@46: public String visitMethodref(CONSTANT_Methodref_info info, Void p) { jjg@46: return visitRef(info, p); jjg@46: } jjg@46: jjg@46: public String visitString(CONSTANT_String_info info, Void p) { jjg@46: try { jjg@46: ClassFile classFile = classWriter.getClassFile(); jjg@46: int string_index = info.string_index; jjg@46: return stringValue(classFile.constant_pool.getUTF8Info(string_index)); jjg@46: } catch (ConstantPoolException e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: jjg@46: public String visitUtf8(CONSTANT_Utf8_info info, Void p) { jjg@46: String s = info.value; jjg@46: StringBuilder sb = new StringBuilder(); jjg@46: for (int i = 0; i < s.length(); i++) { jjg@46: char c = s.charAt(i); jjg@46: switch (c) { jjg@46: case '\t': jjg@46: sb.append('\\').append('t'); jjg@46: break; jjg@46: case '\n': jjg@46: sb.append('\\').append('n'); jjg@46: break; jjg@46: case '\r': jjg@46: sb.append('\\').append('r'); jjg@46: break; jjg@46: case '\"': jjg@46: sb.append('\\').append('\"'); jjg@46: break; jjg@46: default: jjg@46: sb.append(c); jjg@46: } jjg@46: } jjg@46: return sb.toString(); jjg@46: } jjg@46: jjg@46: String visitRef(CPRefInfo info, Void p) { jjg@46: String cn = getCheckedClassName(info); jjg@46: String nat; jjg@46: try { jjg@46: nat = stringValue(info.getNameAndTypeInfo()); jjg@46: } catch (ConstantPoolException e) { jjg@46: nat = report(e); jjg@46: } jjg@46: return cn + "." + nat; jjg@46: } jjg@46: jjg@46: String getCheckedClassName(CPRefInfo info) { jjg@46: try { jjg@46: return checkName(info.getClassName()); jjg@46: } catch (ConstantPoolException e) { jjg@46: return report(e); jjg@46: } jjg@46: } jjg@46: } jjg@46: jjg@46: jjg@46: /* If name is a valid binary name, return it; otherwise quote it. */ jjg@46: private static String checkName(String name) { jjg@46: if (name == null) jjg@46: return "null"; jjg@46: jjg@46: int len = name.length(); jjg@46: if (len == 0) jjg@46: return "\"\""; jjg@46: jjg@46: int cc = '/'; jjg@46: int cp; jjg@46: for (int k = 0; k < len; k += Character.charCount(cp)) { jjg@46: cp = name.codePointAt(k); jjg@46: if ((cc == '/' && !Character.isJavaIdentifierStart(cp)) jjg@46: || (cp != '/' && !Character.isJavaIdentifierPart(cp))) { jrose@267: return "\"" + addEscapes(name) + "\""; jjg@46: } jjg@46: cc = cp; jjg@46: } jjg@46: jjg@46: return name; jjg@46: } jjg@46: jrose@267: /* If name requires escapes, put them in, so it can be a string body. */ jrose@267: private static String addEscapes(String name) { jrose@267: String esc = "\\\"\n\t"; jrose@267: String rep = "\\\"nt"; jrose@267: StringBuilder buf = null; jrose@267: int nextk = 0; jrose@267: int len = name.length(); jrose@267: for (int k = 0; k < len; k++) { jrose@267: char cp = name.charAt(k); jrose@267: int n = esc.indexOf(cp); jrose@267: if (n >= 0) { jrose@267: if (buf == null) jrose@267: buf = new StringBuilder(len * 2); jrose@267: if (nextk < k) jrose@267: buf.append(name, nextk, k); jrose@267: buf.append('\\'); jrose@267: buf.append(rep.charAt(n)); jrose@267: nextk = k+1; jrose@267: } jrose@267: } jrose@267: if (buf == null) jrose@267: return name; jrose@267: if (nextk < len) jrose@267: buf.append(name, nextk, len); jrose@267: return buf.toString(); jrose@267: } jrose@267: jjg@46: private ClassWriter classWriter; jjg@46: private Options options; jjg@46: }