View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Genjava-Core nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.util;
33  
34  import java.io.File;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  
39  import com.generationjava.collections.FQMap;
40  
41  import com.generationjava.io.find.Finder;
42  import com.generationjava.io.find.FileFinder;
43  import com.generationjava.io.find.ZipFinder;
44  import com.generationjava.io.find.FindListener;
45  import com.generationjava.io.find.FindEvent;
46  
47  /***
48   * Indexes a class path.
49   */
50  public class ClassIndex implements FindListener {
51  
52      private FQMap map = null;
53  
54      /***
55       * Create an index of the runtime classpath.
56       */
57      public ClassIndex() {
58          this(System.getProperty("java.class.path"));
59      }
60      
61      /***
62       * Creates an index of the given classpath.
63       */
64      public ClassIndex(String classpath) {
65          map = new FQMap();
66          int idx = -1;
67          int current = 0;
68          while( (idx = classpath.indexOf(File.pathSeparatorChar, idx)) != -1 ) {
69              String path = classpath.substring(current, idx);
70  
71              File file = new File(path);
72              Finder finder = null;
73              if(file.isDirectory()) {
74                  // find all .class files.
75                  finder = new FileFinder();
76              } else {
77                  // assume it's a zip/jar
78                  // open and find all .class files
79                  finder = new ZipFinder();
80              }
81              finder.addFindListener(this);
82              HashMap options = new HashMap();
83              options.put(Finder.NAME, "class");
84              finder.find(file, options);
85  
86              idx++;
87              current = idx;
88          }
89  
90      }
91  
92      /***
93       * Get the package that a given classname is in.
94       */
95      public String getPackage(String classname) {
96          return (String)map.get(classname);
97      }
98  
99      public Collection getRootClasses() {
100         return getClassesIn("");
101     }
102     publicong> Collection getClassesIn(String packagename) {
103         FQMap tmp = map;
104         if(!"".equals(packagename)) {
105             tmp = (FQMap)map.get(packagename);
106         }
107         Collection[] colls = tmp.getSeparatedValues();
108         return colls[1];
109     }
110 
111     public Collection getRootPackages() {
112         return getPackagesIn("");
113     }
114     publicong> Collection getPackagesIn(String packagename) {
115         FQMap tmp = map;
116         if(!"".equals(packagename)) {
117             tmp = (FQMap)map.get(packagename);
118         }
119         Collection[] colls = tmp.getSeparatedValues();
120         return colls[0];
121     }
122 
123     public void directoryStarted(FindEvent findEvent) {
124         // ignore
125     }
126 
127     public void directoryFinished(FindEvent findEvent) {
128         // ignore
129     }
130 
131     public void fileFound(FindEvent findEvent) {
132         File file = findEvent.getFile();
133         String filename = file.getName();
134 
135         // ignore inner classes
136         if(filename.indexOf("$") != -1) {
137             return;
138         }
139 
140         filename = filename.substring(0,filename.length()-6);
141 
142         if(map.get(filename) != null) {
143             // simulate the import method of getting the first one
144             return;
145         }
146 
147         String pck = findEvent.getDirectory().getPath();
148         pck = pck.replace('/','.');
149         while(pck.startsWith(".")) {
150             pck = pck.substring(1);
151         }
152 
153         map.put(filename, pck);
154     }
155 
156 }
157