[3037] in java-interest

home help back first fref pref prev next nref lref last post

Re: javadoc suggestion

daemon@ATHENA.MIT.EDU (Anselm Baird_smith)
Fri Oct 27 15:14:28 1995

Date: Fri, 27 Oct 1995 15:58:17 +0100
From: Anselm Baird_smith <Anselm.Baird_Smith@inria.fr>
To: java-interest@java.sun.com
Reply-To: Anselm Baird-Smith <Anselm.BairdSmith@inria.fr>


Hi, 

Find enclosed a small program that implements my suggestion about
javadoc. Given a set of packages, it will dump the full class
hierarchy (ASCII text).

Sample usage:
-----
# java classtree.tree -p java.lang 
java.lang.Object
  java.lang.Throwable
    java.lang.Error
      java.lang.LinkageError
        java.lang.VerifyError
        java.lang.IncompatibleClassChangeError
          java.lang.AbstractMethodError
          java.lang.NoSuchMethodError
          java.lang.IllegalAccessError
          java.lang.InstantiationError
          java.lang.NoSuchFieldError
        java.lang.ClassCircularityError
...
-----

Enjoy,

Anselm.
BTW: the code is quiet ugly, I'll probably make a version spitting
HTML instead of ASCII, with links to the doc directory.

----- ~/myclasses/classtree/Tree.java
package classtree ;

import java.io.* ;
import java.util.* ;

public class Tree {
    public static Tree      root       = null ;
    public static Hashtable registered = new Hashtable() ;

    static {
	root = makeTree ("java.lang.Object") ;
	root.installed = true ;
    }

    Class   cls        = null ;
    Vector  subclasses = null ;
    boolean installed  = false ;	// shouldn't be need, poorly designed

    /**
     * Emit an error and exit.
     * @param msg The error message.
     */

    public static void error (String msg) {
	System.out.println (msg) ;
	System.exit (1) ;
    }

    /**
     * Emit a warning.
     * @param msg The warning to emit.
     */

    public static void warning (String msg) {
	System.out.println (msg) ;
    }

    /**
     * Create a new Tree entry.
     * @param cls The subclass to install
     */
    
    private static Tree makeTree (Class cls) {
	Tree entry = (Tree) registered.get (cls.getName()) ;
	if ( entry == null ) {
	    entry = new Tree () ;
	    entry.cls        = cls ;
	    entry.subclasses = null ;
	    registered.put (cls.getName(), entry) ;
	}
	return entry ;
    }

    private static Tree makeTree (String clsname) {
	Class cls = Class.forName (clsname) ;
	return makeTree (cls) ;
    }

    /**
     * Insert a class in the class structure.
     * @param cls The class to insert in the hierarchy.
     * @return The Tree entry of this class.
     */
     
    public static Tree insertClass (Class cls) {
	Tree entry = makeTree (cls) ;
	if ( entry.installed )
	    return entry ;
	// Install the class and all its superclass:
	Class superclass = cls.getSuperclass() ;
	Tree  superentry = insertClass (superclass) ;
	if ( superentry.subclasses == null )
	    superentry.subclasses = new Vector(5) ;
	superentry.subclasses.addElement (entry) ;
	entry.installed = true ;
	return entry ;
    }

    public static void dumpAll (String prefix, Tree node) {
	System.out.println (prefix+node.cls.getName()) ;
	if ( node.subclasses != null) {
	    for (int i = 0 ; i < node.subclasses.size() ; i++)
		dumpAll (prefix+"  ", (Tree) node.subclasses.elementAt(i)) ;
	}
    }

    public static void doPackage (File pkgdir, String pkg) {
	String files[] = pkgdir.list() ;
	for (int i = 0 ; i < files.length ; i++) {
	    String filename = files[i] ;
	    if ( ! filename.endsWith (".class") )
		continue ;
	    // okay, this looks like a proper class, add it in hierarchy image
	    String clsname = pkg+"."+filename.substring(0, filename.length()-6);
	    Class  cls = null ;
	    try {
		cls = Class.forName(clsname) ;
		insertClass (cls) ;
	    } catch (Exception e) {
		warning ("Couldn't find class " + clsname + ", skipping.") ;
	    }
	}
    }

    public static void doPackage (File pathes[], String pkg) {
	String pkgpath[] = parsePackage (pkg) ;
	// First, try to locate the package:
	for (int i = 0 ; i < pathes.length ; i++) {
	    File pkgdir = pathes[i] ;
	    for (int j = 0 ; j < pkgpath.length ; j++) 
		pkgdir = new File (pkgdir, pkgpath[j]) ;
	    if ( pkgdir.exists() ) {
		doPackage (pkgdir, pkg) ;
		return ;
	    }
	}
    }

    public static String[] parsePackage (String pkg) {
	Vector pkgs = new Vector() ;
	int isep  = 0 ;
	int len   = pkg.length() ;
	int start = 0 ;
	while ((isep = pkg.indexOf('.', start)) >= 0) {
	    if ( isep == start ) {
		start = isep+1 ;
		continue ;
	    }
	    pkgs.addElement (pkg.substring(start, isep)) ;
	    start = isep+1 ;
	}
	if ( start < len ) 
	    pkgs.addElement(pkg.substring (start)) ;
	String packages[] = new String[pkgs.size()] ;
	pkgs.copyInto (packages) ;
	return packages ;
    }

    public static File[] parseClasspath () {
	String clspath = (String) System.getProperty ("java.class.path") ;
	if ( clspath == null )
	    error ("[properties] Couldn't find class path.") ;
	String pathsep = (String) System.getProperty ("path.separator") ;
	if ( pathsep == null )
	    error ("[properties] Couldn't find path separator.") ;
	Vector pathes = new Vector() ;
	int    isep   = 0 ;
	int    seplen = pathsep.length() ;
	int    start  = 0 ;
	int    len    = clspath.length() ;
	while ((isep = clspath.indexOf (pathsep, start)) >= 0 ) {
	    // Skip empty items:
	    if ( isep == start ) {
		start = isep + 1 ;
		continue ;
	    } 
	    // Get current item:
	    pathes.addElement (new File(clspath.substring (start, isep))) ;
	    start = (isep+seplen) ;
	}
	if ( start < len )
	    pathes.addElement (new File (clspath.substring (start))) ;
	File p[] = new File[pathes.size()] ;
	pathes.copyInto (p) ;
	return p ;
    }
	
    public static void main (String args[]) {
	Vector packages = new Vector() ;
	for (int i = 0 ; i < args.length ; i++) {
	    if (args[i].equals("-p") && (i+1 < args.length)) {
		packages.addElement (args[++i]) ;
	    } else {
		System.out.println ("classtree.tree -p <package>") ;
	    }
	}
	File pathes[] = parseClasspath () ;
	for (int i = 0 ; i < packages.size() ; i++)
	    doPackage (pathes, (String) packages.elementAt(i)) ;
	Tree.dumpAll("", root) ;
    }

}


-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post