001: /*
002: * PrintClass.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:
052: /**
053: * PrintClass: write a class as a println statement.
054: *
055: * @author Thomas Ball
056: */
057: public class PrintClass {
058: String this Class;
059:
060: PrintClass(String spec) {
061: this Class = spec;
062: }
063:
064: void print(PrintStream out) throws IOException {
065: ClassName cn = ClassName.getClassName(this Class.replace('.',
066: '/'));
067: InputStream is = findClassStream(cn.getType());
068: if (is == null) {
069: System.err.println("couldn't find class: "
070: + cn.getExternalName());
071: return;
072: }
073: ClassFile cfile = new ClassFile(is);
074: out.println(cfile);
075: }
076:
077: private InputStream findClassStream(String className) {
078: InputStream is = ClassLoader.getSystemClassLoader()
079: .getResourceAsStream(className + ".class");
080: return is;
081: }
082:
083: /**
084: * An error routine which displays the command line usage
085: * before exiting.
086: */
087: public static void usage() {
088: System.err
089: .println("usage: java PrintClass <class> [ <class> ...]");
090: System.exit(1);
091: }
092:
093: public static void main(String[] args) {
094: if (args.length == 0)
095: usage();
096:
097: for (int i = 0; i < args.length; i++) {
098: if (args[i].charAt(0) == '-')
099: usage();
100: else {
101: try {
102: PrintClass pc = new PrintClass(args[i]);
103: pc.print(System.out);
104: } catch (IOException e) {
105: System.err.println("error accessing \"" + args[i]
106: + "\": " + e.toString());
107: }
108: }
109: }
110: }
111: }
|