New test.
[mono.git] / mcs / tools / misc / IFaceDisco.cs
1 // IFaceDisco.cs\r
2 //\r
3 // Nick Drochak (ndrochak@gol.com)\r
4 //\r
5 // (C) 2001 Nick Drochak\r
6 \r
7 using System;\r
8 using System.Reflection;\r
9 using System.Collections;\r
10 using System.IO;\r
11 \r
12 namespace Mono.Util\r
13 {\r
14         class IFaceDisco {\r
15                 public static void Main(string[] args) {\r
16                         Assembly                                        asm;\r
17                         Type[]                                          asmTypes;\r
18                         InterfaceMapping                map;\r
19                         Type[]                                          interfaces;\r
20                         ArrayList                                       TypesList                                       = new ArrayList();\r
21                         ArrayList                                       implementingTypes       = new ArrayList();\r
22                         string                                          asmFullPath                             = null;\r
23                         string                                          ifaceToDiscover         = null;\r
24                         \r
25                         if (args.Length < 1 || args.Length > 3) {\r
26                                 Usage();\r
27                                 return;\r
28                         }\r
29 \r
30                         for (int i = 0; i < args.Length; i++) {\r
31                                 string arg = args[i];\r
32 \r
33                                 if (arg.StartsWith("-") && ((i + 1) < args.Length)) {\r
34                                         if (arg == "--asm") {\r
35                                                 asmFullPath = args[++i];\r
36                                         } else {\r
37                                                 Usage();\r
38                                                 return;\r
39                                         }\r
40                                 } else {\r
41                                         // allow only one interface to discover\r
42                                         if (ifaceToDiscover != null){\r
43                                                 Usage();\r
44                                                 return;\r
45                                         }\r
46                                         ifaceToDiscover = arg;\r
47                                 }\r
48                         }\r
49 \r
50                         // find the assembly\r
51                         if (null == asmFullPath){\r
52                                 asm = Assembly.GetAssembly(typeof (System.Object));\r
53                         }\r
54                         else {\r
55                                 try{\r
56                                         asm = Assembly.LoadFrom(asmFullPath);\r
57                                 }\r
58                                 catch(Exception e){\r
59                                         Console.WriteLine("Could not open assembly '{0}' for discovery. Error is: "+e.Message, asmFullPath);\r
60                                         return;\r
61                                 }\r
62                         }\r
63                         asmTypes = asm.GetTypes();\r
64 \r
65                         // examine all the public types\r
66                         foreach(Type t in asmTypes) {\r
67                                 if (t.IsPublic) {\r
68                                         // find out which, if any, interfaces are "in" the type\r
69                                         interfaces= t.GetInterfaces();\r
70                                         if (null != interfaces){\r
71                                                 // look for the interface we want to discover\r
72                                                 foreach (Type iface in interfaces) {\r
73                                                         // this area seems to throw an exception sometimes, just ignore it\r
74                                                         try{\r
75                                                                 if (iface.FullName.ToLower() == args[0].ToLower()) {\r
76                                                                         // find out if this type is the one which "declares" the interface\r
77                                                                         map = t.GetInterfaceMap(iface);\r
78                                                                         if (map.TargetMethods[0].DeclaringType.FullName == t.FullName){\r
79                                                                                 // if so, then we found a class to report\r
80                                                                                 implementingTypes.Add(t.FullName);\r
81                                                                         } // if\r
82                                                                 }  // if\r
83                                                         }catch{}\r
84                                                 } // foreach\r
85                                         } // if\r
86                                 } // if\r
87                         } // foreach\r
88 \r
89                         // sort the list to make it easier to find what you are looking for\r
90                         implementingTypes.Sort();\r
91                         Console.WriteLine(XMLUtil.ToXML(implementingTypes, "Type", "ImplementingTypes"));\r
92                 } // Main()\r
93 \r
94                 private static void Usage() {\r
95                         Console.WriteLine (\r
96                                 "Mono Interface Discovery Tool\n" +\r
97                                 "usage: ifacedisco [--asm assembly] interface\n\n" +\r
98                                 "  The full path to 'assembly' should be specified when using --asm.\n" +\r
99                                 "  If 'assembly' is not specified, the assembly that contains System.Object will be used.\n" +\r
100                                 "  Use the fully qualified form for 'interface', e.g. System.Runtime.Serialization.ISerializable\n"\r
101                                 );\r
102                 } // Usage()\r
103 \r
104         } // class IFaceDisco\r
105 }  // namespace Mono.Util\r