2 // monop -- a semi-clone of javap
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // John Luke (john.luke@gmail.com)
14 using System.CodeDom.Compiler;
15 using System.Collections;
16 using System.Diagnostics;
18 using System.Reflection;
21 static string assembly;
22 static BindingFlags default_flags =
23 BindingFlags.Instance |
27 // very common namespaces, all in corlib
28 static readonly string [] v_common_ns = {
36 static readonly string [] common_assemblies = {
43 static readonly string [] common_ns = {
50 static Type GetType (string tname, bool ignoreCase)
53 if (assembly != null) {
54 Assembly a = GetAssembly (assembly, true);
55 t = a.GetType (tname, false, ignoreCase);
57 t = Type.GetType (tname, false, ignoreCase);
63 static string [] GetAssemblyNamesFromGAC ()
65 Process p = new Process ();
66 p.StartInfo.UseShellExecute = false;
67 p.StartInfo.RedirectStandardOutput = true;
68 p.StartInfo.FileName = "gacutil";
69 p.StartInfo.Arguments = "-l";
73 ArrayList names = new ArrayList ();
74 StreamReader output = p.StandardOutput;
76 while ((s = output.ReadLine ()) != null)
81 string [] retval = new string [names.Count - 2];
82 names.CopyTo (1, retval, 0, retval.Length); // skip the first and last line
86 static Assembly GetAssembly (string assembly, bool exit)
91 // if it exists try to use LoadFrom
92 if (File.Exists (assembly))
93 a = Assembly.LoadFrom (assembly);
94 // if it looks like a fullname try that
95 else if (assembly.Split (',').Length == 4)
96 a = Assembly.Load (assembly);
97 // see if MONO_PATH has it
99 a = LoadFromMonoPath (assembly);
101 // ignore exception it gets handled below
104 // last try partial name
105 // this (apparently) is exception safe
107 a = Assembly.LoadWithPartialName (assembly);
109 if (a == null && exit) {
110 Console.WriteLine ("Could not load {0}", MonoP.assembly);
111 Environment.Exit (1);
117 static Assembly LoadFromMonoPath (string assembly)
119 // ; on win32, : everywhere else
120 char sep = (Path.DirectorySeparatorChar == '/' ? ':' : ';');
121 string[] paths = Environment.GetEnvironmentVariable ("MONO_PATH").Split (sep);
122 foreach (string path in paths)
124 string apath = Path.Combine (path, assembly);
125 if (File.Exists (apath))
126 return Assembly.LoadFrom (apath);
131 static Type GetType (string tname)
133 return GetType (tname, false);
136 static void PrintTypes (string assembly)
138 Assembly a = GetAssembly (assembly, true);
140 Console.WriteLine ();
141 Console.WriteLine ("Assembly Information:");
143 object[] cls = a.GetCustomAttributes (typeof (CLSCompliantAttribute), false);
146 CLSCompliantAttribute cca = cls[0] as CLSCompliantAttribute;
148 Console.WriteLine ("[CLSCompliant]");
151 foreach (string ai in a.ToString ().Split (','))
152 Console.WriteLine (ai.Trim ());
154 Console.WriteLine ();
155 Type [] types = a.GetExportedTypes ();
157 foreach (Type t in types)
158 Console.WriteLine (t.FullName);
160 Console.WriteLine ("\nTotal: {0} types.", types.Length);
163 static void Completion (string prefix)
165 foreach (Type t in typeof (object).Assembly.GetExportedTypes ()) {
166 if (t.Name.StartsWith (prefix)) {
167 if (Array.IndexOf (v_common_ns, t.Namespace) != -1) {
168 Console.WriteLine (t.Name);
173 if (t.FullName.StartsWith (prefix)) {
174 Console.WriteLine (t.FullName);
178 foreach (string assm in common_assemblies) {
181 Assembly a = GetAssembly (assm, true);
182 foreach (Type t in a.GetExportedTypes ()) {
184 if (t.Name.StartsWith (prefix)) {
185 if (Array.IndexOf (common_ns, t.Namespace) != -1) {
186 Console.WriteLine (t.Name);
191 if (t.FullName.StartsWith (prefix)) {
192 Console.WriteLine (t.FullName);
202 static void PrintUsage ()
204 Console.WriteLine ("Usage is: monop [option] [-c] [-r:Assembly] [class-name]");
207 static void PrintHelp ()
210 Console.WriteLine ("");
211 Console.WriteLine ("options:");
212 Console.WriteLine ("\t--declared-only,-d\tOnly show members declared in the Type");
213 Console.WriteLine ("\t--help,-h\t\tShow this information");
214 Console.WriteLine ("\t--private,-p\t\tShow private members");
217 static void Main (string [] args)
219 if (args.Length < 1) {
224 if (args.Length == 1 && (args[0] == "--help" || args[0] == "-h"))
232 if (args [0].StartsWith ("-r:") || args [0].StartsWith ("/r:")){
234 assembly = args [0].Substring (3);
236 if (args.Length == 1) {
237 PrintTypes (assembly);
242 if (args [0] == "-c") {
243 Completion (args [1]);
247 if (args [i] == "--private" || args [i] == "-p") {
248 default_flags |= BindingFlags.NonPublic;
252 if (args [i] == "--declared-only" || args [i] == "-d") {
253 default_flags |= BindingFlags.DeclaredOnly;
257 if (args.Length < i + 1) {
262 string message = null;
263 string tname = args [i];
264 Type t = GetType (tname);
267 // Try some very common ones, dont load anything
268 foreach (string ns in v_common_ns) {
269 t = GetType (ns + "." + tname, true);
276 foreach (string assm in GetAssemblyNamesFromGAC ()) {
278 Assembly a = GetAssembly (assm, false);
279 t = a.GetType (tname, false, true);
281 message = String.Format ("{0} is included in the {1} assembly.",
283 t.Assembly.GetName ().Name);
286 foreach (string ns in common_ns) {
287 t = a.GetType (ns + "." + tname, false, true);
289 message = String.Format ("{0} is included in the {1} assembly.",
291 t.Assembly.GetName ().Name);
301 Console.WriteLine ("Could not find {0}", tname);
306 // This gets us nice buffering
308 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
309 new Outline (t, sw).OutlineType (default_flags);
313 Console.WriteLine (message);