buffer
[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 Main (string [] args)
61         {
62                 if (args.Length < 1) {
63                         Console.WriteLine ("monop [-r:Assembly] class-name");
64                         return;
65                 }
66                 
67                 IndentedTextWriter o = new IndentedTextWriter (Console.Out, "    ");
68
69                 int i = 0;
70                 if (args [0].StartsWith ("-r:")){
71                         i++;
72                         assembly = args [0].Substring (3);
73                 }
74
75                 if (args.Length < i+1){
76                         Console.WriteLine ("Usage is: monop [-r:Assembly] class-name");
77                         return;
78                 }
79
80                 string tname = args [i];
81                 Type t = GetType (tname);
82
83                 if (t == null) {
84                         // Try some very common ones, dont load anything
85                         foreach (string ns in v_common_ns) {
86                                 t = GetType (ns + "." + tname, true);
87                                 if (t != null)
88                                         goto found;
89                         }
90                 }
91
92                 if (assembly != null){
93                         Console.WriteLine ("Did not find type in assembly");
94                 }
95                 
96                 if (t == null) {
97                         foreach (string assm in common_assemblies) {
98                                 try {
99                                         Assembly a = Assembly.Load (assm);
100                                         t = a.GetType (tname, false, true);
101                                         if (t != null)
102                                                 goto found;
103                                         foreach (string ns in common_ns) {
104                                                 t = a.GetType (ns + "." + tname, false, true);
105                                                 if (t != null) {
106                                                         Console.WriteLine ("(using class from {0})", ns);
107                                                         goto found;
108                                                 }
109                                         }
110                                 } catch {
111                                 }
112                         }
113                 }
114                 
115                 if (t == null) {
116                         Console.WriteLine ("Could not find {0}", tname);
117                         return;
118                 }
119         found:
120                 //
121                 // This gets us nice buffering
122                 //
123                 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
124                 new Outline (t, sw).OutlineType ();
125                 sw.Flush ();
126         }
127 }
128