1 24 25 package org.aspectj.compiler.base.bytecode; 26 27 import org.aspectj.compiler.base.*; 28 import org.aspectj.compiler.base.ast.*; 29 30 import java.io.*; 31 import java.util.zip.*; 32 import java.util.StringTokenizer ; 33 34 import java.util.*; 35 36 public class ClassPathManager extends ClassManager { 37 List classManagers; 38 39 private ClassPathManager(JavaCompiler compiler, List classManagers) { 40 super(compiler); 41 this.classManagers = classManagers; 42 } 43 44 public ClassPathManager(JavaCompiler compiler, String classpath, 45 String bootclasspath, String extdirs) { 46 super(compiler); 47 List managers = new LinkedList(); 50 51 if (bootclasspath == null) { 52 bootclasspath = System.getProperty("sun.boot.class.path", ""); 53 } 54 addClasspathManagers(managers, bootclasspath); 55 56 if (extdirs == null) { 57 extdirs = System.getProperty("java.ext.dirs", ""); 58 } 59 addExtensionManagers(managers, extdirs); 60 61 if (classpath == null) { 62 classpath = System.getProperty("java.class.path", ""); 63 } 64 addClasspathManagers(managers, classpath); 65 66 classManagers = managers; 67 } 68 69 protected void addClasspathManagers(List managers, String classpath) { 70 StringTokenizer tok = new StringTokenizer (classpath, java.io.File.pathSeparator); 71 while (tok.hasMoreTokens()) { 72 String entry = tok.nextToken().trim(); 73 75 if (entry.endsWith(".jar") || entry.endsWith(".zip")) { 76 managers.add(new JarClassManager(getCompiler(), new File(entry))); 77 } else { 78 managers.add(new DirectoryClassManager(getCompiler(), new File(entry))); 79 } 80 } 81 } 82 83 84 protected void addExtensionManagers(List managers, String extdirs) { 85 StringTokenizer tok = new StringTokenizer (extdirs, java.io.File.pathSeparator); 86 while (tok.hasMoreTokens()) { 87 String entry = tok.nextToken().trim(); 88 addExtensionDirManagers(managers, new File(entry)); 89 } 90 } 91 92 protected void addExtensionDirManagers(List managers, File extdir) { 93 if (!extdir.isDirectory()) return; 94 95 String [] names = extdir.list(); 96 if (names == null) return; 97 98 for(int i=0; i<names.length; i++) { 99 if (!names[i].endsWith(".jar") && !names[i].endsWith(".zip")) { 101 continue; 102 } 103 104 File file = new File(extdir, names[i]); 105 if (file.isFile()) { 106 managers.add(new JarClassManager(getCompiler(), file)); 107 } 108 } 109 } 110 111 private String join(String [] names) { 112 StringBuffer buf = new StringBuffer (); 113 buf.append(names[0]); 114 for(int i=1; i<names.length; i++) { 115 buf.append('.'); 116 buf.append(names[i]); 117 } 118 return buf.toString(); 119 } 120 121 public TypeDec findTypeDec(String name) { 122 for (Iterator i = classManagers.iterator(); i.hasNext(); ) { 123 ClassManager cm = (ClassManager)i.next(); 124 TypeDec ret = cm.findTypeDec(name); 125 if (ret != null) return ret; 126 } 127 return null; 128 } 129 130 public ClassManager makeSubPackageManager(String subPackageName) { 131 List ret = new LinkedList(); 132 for (Iterator i = classManagers.iterator(); i.hasNext(); ) { 135 ClassManager cm = (ClassManager)i.next(); 136 ClassManager newCM = cm.makeSubPackageManager(subPackageName); 137 if (newCM != null) ret.add(newCM); 138 } 139 return new ClassPathManager(getCompiler(), ret); 143 } 144 145 public Collection getAllPossibleTypeNames() { 146 Collection ret = new ArrayList(); 147 for (Iterator i = classManagers.iterator(); i.hasNext(); ) { 148 ClassManager cm = (ClassManager)i.next(); 149 ret.addAll(cm.getAllPossibleTypeNames()); 150 } 151 return ret; 152 } 153 154 public Collection getPathStrings() { 155 Collection ret = new ArrayList(); 156 for (Iterator i = classManagers.iterator(); i.hasNext(); ) { 157 ClassManager cm = (ClassManager)i.next(); 158 ret.addAll(cm.getPathStrings()); 159 } 160 return ret; 161 } 162 163 } 164 165 166 class DirectoryClassManager extends ClassManager { 167 File directory; 168 public DirectoryClassManager(JavaCompiler compiler, File directory) { 169 super(compiler); 170 this.directory = directory; 171 } 172 173 public ClassManager makeSubPackageManager(String subPackageName) { 174 File dir = new File(directory, subPackageName); 175 if (!dir.isDirectory()) return null; 176 return new DirectoryClassManager(getCompiler(), dir); 177 } 178 179 public TypeDec findTypeDec(String name) { 180 File testFile = new File(directory, name+".class"); 181 if (name.equals("nul")) return null; 184 if (testFile.isFile()) { 185 try { 186 return makeTypeDec(name, new FileInputStream(testFile)); 188 } catch (FileNotFoundException e) { 189 return null; 190 } 191 } else { 192 return null; 193 } 194 } 195 public Collection getAllPossibleTypeNames() { 196 Collection temp = new ArrayList(); 197 collectNames(directory, Collections.EMPTY_LIST, temp); 198 return temp; 201 } 202 203 private void collectNames(File directory, List prefixName, Collection names) { 204 File[] files = directory.listFiles(); 205 if (files == null) return; 206 for (int i = files.length - 1; i >= 0; i--) { 207 File f = files[i]; 208 if (f.isDirectory()) { 209 collectNames(f, newList(prefixName, f.getName()), names); 210 } else if (isClassFile(f)) { 211 names.add(newList(prefixName, filenameToClassName(f.getName()))); 212 } 213 } 214 } 215 216 private List newList(List left, Object right) { 217 ArrayList temp = new ArrayList(left); 218 temp.add(right); 219 return temp; 220 } 221 222 private List newList(List left, List right) { 223 ArrayList temp = new ArrayList(left); 224 temp.addAll(right); 225 return temp; 226 } 227 228 private boolean isClassFile(File f) { 229 return f.isFile() && f.getName().toLowerCase().endsWith(".class"); 230 } 231 232 public String toString() { return "dir(" + directory + ")"; } 233 234 protected String getPathString() { 235 return directory.toString(); 236 } 237 } 238 239 240 241 class JarClassManager extends ClassManager { 242 File origFile; 243 ZipFile zipFile; 244 String prefix = null; 245 246 private JarClassManager(JavaCompiler compiler, ZipFile zipFile, String prefix) { 247 super(compiler); 248 this.zipFile = zipFile; 249 this.prefix = prefix; 250 } 251 252 public JarClassManager(JavaCompiler compiler, File jarFile) { 253 super(compiler); 254 origFile = jarFile; 255 if (jarFile.isFile()) { 257 try { 259 zipFile = new ZipFile(jarFile); 260 } catch (IOException e) { 261 zipFile = null; 262 } 263 } else { 264 zipFile = null; 265 } 266 } 267 268 public ClassManager makeSubPackageManager(String subPackageName) { 269 if (zipFile == null) return null; 270 271 String newPrefix; 272 if (prefix == null) newPrefix = subPackageName + '/'; 273 else newPrefix = prefix + subPackageName + '/'; 274 275 277 JarClassManager ret = new JarClassManager(getCompiler(), zipFile, newPrefix); 278 return ret; 279 } 280 281 public TypeDec findTypeDec(String name) { 282 if (zipFile == null) return null; 283 284 String fileName = prefix + name + ".class"; 285 286 ZipEntry entry = zipFile.getEntry(fileName); 287 if (entry == null) return null; 288 289 try { 290 return makeTypeDec(name, zipFile.getInputStream(entry)); 291 } catch (IOException e) { 292 return null; 293 } 294 } 295 296 public Collection getAllPossibleTypeNames() { 297 Collection ret = new ArrayList(); 298 if (zipFile == null) return ret; 299 300 for (Enumeration i = zipFile.entries(); i.hasMoreElements(); ) { 301 ZipEntry entry = (ZipEntry)i.nextElement(); 302 if (entry.isDirectory()) continue; 303 String name = entry.getName(); 304 if (name.toLowerCase().endsWith(".class")) { 305 ret.add(filenameToClassName(name)); 306 } 307 } 308 return ret; 309 } 310 311 public String toString() { return "jar(" + zipFile.getName() + ")"; } 312 313 protected String getPathString() { 314 if (zipFile == null) { 315 return origFile.toString() + "<not found>"; 316 } else { 317 return origFile.toString(); 318 } 319 } 320 } 321 | Popular Tags |