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