001    /*
002     * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javap;
027    
028    import com.sun.tools.classfile.ClassFile;
029    import com.sun.tools.classfile.ConstantPool;
030    import com.sun.tools.classfile.ConstantPoolException;
031    
032    import static com.sun.tools.classfile.ConstantPool.*;
033    
034    /*
035     *  Write a constant pool entry.
036     *
037     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
038     *  you write code that depends on this, you do so at your own risk.
039     *  This code and its internal interfaces are subject to change or
040     *  deletion without notice.</b>
041     */
042    public class ConstantWriter extends BasicWriter {
043        static ConstantWriter instance(Context context) {
044            ConstantWriter instance = context.get(ConstantWriter.class);
045            if (instance == null)
046                instance = new ConstantWriter(context);
047            return instance;
048        }
049    
050        protected ConstantWriter(Context context) {
051            super(context);
052            context.put(ConstantWriter.class, this);
053            classWriter = ClassWriter.instance(context);
054            options = Options.instance(context);
055        }
056    
057        void writeConstantPool() {
058            ConstantPool.Visitor<Integer, Void> v = new ConstantPool.Visitor<Integer,Void>() {
059                public Integer visitClass(CONSTANT_Class_info info, Void p) {
060                    println("#" + info.name_index + ";\t//  " + stringValue(info));
061                    return 1;
062                }
063    
064                public Integer visitDouble(CONSTANT_Double_info info, Void p) {
065                    println(stringValue(info) + ";");
066                    return 2;
067                }
068    
069                public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) {
070                    println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
071                    return 1;
072                }
073    
074                public Integer visitFloat(CONSTANT_Float_info info, Void p) {
075                    println(stringValue(info) + ";");
076                    return 1;
077                }
078    
079                public Integer visitInteger(CONSTANT_Integer_info info, Void p) {
080                    println(stringValue(info) + ";");
081                    return 1;
082                }
083    
084                public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
085                    println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
086                    return 1;
087                }
088    
089                public Integer visitLong(CONSTANT_Long_info info, Void p) {
090                    println(stringValue(info) + ";");
091                    return 2;
092                }
093    
094                public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
095                    String tab = (options.compat ? "" : "\t"); // BUG 6622232 javap gets whitespace confused
096                    println("#" + info.name_index + ":#" + info.type_index + ";" + tab + "//  " + stringValue(info));
097                    return 1;
098                }
099    
100                public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) {
101                    println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
102                    return 1;
103                }
104    
105                public Integer visitString(CONSTANT_String_info info, Void p) {
106                    println("#" + info.string_index + ";\t//  " + stringValue(info));
107                    return 1;
108                }
109    
110                public Integer visitUtf8(CONSTANT_Utf8_info info, Void p) {
111                    println(stringValue(info) + ";");
112                    return 1;
113                }
114    
115            };
116            println("  Constant pool:");
117            ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
118            int cpx = 1;
119            while (cpx < constant_pool.size()) {
120                try {
121                    CPInfo cpInfo = constant_pool.get(cpx);
122                    print("const #" + cpx + " = " + tagName(cpInfo.getTag()) + "\t");
123                    cpx += cpInfo.accept(v, null);
124                } catch (ConstantPool.InvalidIndex ex) {
125                    print("const #" + cpx); // should not happen
126                }
127            }
128        }
129    
130        void write(int cpx) {
131            ClassFile classFile = classWriter.getClassFile();
132            if (cpx == 0) {
133                print("#0");
134                return;
135            }
136    
137            CPInfo cpInfo;
138            try {
139                cpInfo = classFile.constant_pool.get(cpx);
140            } catch (ConstantPoolException e) {
141                print("#" + cpx);
142                return;
143            }
144    
145            int tag = cpInfo.getTag();
146            switch (tag) {
147                case CONSTANT_Methodref:
148                case CONSTANT_InterfaceMethodref:
149                case CONSTANT_Fieldref:
150                    // simplify references within this class
151                    CPRefInfo ref = (CPRefInfo) cpInfo;
152                    try {
153                        if (ref.class_index == classFile.this_class)
154                             cpInfo = classFile.constant_pool.get(ref.name_and_type_index);
155                    } catch (ConstantPool.InvalidIndex e) {
156                        // ignore, for now
157                    }
158            }
159            print(tagName(tag) + " " + stringValue(cpInfo));
160        }
161    
162        String tagName(int tag) {
163            switch (tag) {
164                case CONSTANT_Utf8:
165                    return "Asciz";
166                case CONSTANT_Integer:
167                    return "int";
168                case CONSTANT_Float:
169                    return "float";
170                case CONSTANT_Long:
171                    return "long";
172                case CONSTANT_Double:
173                    return "double";
174                case CONSTANT_Class:
175                    return "class";
176                case CONSTANT_String:
177                    return "String";
178                case CONSTANT_Fieldref:
179                    return "Field";
180                case CONSTANT_Methodref:
181                    return "Method";
182                case CONSTANT_InterfaceMethodref:
183                    return "InterfaceMethod";
184                case CONSTANT_NameAndType:
185                    return "NameAndType";
186                default:
187                    return "unknown tag";
188            }
189        }
190    
191        String stringValue(int constant_pool_index) {
192            ClassFile classFile = classWriter.getClassFile();
193            try {
194                return stringValue(classFile.constant_pool.get(constant_pool_index));
195            } catch (ConstantPool.InvalidIndex e) {
196                return report(e);
197            }
198        }
199    
200        String stringValue(CPInfo cpInfo) {
201            return stringValueVisitor.visit(cpInfo);
202        }
203    
204        StringValueVisitor stringValueVisitor = new StringValueVisitor();
205    
206        private class StringValueVisitor implements ConstantPool.Visitor<String, Void> {
207            public String visit(CPInfo info) {
208                return info.accept(this, null);
209            }
210    
211            public String visitClass(CONSTANT_Class_info info, Void p) {
212                return getCheckedName(info);
213            }
214    
215            String getCheckedName(CONSTANT_Class_info info) {
216                try {
217                    return checkName(info.getName());
218                } catch (ConstantPoolException e) {
219                    return report(e);
220                }
221            }
222    
223            public String visitDouble(CONSTANT_Double_info info, Void p) {
224                return info.value + "d";
225            }
226    
227            public String visitFieldref(CONSTANT_Fieldref_info info, Void p) {
228                return visitRef(info, p);
229            }
230    
231            public String visitFloat(CONSTANT_Float_info info, Void p) {
232                return info.value + "f";
233            }
234    
235            public String visitInteger(CONSTANT_Integer_info info, Void p) {
236                return String.valueOf(info.value);
237            }
238    
239            public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
240                return visitRef(info, p);
241            }
242    
243            public String visitLong(CONSTANT_Long_info info, Void p) {
244                return info.value + "l";
245            }
246    
247            public String visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
248                return getCheckedName(info) + ":" + getType(info);
249            }
250    
251            String getCheckedName(CONSTANT_NameAndType_info info) {
252                try {
253                    return checkName(info.getName());
254                } catch (ConstantPoolException e) {
255                    return report(e);
256                }
257            }
258    
259            String getType(CONSTANT_NameAndType_info info) {
260                try {
261                    return info.getType();
262                } catch (ConstantPoolException e) {
263                    return report(e);
264                }
265            }
266    
267            public String visitMethodref(CONSTANT_Methodref_info info, Void p) {
268                return visitRef(info, p);
269            }
270    
271            public String visitString(CONSTANT_String_info info, Void p) {
272                try {
273                    ClassFile classFile = classWriter.getClassFile();
274                    int string_index = info.string_index;
275                    return stringValue(classFile.constant_pool.getUTF8Info(string_index));
276                } catch (ConstantPoolException e) {
277                    return report(e);
278                }
279            }
280    
281            public String visitUtf8(CONSTANT_Utf8_info info, Void p) {
282                String s = info.value;
283                StringBuilder sb = new StringBuilder();
284                for (int i = 0; i < s.length(); i++) {
285                    char c = s.charAt(i);
286                    switch (c) {
287                        case '\t':
288                            sb.append('\\').append('t');
289                            break;
290                        case '\n':
291                            sb.append('\\').append('n');
292                            break;
293                        case '\r':
294                            sb.append('\\').append('r');
295                            break;
296                        case '\"':
297                            sb.append('\\').append('\"');
298                            break;
299                        default:
300                            sb.append(c);
301                    }
302                }
303                return sb.toString();
304            }
305    
306            String visitRef(CPRefInfo info, Void p) {
307                String cn = getCheckedClassName(info);
308                String nat;
309                try {
310                    nat = stringValue(info.getNameAndTypeInfo());
311                } catch (ConstantPoolException e) {
312                    nat = report(e);
313                }
314                return cn + "." + nat;
315            }
316    
317            String getCheckedClassName(CPRefInfo info) {
318                try {
319                    return checkName(info.getClassName());
320                } catch (ConstantPoolException e) {
321                    return report(e);
322                }
323            }
324        }
325    
326    
327        /* If name is a valid binary name, return it; otherwise quote it. */
328        private static String checkName(String name) {
329            if (name == null)
330                return "null";
331    
332            int len = name.length();
333            if (len == 0)
334                return "\"\"";
335    
336            int cc = '/';
337            int cp;
338            for (int k = 0; k < len; k += Character.charCount(cp)) {
339                cp = name.codePointAt(k);
340                if ((cc == '/' && !Character.isJavaIdentifierStart(cp))
341                        || (cp != '/' && !Character.isJavaIdentifierPart(cp))) {
342                    return "\"" + name + "\"";
343                }
344                cc = cp;
345            }
346    
347            return name;
348        }
349    
350        private ClassWriter classWriter;
351        private Options options;
352    }