169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.tools;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.*;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.ClassFile;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.ClassFilePrinter;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Dump is a tool for viewing the class definition in the given
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * class file.  Unlike the JDK javap tool, Dump works even if
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the class file is broken.
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>For example,
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>% java javassist.tools.Dump foo.class</pre></ul>
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>prints the contents of the constant pool and the list of methods
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * and fields.
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Dump {
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private Dump() {}
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Main method.
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param args           <code>args[0]</code> is the class file name.
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static void main(String[] args) throws Exception {
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (args.length != 1) {
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            System.err.println("Usage: java Dump <class file name>");
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return;
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        DataInputStream in = new DataInputStream(
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                         new FileInputStream(args[0]));
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ClassFile w = new ClassFile(in);
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        PrintWriter out = new PrintWriter(System.out, true);
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.println("*** constant pool ***");
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        w.getConstPool().print(out);
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.println();
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        out.println("*** members ***");
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ClassFilePrinter.print(w, out);
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
58