KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrintClassFile


1 /*
2  * PrintClassFile.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 2000-2001 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.2 $
24  */

25
26 import org.netbeans.modules.classfile.*;
27 import java.io.*;
28 import java.util.*;
29
30 /**
31  * PrintClassFile: write a class as a println statement.
32  *
33  * @author Thomas Ball
34  */

35 public class PrintClassFile {
36     String JavaDoc thisClass;
37
38     PrintClassFile(String JavaDoc spec) {
39         thisClass = spec;
40     }
41
42     void print(PrintStream out) throws IOException {
43     InputStream is = new FileInputStream(thisClass);
44     ClassFile cfile = new ClassFile(is);
45         out.println(cfile);
46     }
47
48     /**
49      * An error routine which displays the command line usage
50      * before exiting.
51      */

52     public static void usage() {
53         System.err.println(
54             "usage: java PrintClassFile <file> [ <file> ...]");
55         System.exit(1);
56     }
57
58     public static void main(String JavaDoc[] args) {
59         if (args.length == 0)
60             usage();
61
62         for (int i = 0; i < args.length; i++) {
63             if (args[i].charAt(0) == '-')
64                 usage();
65             else {
66                 try {
67                     PrintClassFile pc = new PrintClassFile(args[i]);
68                     pc.print(System.out);
69                 } catch (IOException e) {
70                     System.err.println("error accessing \"" + args[i] +
71                                        "\": " + e.toString());
72                 }
73             }
74         }
75     }
76 }
77
Popular Tags