KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ScanJar


1 /*
2  * ScanJar.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.3 $
24  */

25
26 import org.netbeans.modules.classfile.*;
27 import java.io.*;
28 import java.util.*;
29 import java.util.zip.ZipFile JavaDoc;
30 import java.util.zip.ZipEntry JavaDoc;
31
32 /**
33  * ScanJar: load all of the classes of a specified jar file,
34  * useful for performance and regression testing.
35  *
36  * @author Thomas Ball
37  */

38 public class ScanJar {
39     String JavaDoc jarName;
40     boolean includeCode;
41     boolean toString;
42
43     public static void main(String JavaDoc[] args) {
44     boolean includeCode = false;
45     boolean toString = false;
46         if (args.length == 0)
47             usage();
48         for (int i = 0; i < args.length; i++) {
49         if (args[i].equals("-includeCode"))
50         includeCode = true;
51         else if (args[i].equals("-toString"))
52         toString = true;
53             else if (args[i].charAt(0) == '-')
54                 usage();
55             else {
56                 try {
57                     ScanJar sj = new ScanJar(args[i], includeCode, toString);
58             System.out.print("scanning " + args[i]);
59             if (includeCode || toString) {
60             System.out.print(": ");
61             if (includeCode)
62                 System.out.print("includeCode ");
63             if (toString)
64                 System.out.print("toString");
65             }
66             System.out.println();
67             ElapsedTimer timer = new ElapsedTimer();
68             int n = sj.scan();
69             System.out.println("scanned " + n + " files in " +
70                        timer.toString());
71                 } catch (IOException e) {
72                     System.err.println("error accessing \"" + args[i] +
73                                        "\": " + e.toString());
74                 }
75             }
76         }
77     }
78
79     ScanJar(String JavaDoc name, boolean incl, boolean tos) {
80     jarName = name;
81     includeCode = incl;
82     toString = tos;
83     }
84
85     /**
86      * Reads class entries from the jar file into ClassFile instances.
87      * Returns the number of classes scanned.
88      */

89     public int scan() throws IOException {
90     int n = 0;
91     ZipFile JavaDoc zf = new ZipFile JavaDoc(jarName);
92     Enumeration files = zf.entries();
93     while (files.hasMoreElements()) {
94         ZipEntry JavaDoc entry = (ZipEntry JavaDoc)files.nextElement();
95         String JavaDoc name = entry.getName();
96         if (name.endsWith(".class")) {
97                 InputStream in = zf.getInputStream(entry);
98                 ClassFile cf = new ClassFile(in, includeCode);
99                 if (toString)
100                     try {
101                         cf.toString(); // forces loading of attributes.
102
} catch (InvalidClassFileAttributeException ex) {
103                         System.out.println("error accessing " + name);
104                         throw ex;
105                     }
106                 in.close();
107         n++;
108         }
109     }
110     zf.close();
111     return n;
112     }
113
114     public static void usage() {
115         System.err.println("usage: java ScanJar [-includeCode] " +
116                "[-toString] <jar file> [ <jar file> ...]");
117         System.exit(1);
118     }
119 }
120
121
Popular Tags