1 36 package jline; 37 38 import java.io.*; 39 import java.net.*; 40 import java.util.*; 41 import java.util.jar.JarFile ; 42 import java.util.jar.JarEntry ; 43 44 45 51 public class ClassNameCompletor 52 extends SimpleCompletor 53 { 54 58 public ClassNameCompletor () 59 throws IOException 60 { 61 this (null); 62 } 63 64 65 public ClassNameCompletor (final SimpleCompletorFilter filter) 66 throws IOException 67 { 68 super (getClassNames (), filter); 69 setDelimiter ("."); 70 } 71 72 73 public static String [] getClassNames () 74 throws IOException 75 { 76 Set urls = new HashSet (); 77 for (ClassLoader loader = ClassNameCompletor.class.getClassLoader (); 78 loader != null; loader = loader.getParent ()) 79 { 80 if (!(loader instanceof URLClassLoader)) 81 continue; 82 83 urls.addAll (Arrays.asList (((URLClassLoader)loader).getURLs ())); 84 } 85 86 Class [] systemClasses = new Class [] { 90 String .class, 91 javax.swing.JFrame .class 92 }; 93 for (int i = 0; i < systemClasses.length; i++) 94 { 95 URL classURL = systemClasses[i].getResource ("/" 96 + systemClasses[i].getName ().replace ('.', '/') + ".class"); 97 if (classURL != null) 98 { 99 URLConnection uc = (URLConnection)classURL.openConnection (); 100 if (uc instanceof JarURLConnection) 101 urls.add (((JarURLConnection)uc).getJarFileURL ()); 102 } 103 } 104 105 106 Set classes = new HashSet (); 107 for (Iterator i = urls.iterator (); i.hasNext (); ) 108 { 109 URL url = (URL)i.next (); 110 File file = new File (url.getFile ()); 111 if (file.isDirectory ()) 112 { 113 Set files = getClassFiles (file.getAbsolutePath (), 114 new HashSet (), file, new int[] { 200 }); 115 classes.addAll (files); 116 continue; 117 } 118 119 if (file == null || !file.isFile ()) continue; 121 122 JarFile jf = new JarFile (file); 123 for (Enumeration entries = jf.entries (); 124 entries.hasMoreElements () ;) 125 { 126 JarEntry entry = (JarEntry )entries.nextElement (); 127 if (entry == null) 128 continue; 129 130 String name = entry.getName (); 131 if (!name.endsWith (".class")) continue; 133 134 classes.add (name); 135 } 136 } 137 138 Set classNames = new TreeSet (); 141 for (Iterator i = classes.iterator (); i.hasNext (); ) 142 { 143 String name = (String )i.next (); 144 classNames.add (name.replace ('/', '.').substring (0, 145 name.length () - 6)); 146 } 147 148 return (String [])classNames.toArray (new String [classNames.size ()]); 149 } 150 151 152 private static Set getClassFiles (String root, Set holder, File directory, 153 int[] maxDirectories) 154 { 155 if (maxDirectories[0]-- < 0) 157 return holder; 158 159 File [] files = directory.listFiles (); 160 for (int i = 0; files != null && i < files.length; i++) 161 { 162 String name = files[i].getAbsolutePath (); 163 if (!(name.startsWith (root))) 164 continue; 165 else if (files[i].isDirectory ()) 166 getClassFiles (root, holder, files[i], maxDirectories); 167 else if (files[i].getName ().endsWith (".class")) 168 holder.add (files[i].getAbsolutePath ().substring ( 169 root.length () + 1)); 170 } 171 172 return holder; 173 } 174 } 175 176 | Popular Tags |