001: /*
002: * ScanJar.java
003: *
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
005: *
006: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common
010: * Development and Distribution License("CDDL") (collectively, the
011: * "License"). You may not use this file except in compliance with the
012: * License. You can obtain a copy of the License at
013: * http://www.netbeans.org/cddl-gplv2.html
014: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
015: * specific language governing permissions and limitations under the
016: * License. When distributing the software, include this License Header
017: * Notice in each file and include the License file at
018: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
019: * particular file as subject to the "Classpath" exception as provided
020: * by Sun in the GPL Version 2 section of the License file that
021: * accompanied this code. If applicable, add the following below the
022: * License Header, with the fields enclosed by brackets [] replaced by
023: * your own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Contributor(s):
027: *
028: * The Original Software is NetBeans. The Initial Developer of the Original
029: * Software is Sun Microsystems, Inc. Portions Copyright 2000-2001 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: *
032: * If you wish your version of this file to be governed by only the CDDL
033: * or only the GPL Version 2, indicate your decision by adding
034: * "[Contributor] elects to include this software in this distribution
035: * under the [CDDL or GPL Version 2] license." If you do not indicate a
036: * single choice of license, a recipient has the option to distribute
037: * your version of this file under either the CDDL, the GPL Version 2 or
038: * to extend the choice of license to its licensees as provided above.
039: * However, if you add GPL Version 2 code and therefore, elected the GPL
040: * Version 2 license, then the option applies only if the new code is
041: * made subject to such option by the copyright holder.
042: *
043: * Contributor(s): Thomas Ball
044: *
045: * Version: $Revision$
046: */
047:
048: import org.netbeans.modules.classfile.*;
049: import java.io.*;
050: import java.util.*;
051: import java.util.zip.ZipFile;
052: import java.util.zip.ZipEntry;
053:
054: /**
055: * ScanJar: load all of the classes of a specified jar file,
056: * useful for performance and regression testing.
057: *
058: * @author Thomas Ball
059: */
060: public class ScanJar {
061: String jarName;
062: boolean includeCode;
063: boolean toString;
064:
065: public static void main(String[] args) {
066: boolean includeCode = false;
067: boolean toString = false;
068: if (args.length == 0)
069: usage();
070: for (int i = 0; i < args.length; i++) {
071: if (args[i].equals("-includeCode"))
072: includeCode = true;
073: else if (args[i].equals("-toString"))
074: toString = true;
075: else if (args[i].charAt(0) == '-')
076: usage();
077: else {
078: try {
079: ScanJar sj = new ScanJar(args[i], includeCode,
080: toString);
081: System.out.print("scanning " + args[i]);
082: if (includeCode || toString) {
083: System.out.print(": ");
084: if (includeCode)
085: System.out.print("includeCode ");
086: if (toString)
087: System.out.print("toString");
088: }
089: System.out.println();
090: ElapsedTimer timer = new ElapsedTimer();
091: int n = sj.scan();
092: System.out.println("scanned " + n + " files in "
093: + timer.toString());
094: } catch (IOException e) {
095: System.err.println("error accessing \"" + args[i]
096: + "\": " + e.toString());
097: }
098: }
099: }
100: }
101:
102: ScanJar(String name, boolean incl, boolean tos) {
103: jarName = name;
104: includeCode = incl;
105: toString = tos;
106: }
107:
108: /**
109: * Reads class entries from the jar file into ClassFile instances.
110: * Returns the number of classes scanned.
111: */
112: public int scan() throws IOException {
113: int n = 0;
114: ZipFile zf = new ZipFile(jarName);
115: Enumeration files = zf.entries();
116: while (files.hasMoreElements()) {
117: ZipEntry entry = (ZipEntry) files.nextElement();
118: String name = entry.getName();
119: if (name.endsWith(".class")) {
120: InputStream in = zf.getInputStream(entry);
121: ClassFile cf = new ClassFile(in, includeCode);
122: if (toString)
123: try {
124: cf.toString(); // forces loading of attributes.
125: } catch (InvalidClassFileAttributeException ex) {
126: System.out.println("error accessing " + name);
127: throw ex;
128: }
129: in.close();
130: n++;
131: }
132: }
133: zf.close();
134: return n;
135: }
136:
137: public static void usage() {
138: System.err.println("usage: java ScanJar [-includeCode] "
139: + "[-toString] <jar file> [ <jar file> ...]");
140: System.exit(1);
141: }
142: }
|