001: package isql;
002:
003: import java.util.*;
004: import java.io.*;
005: import java.util.zip.*;
006: import util.*;
007:
008: /** given a package like 'java.lan.reflect.*' will return
009: * the classes within it.
010: * Passing a dot as parameter needs to be looked into.
011: */
012: public class listpackage {
013:
014: public static boolean verbose = false;
015:
016: public static void main(String args[]) {
017: System.err.println("package:" + args[0]);
018: listpackage lp = new listpackage(args[0]);
019: System.out.println("\n\n++++++++++\n " + lp.getClasses());
020:
021: }
022:
023: ArrayList al = new ArrayList();
024:
025: public listpackage(String pack) {
026: // 1. read classpath and split
027: // 2. recurse through directories and jar/zips or classes
028: // 3. if dir, list all contents (class files)
029: // 4. if jar/zip, list contents (should come as a class file)
030: // 5. if you find a path matching given one then notify
031: String classpath = System.getProperty("java.class.path", ".");
032: String newpack = new String(pack);
033: if (pack.endsWith(".*"))
034: newpack = newpack.substring(0, pack.length() - 2);
035: if (!newpack.equals(".")) // existing directory is in classpath
036: newpack = newpack.replace('.', '/');
037: if (verbose)
038: System.out.println("CP:" + classpath);
039: if (verbose)
040: System.out.println("JH:"
041: + System.getProperty("JAVA_HOME", "."));
042: String rtpath = System.getProperty("JAVA_HOME", ".");
043: rtpath += "/jre/lib/rt.jar:";
044: //if (verbose)
045: System.out.println(" newpack:" + newpack);
046: classpath = rtpath + classpath;
047: String paths[] = ArrayUtil.split(classpath, ':');
048: //System.out.println( "Paths:" + paths);
049: // recurse through classpath searching for the packages
050: // imported
051: for (int i = 0; i < paths.length; i++) {
052: //if (verbose)
053: System.out.println("****** Path:" + paths[i]
054: + "***********");
055: java.io.File f = new java.io.File(paths[i]);
056: if (f.isDirectory()) {
057: if (newpack.equals(".")) {
058: if (paths[i].equals(newpack)) {
059: System.out.println("filling for current dir");
060: String[] flist = f.list(new FilenameFilter() {
061: public boolean accept(File dir, String name) {
062: if (name.endsWith(".class"))
063: return true;
064: return false;
065: }
066: });
067: for (int j = 0; j < flist.length; j++) {
068: //System.out.println("XX "+ flist[j] + " in " + paths[i]);
069: String tmp = classbasename(flist[j]);
070: if (tmp != null)
071: al.add(tmp);
072: }
073: }
074: } else {
075: File f1 = new File(paths[i] + '/' + newpack);
076: if (f1.exists()) {
077: //if (verbose)
078: System.out.println("FND " + newpack + " in "
079: + paths[i]);
080: String tmp = classbasename(newpack);
081: if (tmp != null)
082: al.add(tmp);
083: }
084: }
085:
086: } else // is not directory
087: if (f.isFile()) {
088: if (paths[i].endsWith(".jar")
089: || paths[i].endsWith(".zip")) {
090: if (verbose)
091: System.out.println("+++" + paths[i]);
092: try {
093: java.util.zip.ZipFile z = new java.util.zip.ZipFile(
094: paths[i]);
095: Enumeration e = z.entries();
096: while (e.hasMoreElements()) {
097: String name = ((java.util.zip.ZipEntry) e
098: .nextElement()).getName();
099: if (verbose)
100: System.out.println(" " + name);
101: if (newpack.equals(dirname(name))) {
102: if (verbose)
103: System.out
104: .println(" FOUND n zip "
105: + name + " in "
106: + paths[i]);
107: String tmp = classbasename(name);
108: if (tmp != null)
109: al.add(tmp);
110: }
111: }
112: } catch (Exception exc) {
113: System.err.println("ZIP:" + exc.toString());
114: }
115: } // jar or zip
116: else if (paths[i].endsWith(".class")) {
117: if (newpack.equals(paths[i])) {
118: if (verbose)
119: System.out.println(" FOUND:" + paths[i]);
120: String tmp = classbasename(newpack);
121: if (tmp != null)
122: al.add(tmp);
123: }
124:
125: }
126: } // if file
127: } // for i
128: Collections.sort(al);
129: } // listpackage
130:
131: public static String dirname(String filename) {
132: int pos = filename.lastIndexOf('/');
133: if (pos == -1)
134: return filename;
135: return filename.substring(0, pos);
136: }
137:
138: public static String classbasename(String filename) {
139: if (filename.indexOf('$') != -1)
140: return null;
141: int pos = filename.lastIndexOf(".class");
142: if (pos != -1)
143: filename = filename.substring(0, pos);
144: pos = filename.lastIndexOf('/');
145: if (pos == -1)
146: return filename;
147: if (pos == filename.length() - 1)
148: return null;
149: return filename.substring(pos + 1);
150: }
151:
152: public ArrayList getClasses() {
153: return al;
154: }
155:
156: } // class
|