This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         static BindingFlags default_flags = 
20                 BindingFlags.Instance |
21                 BindingFlags.Static |
22                 BindingFlags.Public;
23         
24         // very common namespaces, all in corlib
25         static readonly string [] v_common_ns = {
26                 "System",
27                 "System.Collections",
28                 "System.Reflection",
29                 "System.Text",
30                 "System.IO"
31         };
32         
33         static readonly string [] common_assemblies = {
34                 "System.Xml.dll",
35                 "System.Web.dll",
36                 "gtk-sharp.dll",
37                 "glib-sharp.dll"
38         };
39         
40         static readonly string [] common_ns = {
41                 "System.Xml",
42                 "System.Web",
43                 "Gtk",
44                 "GLib"
45         };
46         
47         static Type GetType (string tname, bool ignoreCase)
48         {
49                 Type t;
50                 if (assembly != null) {
51                         Assembly a = GetAssembly (assembly, true);
52                         t = a.GetType (tname, false, ignoreCase);
53                 } else {
54                         t = Type.GetType (tname, false, ignoreCase);
55                 }
56
57                 return t;
58         }
59
60         static Assembly GetAssembly (string assembly, bool exit)
61         {
62                 Assembly a;
63
64                 try {
65                         // if is starts with / use the full path
66                         // otherwise try to load from the GAC
67                         if (assembly.StartsWith ("/"))
68                                 a = Assembly.LoadFrom (assembly);
69                         else
70                                 a = Assembly.LoadWithPartialName (assembly);
71
72                         // if the above failed try Load
73                         if (a == null)
74                                 a = Assembly.Load (assembly);
75
76                         return a;
77                 }
78                 catch {
79                         if (exit) {
80                                 Console.WriteLine ("Could not load {0}", MonoP.assembly);
81                                 Environment.Exit (1);
82                         }
83                         return null;
84                 }
85         }
86
87         static Type GetType (string tname)
88         {
89                 return GetType (tname, false);
90         }
91         
92         static void PrintTypes (string assembly)
93         {
94                 Assembly a = GetAssembly (assembly, true);
95                 Type [] types = a.GetExportedTypes ();
96
97                 foreach (Type t in types)
98                         Console.WriteLine (t.FullName);
99
100                 Console.WriteLine ("\nTotal: {0} types.", types.Length);
101         }
102         
103         static void Completion (string prefix)
104         {
105                 foreach (Type t in typeof (object).Assembly.GetExportedTypes ()) {
106                         if (t.Name.StartsWith (prefix)) {
107                                 if (Array.IndexOf (v_common_ns, t.Namespace) != -1) {
108                                         Console.WriteLine (t.Name);
109                                         return;
110                                 }
111                         }
112                         
113                         if (t.FullName.StartsWith (prefix)) {
114                                 Console.WriteLine (t.FullName);
115                         }
116                 }
117                 
118                 foreach (string assm in common_assemblies) {
119                         try {
120                                 
121                                 Assembly a = GetAssembly (assm, true);
122                                 foreach (Type t in a.GetExportedTypes ()) {
123                                         
124                                         if (t.Name.StartsWith (prefix)) {
125                                                 if (Array.IndexOf (common_ns, t.Namespace) != -1) {
126                                                         Console.WriteLine (t.Name);
127                                                         return;
128                                                 }
129                                         }
130                                         
131                                         if (t.FullName.StartsWith (prefix)) {
132                                                 Console.WriteLine (t.FullName);
133                                         }
134                                 }
135                                 
136                         } catch {
137                         }
138                 }
139                 
140         }
141         
142         static void Main (string [] args)
143         {
144                 if (args.Length < 1) {
145                         Console.WriteLine ("monop [-c] [-r:Assembly] [class-name]");
146                         return;
147                 }
148                 
149                 IndentedTextWriter o = new IndentedTextWriter (Console.Out, "    ");
150
151                 int i = 0;
152                 if (args [0].StartsWith ("-r:") || args [0].StartsWith ("/r:")){
153                         i++;
154                         assembly = args [0].Substring (3);
155                         
156                         if (args.Length == 1) {
157                                 PrintTypes (assembly);
158                                 return;
159                         }
160                 }
161                 
162                 if (args [0] == "-c") {
163                         Completion (args [1]);
164                         return;
165                 }
166
167                 if (args [i] == "--private" || args [i] == "-p") {
168                                 default_flags |= BindingFlags.NonPublic;
169                                 i++;
170                 }
171
172                 if (args.Length < i+1){
173                         Console.WriteLine ("Usage is: monop [-r:Assembly] [class-name]");
174                         return;
175                 }
176
177                 string tname = args [i];
178                 Type t = GetType (tname);
179
180                 if (t == null) {
181                         // Try some very common ones, dont load anything
182                         foreach (string ns in v_common_ns) {
183                                 t = GetType (ns + "." + tname, true);
184                                 if (t != null)
185                                         goto found;
186                         }
187                 }
188
189                 if (t == null) {
190                         foreach (string assm in common_assemblies) {
191                                 try {
192                                         Assembly a = GetAssembly (assm, false);
193                                         t = a.GetType (tname, false, true);
194                                         if (t != null)
195                                                 goto found;
196                                         foreach (string ns in common_ns) {
197                                                 t = a.GetType (ns + "." + tname, false, true);
198                                                 if (t != null) {
199                                                         Console.WriteLine ("(using class from {0})", ns);
200                                                         goto found;
201                                                 }
202                                         }
203                                 } catch {
204                                 }
205                         }
206                 }
207                 
208                 if (t == null) {
209                         Console.WriteLine ("Could not find {0}", tname);
210                         return;
211                 }
212         found:
213                 //
214                 // This gets us nice buffering
215                 //
216                 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
217                 new Outline (t, sw).OutlineType (default_flags);
218                 sw.Flush ();
219         }
220 }
221