/Users/lyon/j4p/src/classUtils/putils/ClassPathUtils.java

1    package classUtils.putils; 
2     
3    import classUtils.dumper.ClassFile; 
4    import classUtils.loaders.ByteCodeContainer; 
5    import classUtils.loaders.Reloader; 
6    import classUtils.reflection.MethodList; 
7    import futils.DirList; 
8    import futils.Futil; 
9    import futils.Ls; 
10   import utils.StringUtils; 
11   import utils.SystemUtils; 
12    
13   import java.io.File; 
14   import java.io.FileInputStream; 
15   import java.io.IOException; 
16   import java.io.InputStream; 
17   import java.util.Arrays; 
18   import java.util.Comparator; 
19   import java.util.Enumeration; 
20   import java.util.Properties; 
21   import java.util.StringTokenizer; 
22   import java.util.Vector; 
23   import java.util.jar.JarEntry; 
24   import java.util.jar.JarFile; 
25   import java.util.jar.JarInputStream; 
26   import java.util.zip.ZipEntry; 
27    
28   public class ClassPathUtils { 
29       public static void main(String args[]) { 
30           //testGetPathsComposite(); 
31           getClassesWithMain(); 
32       } 
33    
34       private static void testGetPathsComposite() { 
35           testGetClassPathInUserDir(); 
36           System.out.println("The classpath=" + getClassPath()); 
37           System.out.println("the above is in the users directory..."); 
38           testGetPaths(); 
39       } 
40    
41       private static void testGetPaths() { 
42           String s[] = ClassPathUtils.getClassPaths(); 
43           print(s); 
44       } 
45    
46       private static void testGetClassPathInUserDir() { 
47           String s = getClassPathInUserDir(); 
48           System.out.println(s); 
49       } 
50    
51       /** 
52        * Search the class path and return the first 
53        * one that appears in the users directory 
54        * 
55        * @return string representing a path in the users directory 
56        */ 
57       public static String getClassPathInUserDir() { 
58           String s[] = getClassPaths(); 
59           String usrDir = SystemUtils.getUserDir(); 
60           for (int i = 0; i < s.length; i++) 
61               if (s[i].indexOf(usrDir) >= 0) { 
62                   String suspectedPath = s[i] + SystemUtils.getDirectorySeparator(); 
63                   File f = new File(suspectedPath); 
64                   if (f.isDirectory()) return suspectedPath; 
65               } 
66           return Futil.getReadDirFile("select a user class path directory").getAbsolutePath(); 
67       } 
68    
69       private static void testGetClassPath() { 
70           String s = getClassPath(); 
71           System.out.println(s); 
72       } 
73    
74       public static void testJarList() { 
75           JarFile jf = getAJarFile(); 
76           printEntries(jf); 
77           //loadJarEntry(jf); 
78           //System.out.println("---- Jar entries are ---"); 
79           // print(jf); 
80           //System.out.println("---- Class Entries are ---"); 
81           //Class ca[] = getClasses(jf); 
82           //print(ca); 
83       } 
84    
85       public static Class[] getClasses(JarFile jf) { 
86           loadJar(jf); 
87           return classList.getClasses(); 
88       } 
89    
90       public static void testGetComputableClasses() { 
91           print("The class files are:"); 
92           File cf[] = Reloader.getClassFiles(); 
93           loadClasses(cf); 
94           Class c[] = getClasses(); 
95           print(getComputableClasses(c)); 
96    
97           //ClassList.sort(ca); 
98    
99           //PackageTree pt = new PackageTree(buildTree(ca)); 
100          //pt.setBounds(0, 0, 380, 280); 
101          //pt.setVisible(true); 
102      } 
103   
104      public static File[] getClassFiles() { 
105          DirList dl = new DirList(".class"); 
106          return dl.getFiles(); 
107      } 
108   
109      public static void printInterfaces(Class c[]) { 
110          for (int i = 0; i < c.length; i++) 
111              printComputableObjects(c[i]); 
112      } 
113   
114      public static Class[] getComputableClasses(Class c[]) { 
115          Vector v = new Vector(); 
116          for (int i = 0; i < c.length; i++) 
117              if (implementsComputableObject(c[i])) 
118                  v.addElement(c[i]); 
119          Class ca[] = new Class[v.size()]; 
120          v.copyInto(ca); 
121          return ca; 
122      } 
123   
124      public static void printComputableObjects(Class c) { 
125          if (implementsComputableObject(c)) { 
126              System.out.println("ComputableObjectFound!:" + 
127                      c.getName()); 
128          } 
129      } 
130   
131      private static void printClassArray(Class[] classArray) { 
132          print("The class array is"); 
133          print(classArray); 
134      } 
135   
136      private static void printJars() { 
137          JarFile jfs[] = getJarFiles(); 
138          print(jfs); 
139          //loadJars(jfs); 
140      } 
141   
142      public static String classFileToClassName(File ccf) { 
143          // you need the BCEL for this to compile. 
144          //JavaClass jc = null; 
145          //try { 
146          //  ClassParser cp = new ClassParser(ccf.toString()); 
147          //  jc = cp.parse(); 
148          //} catch (IOException e) { 
149          //  e.printStackTrace(); 
150          //  System.exit(0); 
151          // } catch (ClassFormatError e) { 
152          //   e.printStackTrace(); 
153          //  System.exit(0); 
154          // } 
155          //  print("converted " + ccf); 
156          //  print("to a class called:" + jc.getClassName()); 
157          //  return jc.getClassName(); 
158          return null; 
159      } 
160   
161      public static void addClassPath(String dir) { 
162          Properties p = System.getProperties(); 
163          String oldPath = System.getProperty("java.class.path"); 
164          String newPath = dir + 
165                  System.getProperty("path.separator") + 
166                  oldPath; 
167          p.put("java.class.path", newPath); 
168          System.setProperties(p); 
169      } 
170   
171      public static void addClassPath(File f[]) { 
172          for (int i = 0; i < f.length; i++) 
173              addClassPath(f[i].toString()); 
174      } 
175   
176      public static boolean contains(String s1, 
177                                     String s2) { 
178          if (-1 == s1.indexOf(s2)) return false; 
179          return true; 
180      } 
181   
182      public static Vector buildTree(Class ca[]) { 
183          Vector root = new Vector(); 
184          Package p[] = getPackages(); 
185          Vector pv = new Vector(); 
186          pv.add("packages"); 
187          Vector cv = new Vector(); 
188          for (int i = 0; i < p.length; i++) { 
189              Package pp = p[i]; 
190              String packageName = pp.getName(); 
191              pv.add(packageName); 
192          } 
193          root.add(pv); 
194          for (int j = 0; j < ca.length; j++) { 
195              if ((j % 10) == 0) { 
196                  root.add(cv); 
197                  cv = new Vector(); 
198              } 
199              String className = ca[j].getName(); 
200              // System.out.println(className); 
201              if (contains(className, 
202                      "cutils.putils")) 
203                  ; 
204              cv.add(className); 
205          } 
206          root.add(cv); 
207          return root; 
208      } 
209   
210      public static void print(Object o) { 
211          System.out.println(o); 
212      } 
213   
214      public static void printSystemInfo() { 
215          System.out.println("loadPackages:"); 
216          loadPackages(); 
217          System.out.println("loaded packages"); 
218          print(getPackages()); 
219          System.out.println("loaded " 
220                  + classList.getSize() 
221                  + " classes"); 
222          System.out.println("number of methods=" + 
223                  classList.getNumberOfMethods()); 
224          print(classList.getClasses()); 
225      } 
226   
227      private static ClassList classList = new ClassList(); 
228   
229      /** 
230       * list all the classes loaded into the 
231       * system. 
232       */ 
233      public static Class[] getClasses() { 
234          return classList.getClasses(); 
235      } 
236   
237      public static void printClassPaths() { 
238          String s[] = getClassPaths(); 
239          print(s); 
240      } 
241   
242      public static void printJars(String jfn[]) { 
243          for (int i = 0; i < jfn.length; i++) 
244              printJars(jfn[i]); 
245      } 
246   
247      public static void printJars(String jfn) { 
248          try { 
249              JarFile jf = new JarFile(jfn); 
250              System.out.println("jarName=" + jf.getName()); 
251              print(jf); 
252          } catch (java.io.IOException e) { 
253              e.printStackTrace(); 
254          } 
255      } 
256   
257      public static void loadJars(JarFile jf[]) { 
258          if (jf == null) return; 
259          for (int i = 0; i < jf.length; i++) 
260              loadJar(jf[i]); 
261      } 
262   
263      public static void loadClasses(File cf[]) { 
264          if (cf == null) return; 
265          for (int i = 0; i < cf.length; i++) 
266              loadClass(cf[i].toString()); 
267      } 
268   
269      public static void loadJar(JarFile jf) { 
270          for (Enumeration e = jf.entries(); 
271               e.hasMoreElements();) { 
272              Object o = e.nextElement(); 
273              String s = o.toString(); 
274              loadClass(s); 
275          } 
276      } 
277   
278      /** 
279       * <code>loadClass</code> will load a class, 
280       * at your peril static data members will be 
281       * automatically invoked upon load using this 
282       * method. Such elements should probably be 
283       * invoked from within a thread. 
284       */ 
285      public static void loadClass(String classString) { 
286          if (!classString.endsWith(".class")) { 
287              System.out.println("classString does not end with .class" 
288                      + classString); 
289              return; 
290          } 
291          String s = makeClassString(classString); 
292          System.out.println("attempting to load:" + s); 
293          try { 
294              Class c = null; 
295              try { 
296                  c = loadClassName(s); 
297              } catch (Exception e) { 
298              } 
299              if (c == null) return; 
300              classList.add(c); 
301          } catch (RuntimeException e) { 
302              System.out.println(classString + 
303                      ":failed to load"); 
304          } 
305      } 
306   
307      /** 
308       * takes a string of the java/lang/String.class 
309       * form and returns java.lang.String 
310       */ 
311      public static String makeClassString(String s) { 
312          s = StringUtils.sub(s, ".class", ""); 
313          s = s.replace('/', '.'); 
314          s = s.replace('\\', '.'); 
315          //s = ReplaceString.sub(s, "c:.lyon.j4p.classes.", ""); 
316          return s; 
317      } 
318   
319      private static Class loadClassName(String s) { 
320          Class c = null; 
321          try { 
322              c = Class.forName(s); 
323          } catch (Exception e) { 
324              System.out.println("cant load in loadClassName:" + 
325                      s); 
326          } 
327          return c; 
328      } 
329   
330      public static void testGetAClass() { 
331          JarFile jf = getAJarFile(); 
332          JarEntry je[] = getClassEntries(jf); 
333          for (int i = 0; i < je.length; i++) { 
334              byte b[] = getByteCodes(jf, je[i]); 
335              System.out.println(je[i].getName() + " read " + 
336                      b.length + 
337                      " bytes"); 
338              Class c = getAClass(b); 
339              System.out.println("got a class!" + c.getName()); 
340          } 
341      } 
342   
343      public static void getClassesWithMain() { 
344          File f[] = ClassPathUtils.getClassFiles(); 
345          for (int i = 0; i < f.length; i++) { 
346              ClassFile classFile = ClassFile.getClassFile(f[i]); 
347              String className = classFile.getClassName(); 
348              Class c = null; 
349              try { 
350                  c = Class.forName(className); 
351              } catch (Exception e) { 
352                  continue; 
353              } 
354              MethodList ml = new MethodList(c); 
355              if (ml.hasMainMethod()) 
356                  System.out.println("classes:" + className + " has main method"); 
357          } 
358      } 
359   
360      public static Class getAClass() { 
361          return getAClass(futils.Futil.getReadFile("select a class file")); 
362      } 
363   
364      public static Class getAClass(File f) { 
365          byte b[] = Futil.getBytes(f); 
366          return getAClass(b); 
367      } 
368   
369      public static Class getAClass(byte b[]) { 
370          ByteCodeContainer bcc = new ByteCodeContainer(b); 
371          return bcc.getLoadedClass(); 
372      } 
373   
374      public static void testGetBytesCodes() { 
375          JarFile jf = getAJarFile(); 
376          JarEntry je[] = getClassEntries(jf); 
377          for (int i = 0; i < je.length; i++) { 
378              byte b[] = getByteCodes(jf, je[i]); 
379              System.out.println(je[i].getName() + 
380                      " read " 
381                      + b.length + " bytes"); 
382          } 
383      } 
384   
385      public static byte[] getByteCodes(JarFile jf, 
386                                        JarEntry je) { 
387          byte b[] = new byte[(int) je.getSize()]; 
388          ZipEntry ze = jf.getEntry(je.getName()); 
389          try { 
390              InputStream is = jf.getInputStream(ze); 
391              is.read(b); 
392          } catch (IOException e) { 
393              e.printStackTrace(); 
394          } 
395          return b; 
396      } 
397   
398      public static void printEntries(JarFile jf) { 
399          print(getClassEntries(jf)); 
400      } 
401   
402      public static JarEntry[] getGifEntries(JarFile jf) { 
403          Vector v = new Vector(); 
404          for (Enumeration e = jf.entries(); 
405               e.hasMoreElements();) { 
406              Object o = e.nextElement(); 
407              JarEntry je = (JarEntry) o; 
408              String s = je.getName(); 
409              if (s.endsWith(".gif") && 
410                      (-1 == s.indexOf('$'))) 
411                  v.addElement(je); 
412          } 
413          JarEntry je[] = new JarEntry[v.size()]; 
414          v.copyInto(je); 
415          return je; 
416      } 
417   
418      public static JarEntry[] getClassEntries(JarFile jf) { 
419          Vector v = new Vector(); 
420          for (Enumeration e = jf.entries(); 
421               e.hasMoreElements();) { 
422              Object o = e.nextElement(); 
423              JarEntry je = (JarEntry) o; 
424              String s = je.getName(); 
425              if (s.endsWith(".class") && 
426                      (-1 == s.indexOf('$'))) 
427                  v.addElement(je); 
428          } 
429          JarEntry je[] = new JarEntry[v.size()]; 
430          v.copyInto(je); 
431          return je; 
432      } 
433   
434      public static JarEntry[] getJarEntries(JarFile jf) { 
435          Vector v = new Vector(); 
436          for (Enumeration e = jf.entries(); 
437               e.hasMoreElements();) { 
438              Object o = e.nextElement(); 
439              JarEntry je = (JarEntry) o; 
440              v.addElement(je); 
441          } 
442          JarEntry je[] = new JarEntry[v.size()]; 
443          v.copyInto(je); 
444          return je; 
445      } 
446   
447      public static void print(Object s[]) { 
448          if (s == null) return; 
449          for (int i = 0; i < s.length; i++) 
450              System.out.println(s[i]); 
451          System.out.println("found:" + s.length + " items"); 
452      } 
453   
454      public static boolean implementsComputableObject(Class c) { 
455          return implementsComputableObject(c.getInterfaces()); 
456      } 
457   
458      public static boolean implementsComputableObject(Class s[]) { 
459          if (s == null) return false; 
460          for (int i = 0; i < s.length; i++) 
461              if (s[i].getName().endsWith("ComputableObject")) 
462                  return true; 
463          return false; 
464      } 
465   
466      public static void print(Class s[]) { 
467          if (s == null) return; 
468          for (int i = 0; i < s.length; i++) 
469              System.out.println(s[i].getName()); 
470          System.out.println("found:" + s.length + " Classes"); 
471      } 
472   
473      public static JarFile[] getJars() { 
474          String s[] = getClassPaths(); 
475          Vector v = new Vector(); 
476          for (int i = 0; i < s.length; i++) 
477              try { 
478                  JarFile j = new JarFile(s[i]); 
479                  v.addElement(j); 
480              } catch (java.io.IOException e) { 
481              } 
482          JarFile jf[] = new JarFile[v.size()]; 
483          v.copyInto(jf); 
484          return jf; 
485      } 
486   
487      public static int countJars(String s[]) { 
488          int n = 0; 
489          for (int i = 0; i < s.length; i++) 
490              if (s[i].endsWith("jar")) n++; 
491          return n; 
492      } 
493   
494      public static JarFile getAJarFile() { 
495          File f = 
496                  futils.Futil.getReadFile("select a jar file"); 
497          JarFile jf = null; 
498          try { 
499              jf = new JarFile(f); 
500          } catch (IOException e) { 
501              e.printStackTrace(); 
502          } 
503          return jf; 
504      } 
505   
506      public static JarFile[] getJarFiles() { 
507          DirList dl = new DirList(".jar"); 
508          File f[] = dl.getFiles(); 
509          JarFile jf[] = new JarFile[f.length]; 
510          for (int i = 0; i < jf.length; i++) 
511              try { 
512                  jf[i] = new JarFile(f[i]); 
513              } catch (IOException e) { 
514                  System.out.println("could not make jar file out of:" + 
515                          f[i]); 
516              } 
517          return jf; 
518      } 
519   
520      public static String[] getClassPaths() { 
521          String cp = getClassPath(); 
522          String pathSeparator = System.getProperty("path.separator"); 
523          StringTokenizer st = 
524                  new StringTokenizer(cp, 
525                          pathSeparator); 
526          int n = st.countTokens(); 
527          String s[] = new String[n]; 
528          for (int i = 0; i < n; i++) 
529              s[i] = st.nextToken(); 
530          return s; 
531      } 
532   
533      public static String getClassPath() { 
534          return System.getProperty("java.class.path"); 
535      } 
536   
537      // add new classes for any 
538      // elements you want loaded. 
539      private static void loadPackages() { 
540          loadJars(getJars()); 
541          // loadClasses(getClassFiles()); 
542      } 
543   
544      private static Package[] getPackages() { 
545          return Package.getPackages(); 
546      } 
547   
548      public static void print(Package[] pkgs) { 
549          SortPackages(pkgs); 
550          for (int i = 0; i < pkgs.length; i++) 
551              System.out.println("  " + pkgs[i].getName()); 
552          System.out.println("printed:" + pkgs.length + 
553                  " packages."); 
554      } 
555   
556      private static void SortPackages(Package[] pkgs) { 
557          Arrays.sort(pkgs, 
558                  new PackageComparator()); 
559      } 
560   
561      public static void dumpAClassFile() { 
562          FileInputStream fis = Futil.getFileInputStream("Select a class file"); 
563          getComputableObjectImplementor(fis); 
564      } 
565   
566      public static void dumpClassFile(File f[]) { 
567          for (int i = 0; i < f.length; i++) 
568              dumpClassFile(f[i]); 
569      } 
570   
571      public static void dumpClassFile(File f) { 
572          try { 
573              FileInputStream fis = new FileInputStream(f); 
574              Class c = getComputableObjectImplementor(fis); 
575              if (c != null) 
576                  System.out.println("This class is a ComputableObject" + 
577                          c.getName()); 
578              fis.close(); 
579          } catch (IOException e) { 
580              e.printStackTrace(); 
581          } 
582      } 
583   
584      public static Class getComputableObjectImplementor(FileInputStream fis) { 
585          classUtils.dumper.ByteCodeContainer bcc = classUtils.dumper.ByteCodeContainer.getByteCodeContainer(fis); 
586          bcc.loadIt(); 
587          Class c = bcc.getClass(); 
588          Class interfaces[] = c.getInterfaces(); 
589          for (int i = 0; i < interfaces.length; i++) 
590              if (interfaces[i].getName().endsWith("ComputableObject")) 
591                  return c; 
592          return null; 
593      } 
594   
595      public static void dumpAllFilesInADirectory() { 
596          File f[] = Ls.getWildFiles(Futil.getReadDirFile("select a class directory"), "class"); 
597          dumpClassFile(f); 
598      } 
599   
600      private static class PackageComparator 
601              implements Comparator { 
602          public int compare(Object a, Object b) { 
603              return ((Package) a).getName() 
604                      .compareTo(((Package) b).getName()); 
605          } 
606      } 
607   
608      static void loadJarEntry(JarFile jf) { 
609          try { 
610              ZipEntry ze = jf.getEntry(null); 
611              jf.getInputStream(ze); 
612              JarInputStream in = new JarInputStream(new FileInputStream("")); 
613              //ZipEntry ze=  in.getNextEntry(); 
614              System.out.println("nextEntry is:" + ze); 
615              byte b[] = new byte[(int) ze.getSize()]; 
616              in.read(b); 
617              String s = ze.getName(); 
618              if (!s.endsWith(".class")) return; 
619              System.out.println("loading:" + s); 
620              ByteCodeContainer bcc = new ByteCodeContainer(b); 
621              bcc.loadIt(); 
622              in.close(); 
623          } catch (IOException e) { 
624              e.printStackTrace(); 
625          } 
626      } 
627  } 
628