Monop: the all requrests station
[mono.git] / mcs / tools / monop / monop.cs
1 //
2 // monop -- a semi-clone of javap
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2004 Ben Maurer
8 //
9
10
11 using System;
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
15 using System.IO;
16
17 class MonoP {
18         static string assembly;
19         
20         // very common namespaces, all in corlib
21         static readonly string [] v_common_ns = {
22                 "System",
23                 "System.Collections",
24                 "System.Reflection",
25                 "System.Text",
26                 "System.IO"
27         };
28         
29         static readonly string [] common_assemblies = {
30                 "System.Xml.dll",
31                 "System.Web.dll",
32                 "gtk-sharp.dll",
33                 "glib-sharp.dll"
34         };
35         
36         static readonly string [] common_ns = {
37                 "System.Xml",
38                 "System.Web",
39                 "Gtk",
40                 "GLib"
41         };
42         
43         static Type GetType (string tname, bool ignoreCase)
44         {
45                 Type t;
46                 if (assembly != null){
47                         Assembly a = Assembly.Load (assembly);
48                         t = a.GetType (tname, false, ignoreCase);
49                 } else
50                         t = Type.GetType (tname, false, ignoreCase);
51
52                 return t;
53         }
54
55         static Type GetType (string tname)
56         {
57                 return GetType (tname, false);
58         }
59         
60         static void PrintTypes (string assembly)
61         {
62                 Assembly a = Assembly.Load (assembly);
63                 foreach (Type t in a.GetExportedTypes ())
64                         Console.WriteLine (t.FullName);
65         }
66         
67         static void Main (string [] args)
68         {
69                 if (args.Length < 1) {
70                         Console.WriteLine ("monop [-r:Assembly] [class-name]");
71                         return;
72                 }
73                 
74                 IndentedTextWriter o = new IndentedTextWriter (Console.Out, "    ");
75
76                 int i = 0;
77                 if (args [0].StartsWith ("-r:")){
78                         i++;
79                         assembly = args [0].Substring (3);
80                         
81                         if (args.Length == 1) {
82                                 PrintTypes (assembly);
83                                 return;
84                         }
85                 }
86
87                 if (args.Length < i+1){
88                         Console.WriteLine ("Usage is: monop [-r:Assembly] [class-name]");
89                         return;
90                 }
91
92                 string tname = args [i];
93                 Type t = GetType (tname);
94
95                 if (t == null) {
96                         // Try some very common ones, dont load anything
97                         foreach (string ns in v_common_ns) {
98                                 t = GetType (ns + "." + tname, true);
99                                 if (t != null)
100                                         goto found;
101                         }
102                 }
103
104                 if (assembly != null){
105                         Console.WriteLine ("Did not find type in assembly");
106                 }
107                 
108                 if (t == null) {
109                         foreach (string assm in common_assemblies) {
110                                 try {
111                                         Assembly a = Assembly.Load (assm);
112                                         t = a.GetType (tname, false, true);
113                                         if (t != null)
114                                                 goto found;
115                                         foreach (string ns in common_ns) {
116                                                 t = a.GetType (ns + "." + tname, false, true);
117                                                 if (t != null) {
118                                                         Console.WriteLine ("(using class from {0})", ns);
119                                                         goto found;
120                                                 }
121                                         }
122                                 } catch {
123                                 }
124                         }
125                 }
126                 
127                 if (t == null) {
128                         Console.WriteLine ("Could not find {0}", tname);
129                         return;
130                 }
131         found:
132                 //
133                 // This gets us nice buffering
134                 //
135                 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
136                 new Outline (t, sw).OutlineType ();
137                 sw.Flush ();
138         }
139 }
140