Paul Kinnucan wrote:
> Actually, this should not be too difficult to implement almost entirely in
> Java. You can get the classpath from System.properties. For any zip or jar
> entries on the classpath, you can get easily get all the entries, using the
> JDK's JarFile class. Thus, the algorithm would be something like:

There doesn't seem to be a JarFile class in JDK 1.1, but ZipFile works fine.

> If anyone is interested in creating such an ImportWizard class, I'd be glad

I've attached the source code to do this. A couple of points I wasn't sure
about:

1) I've assumed the classpath separator is ':' -- I'm not sure whether this
should be different for Windows (which uses ';', doesn't it?), or other
platforms.

2) It currently prints import statements for all matching classes -- you can
change that by inserting a break in the appropriate place.

3) If you want to find the standard classes, you'll need the appropriate
classes.zip or rt.jar in the classpath. I wasn't sure how to get at those.

You can test if from the command line:

java ImportWizard [multiple class names]

The first class name will be quite slow while the class name database gets
built.

Cheers,
        Len.

---
 Len Trigg =========== http://www.cs.waikato.ac.nz/~trigg/
 Computer Science Dept, University of Waikato, New Zealand
 PGP Public key http://www.cs.waikato.ac.nz/~trigg/key.asc
/*
 *    ImportWizard.java
 *    Copyright (C) 1999 Len Trigg ([EMAIL PROTECTED])
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

// package jde.wizards;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;

/**
 * Converts an unqualified class name to import statements by scanning
 * through the classpath.
 *
 * @author Len Trigg ([EMAIL PROTECTED])
 * @version 1.0 - 6 May 1999
 */
public class ImportWizard {


  
  /** Stores the list of all classes in the classpath */
  public static Vector CLASS_LIST = new Vector(500);

  
  
  /** Build the list of classes */
  static {

    //    System.err.println("Making class list");

    String classPath = System.getProperty("java.class.path");
    String classPathSeparator = ":"; //  Should this be ";" for Windoze
    StringTokenizer st = new StringTokenizer(classPath, classPathSeparator);
    while (st.hasMoreTokens()) {
      String classPathEntry = st.nextToken();
      File classPathFile = new File(classPathEntry);
      if (classPathFile.exists()) {
        if (classPathEntry.toLowerCase().endsWith(".jar")) {
          addClassesFromZip(CLASS_LIST, classPathFile);
        } else if (classPathEntry.toLowerCase().endsWith(".zip")) {
          addClassesFromZip(CLASS_LIST, classPathFile);
        } else if (classPathFile.isDirectory()) {
          addClassesFromDir(CLASS_LIST, classPathFile, classPathFile);
        }
      }
    }

    //    System.err.println("Done (" + CLASS_LIST.size() + " classes)");

  }

  
  
  /**
   * Adds the classes from the supplied Zip file to the class list.
   *
   * @param classList the Vector to add the classes to
   * @param classPathFile the File to scan as a zip file
   */
  public static void addClassesFromZip(Vector classList,
                                       File classPathFile) {

    try {
      ZipFile zipFile = new ZipFile(classPathFile);
      Enumeration enum = zipFile.entries();
      while (enum.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry)enum.nextElement();
        String current = zipEntry.getName();
        if (current.toLowerCase().endsWith(".class")) {
          current = current.substring(0, current.length() - 6);
          classList.addElement(current.replace(File.separatorChar, '.'));
        }
      }
    } catch (Exception ex) {
      System.err.println("Problem opening " + classPathFile + " with zip.");
    }
  }

  
  /**
   * Adds the classes from the supplied directory to the class list.
   *
   * @param classList the Vector to add the classes to
   * @param classPathFile the File to recursively scan as a directory
   */
  public static void addClassesFromDir(Vector classList,
                                       File rootDir,
                                       File currentDir) {
    
    String [] files = currentDir.list();
    for (int i = 0; i < files.length; i++) {
      String current = files[i];
      if (current.toLowerCase().endsWith(".class")) {
        current = current.substring(0, current.length() - 6);
        String rootPath = rootDir.getPath();
        String currentPath = currentDir.getPath();
        if (currentPath.indexOf(rootPath) != 0) {
          System.err.println("currentPath doesn't start with rootPath!\n"
                             + "rootPath: " + rootPath + "\n"
                             + "currentPath: " + currentPath + "\n");
        } else {
          String packageName = currentPath.substring(rootPath.length());
          if (packageName.length() > 0) {
            // Not the current directory
            packageName = packageName.replace(File.separatorChar, '.');
            classList.addElement(packageName.substring(1) + '.' + current);
          } else {
            // The current directory
            classList.addElement(current);
          }
        }
      } else {
        // Check if it's a directory to recurse into
        File currentFile = new File(currentDir, current);
        if (currentFile.isDirectory()) {
          addClassesFromDir(classList, rootDir, currentFile);
        }
      }
    }
  }

  
  /**
   * Looks up an unqualified class name in the class path to find possible
   * fully qualified matches.
   *
   * @param className a value of type 'String'
   */
  public static void makeImportStatement(String className) {

    for (int i = 0; i < CLASS_LIST.size(); i++) {
      if (((String)CLASS_LIST.elementAt(i)).endsWith(className)) {
        System.out.println("\"import " + CLASS_LIST.elementAt(i) + ";\"");
        // Add a break here if you really only want one returned
      }
    }
    System.out.flush();
  }


  /**
   * Tests the ImportWizard from the command line
   *
   * @param args an array of strings containing class names to look up
   */
  public static void main(String[] args) {

    if (args.length == 0) {
      System.out.println("Give class names as arguments to look up");
    } else {
      for (int i = 0; i < args.length; i++) {
        System.out.println("=== " + args[i] + " ===");
        makeImportStatement(args[i]);
      }
    }
  }  
} // ImportWizard

Reply via email to