1 30 31 package org.theshoemakers.which4j; 32 33 import java.io.File ; 34 import java.net.URL ; 35 import java.net.URLClassLoader ; 36 import java.util.Properties ; 37 import java.util.StringTokenizer ; 38 39 54 public class Which4J { 55 56 private static boolean debug = false; 57 58 65 public static void main(String [] args) { 66 String s = System.getProperty("line.separator"); 67 if (args.length == 2) { 68 if (args[0].equals("-debug")) { 69 debug = true; 70 findIt(args[1]); 71 } else { 72 System.err.println( 73 "error: unrecognized \"" + args[0] + "\" option "); 74 System.exit(-1); 75 } 76 } else if (args.length == 1 && args[0].equals("-help")) { 77 usage(); 78 } else if (args.length == 1) { 79 findIt(args[0]); 80 } else { 81 usage(); 82 } 83 84 System.exit(0); 85 } 86 87 private static void usage() { 88 System.err.println( 89 "usage: java Which4J [-debug] <classname>" 90 + "\n\tThe commandline version of Which4J will search the system" 91 + "\n\tclasspath defined in your environment for all occurences" 92 + "\n\tof the class. Alternatively, you can use this class" 93 + "\n\tprogrammatically to search the current (or any) ClassLoader." 94 + "\n\tSee the javadoc for more detail." 95 + "\n\t" 96 + "\n\tNote: if the name of the jar file listed after \"found in:\"" 97 + "\n\tdoesn't match the name of the jar listed next to \"url:\", then " 98 + "\n\tthere is likely a \"Class-Path\" entry in the jar manifest " 99 + "\n\tthat is causing the classloader to indirectly find the class."); 100 System.exit(-1); 101 } 102 103 109 private static void findIt(String classname) { 110 111 try { 112 String classpath = System.getProperty("java.class.path", ""); 114 115 if (classpath.equals("")) { 116 System.err.println("error: classpath is not set"); 117 } 118 119 if (debug) { 120 System.out.println("classname: " + classname); 121 System.out.println("system classpath = " + classpath); 122 } 123 124 if (isPrimitiveOrVoid(classname)) { 125 System.out.println("'" + classname + "' primitive"); 126 return; 127 } 128 129 StringTokenizer st = 130 new StringTokenizer (classpath, File.pathSeparator); 131 132 String classnameAsResource = classname.replace('.', '/') + ".class"; 133 boolean checkSystemClassLoader = true; 134 135 while (st.hasMoreTokens()) { 136 String token = st.nextToken(); 137 File classpathElement = new File (token); 138 139 if (debug) 140 System.out.println( 141 classpathElement.isDirectory() 142 ? "dir: " + token 143 : "jar: " + token); 144 145 URL [] url = { classpathElement.toURL()}; 146 147 URLClassLoader cl = URLClassLoader.newInstance(url, null); 148 149 URL it = cl.findResource(classnameAsResource); 150 if (it != null) { 151 checkSystemClassLoader = false; 152 System.out.println("found in: " + token); 153 System.out.println(" url: " + it.toString()); 154 System.out.println(""); 155 } 156 } 157 158 if (checkSystemClassLoader) { 159 URL it = ClassLoader.getSystemResource(classnameAsResource); 160 if (it != null) { 161 checkSystemClassLoader = false; 162 System.out.println("found in url: " + it.toString()); 163 System.out.println(""); 164 } 165 } 166 } catch (Exception e) { 167 e.printStackTrace(); 168 } 169 } 170 171 179 public static String which(String classname) { 180 return which(classname, Which4J.class.getClassLoader()); 181 } 182 183 190 public static String which(String classname, ClassLoader loader) { 191 192 if (isArrayType(classname)) { 193 classname = getElementType(classname); 194 } 195 196 if (isPrimitiveOrVoid(classname)) { 197 return "'" + classname + "' primitive"; 198 } 199 200 String classnameAsResource = classname.replace('.', '/') + ".class"; 201 202 if (loader == null) { 203 loader = ClassLoader.getSystemClassLoader(); 206 } 207 URL it = loader.getResource(classnameAsResource); 208 if (it != null) { 209 return it.toString(); 210 } else { 211 return null; 212 } 213 } 214 215 223 public static String which(Class clazz) { 224 return which(clazz, clazz.getClassLoader()); 225 } 226 227 234 public static String which(Class clazz, ClassLoader loader) { 235 return which(clazz.getName(), loader); 236 } 237 238 246 private static boolean isPrimitiveOrVoid(String classname) { 247 return ( 248 classname.equals("boolean") 249 || classname.equals("byte") 250 || classname.equals("char") 251 || classname.equals("double") 252 || classname.equals("float") 253 || classname.equals("int") 254 || classname.equals("long") 255 || classname.equals("short") 256 || classname.equals("void")); 257 } 258 259 267 private static boolean isArrayType(String classname) { 268 return classname.startsWith("["); 269 } 270 271 279 private static String getElementType(String classname) { 280 String elementType = classname.split("^\\[+")[1]; 282 char encoding = elementType.charAt(0); 283 284 if (encoding == 'L') { 285 return elementType.substring(1, elementType.length() - 1); 287 } else if (encoding == 'Z') { 288 return "boolean"; 289 } else if (encoding == 'B') { 290 return "byte"; 291 } else if (encoding == 'C') { 292 return "char"; 293 } else if (encoding == 'D') { 294 return "double"; 295 } else if (encoding == 'F') { 296 return "float"; 297 } else if (encoding == 'I') { 298 return "int"; 299 } else if (encoding == 'J') { 300 return "long"; 301 } else if (encoding == 'S') { 302 return "short"; 303 } else { 304 throw new InternalError ( 305 "unknown array type: '" 306 + encoding 307 + "'.\nplease file a bug report at http://which4j.dev.java.net"); 308 } 309 } 310 311 } 312 | Popular Tags |