/Users/lyon/j4p/src/bookExamples/ch24Reflection/MainUtils.java

1    package bookExamples.ch24Reflection; 
2     
3    import classUtils.dumper.ClassFile; 
4    import classUtils.reflection.MethodList; 
5    import futils.DirList; 
6    import futils.Customer; 
7    import gui.In; 
8     
9    import java.io.File; 
10   import java.lang.reflect.InvocationTargetException; 
11   import java.lang.reflect.Method; 
12   import java.util.Date; 
13    
14   import xml.adbk.Address; 
15   import addBk.address.AddressRecord; 
16    
17   /** 
18    * DocJava, Inc. 
19    * http://www.docjava.com 
20    * Programmer: dlyon 
21    * Date: Sep 29, 2004 
22    * Time: 9:05:56 PM 
23    */ 
24   public class MainUtils { 
25       public static void main(String[] args) { 
26           AddressRecord o = new AddressRecord(); 
27           System.out.println(o); 
28           printProps(o); 
29       } 
30    
31       private static void printProps(AddressRecord o) { 
32           MethodList ml = new MethodList(o.getClass()); 
33           Method ma [] = MethodList.getMethodsWithNArgs(ml.getPublicReadMethods(), 0); 
34           try { 
35               for (int i = 0; i < ma.length; i++) { 
36                   System.out.println(ma[i] + ":" + 
37                           ma[i].invoke(o, null).toString()); 
38               } 
39           } catch (IllegalAccessException e) { 
40               e.printStackTrace(); 
41           } catch (InvocationTargetException e) { 
42               e.printStackTrace(); 
43           } 
44       } 
45    
46       private static void bigMainTest() { 
47           try { 
48               testPrintMainClasses(); 
49           } catch (ClassNotFoundException e) { 
50               e.printStackTrace(); 
51           } 
52           In.message("done"); 
53       } 
54    
55       private static void testPrintMainClasses() 
56               throws ClassNotFoundException { 
57           DirList dl = new DirList(".class"); 
58           File fa[] = dl.getFilesNotDirectories(); 
59           for (int i = 0; i < fa.length; i++) 
60               printMainClasses(fa[i]); 
61    
62           // 1a.  Convert a class file into a Class. 
63           // 1b. convert the list of files into a list of classes. 
64           // 2. print out only those classes that contain a 
65           //    public static void main(String args[]) method. 
66       } 
67    
68       private static void printMainClasses(File f) throws ClassNotFoundException { 
69           ClassFile cf = ClassFile.getClassFile(f); 
70           String cn = cf.getClassName(); 
71           Class c = Class.forName(cn); 
72           if (!cf.hasMainMethod()) return; 
73           System.out.println(c.getName() + " has a main"); 
74           Method m[] = c.getMethods(); 
75           for (int i = 0; i < m.length; i++) 
76               System.out.println(m[i]); 
77       } 
78   } 
79