2 // monop -- a semi-clone of javap
5 // Dump all attributes.
8 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // John Luke (john.luke@gmail.com)
11 // (C) 2004 Ben Maurer
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.CodeDom.Compiler;
39 using System.Collections;
40 using System.Diagnostics;
42 using IKVM.Reflection;
45 using Type=IKVM.Reflection.Type;
48 static Universe universe = new Universe(UniverseOptions.EnableFunctionPointers | UniverseOptions.ResolveMissingMembers | UniverseOptions.DisablePseudoCustomAttributeRetrieval);
49 static Assembly mscorlib;
50 static Type obsolete_attribute;
51 static string assembly;
53 // very common namespaces, all in corlib
54 static readonly string [] v_common_ns = {
62 static readonly string [] common_assemblies = {
69 static readonly string [] common_ns = {
80 static Type GetType (string tname, bool ignoreCase)
83 if (assembly != null) {
84 Assembly a = GetAssembly (assembly, true);
85 t = a.GetType (tname, false, ignoreCase);
87 t = mscorlib.GetType (tname, false, ignoreCase);
92 static string SearchTypes (string name, ref Type retval, out int count)
94 StringBuilder sb = new StringBuilder ();
98 string [] assemblies = GetKnownAssemblyNames ();
99 for (int i = 0; i < assemblies.Length; i++) {
100 Console.WriteLine ("Loading {0}", assemblies[i]);
101 Assembly a = GetAssembly (assemblies [i], false);
105 Type [] types = a.GetTypes ();
106 for (int j = 0; j < types.Length; j++) {
108 if (t.IsPublic == false)
111 if (t.Name == name || t.Name.ToLower ().IndexOf (name.ToLower ()) > 0) {
114 sb.Append (t.FullName + " from " + a.Location + "\n");
127 return sb.ToString ();
130 static string [] GetKnownAssemblyNames ()
132 Console.WriteLine (options.PublicDir);
133 if (options.Style == "xios" || options.Style == "xand"){
134 return Directory.GetFiles (options.PublicDir, "*.dll");
137 Process p = new Process ();
138 p.StartInfo.UseShellExecute = false;
139 p.StartInfo.RedirectStandardOutput = true;
140 p.StartInfo.FileName = "gacutil";
141 p.StartInfo.Arguments = "-l";
146 Console.WriteLine ("WARNING: gacutil could not be found.");
147 return new string[0];
151 ArrayList names = new ArrayList ();
152 StreamReader output = p.StandardOutput;
154 while ((s = output.ReadLine ()) != null)
159 int length = names.Count - 1;
160 string [] retval = new string [length];
161 retval [0] = typeof (Object).Assembly.FullName;
162 names.CopyTo (1, retval, 1, length - 1); // skip the first and last line
166 static Assembly GetAssembly (string assembly, bool exit)
170 // if -r:~/foo.dll syntax is used the shell misses it
171 if (assembly.StartsWith ("~/"))
172 assembly = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), assembly.Substring (2));
175 // if it exists try to use LoadFrom
176 if (File.Exists (assembly))
177 a = universe.LoadFile (assembly);
179 a = LoadFromMonoPath (assembly);
181 // ignore exception it gets handled below
184 if (a == null && exit) {
185 Console.WriteLine ("Could not load {0}", MonoP.assembly);
186 Environment.Exit (1);
192 static Assembly LoadFromMonoPath (string assembly)
194 // ; on win32, : everywhere else
195 char sep = (Path.DirectorySeparatorChar == '/' ? ':' : ';');
196 string[] paths = Environment.GetEnvironmentVariable ("MONO_PATH").Split (sep);
197 foreach (string path in paths) {
198 string apath = Path.Combine (path, assembly);
199 if (File.Exists (apath))
200 return universe.LoadFile (apath);
205 static Type GetType (string tname)
207 return GetType (tname, false);
210 static void PrintRefs (string assembly)
212 Assembly a = GetAssembly (assembly, true);
213 foreach (AssemblyName an in a.GetReferencedAssemblies ())
214 Console.WriteLine (an);
217 static void PrintTypes (string assembly, bool show_private, bool filter_obsolete)
219 Assembly a = GetAssembly (assembly, true);
221 Console.WriteLine ();
222 Console.WriteLine ("Assembly Information:");
224 foreach (string ai in a.ToString ().Split (','))
225 Console.WriteLine (ai.Trim ());
227 Console.WriteLine ();
228 Type [] types = show_private ? a.GetTypes () : a.GetExportedTypes ();
229 Array.Sort (types, new TypeSorter ());
231 int obsolete_count = 0;
232 foreach (Type t in types) {
233 if (filter_obsolete && t.IsDefined (obsolete_attribute, false))
236 Console.WriteLine (t.FullName);
239 Console.WriteLine ("\nTotal: {0} types.", types.Length - obsolete_count);
242 internal static void Completion (string prefix)
244 foreach (Type t in mscorlib.GetExportedTypes ()) {
245 if (t.Name.StartsWith (prefix)) {
246 if (Array.IndexOf (v_common_ns, t.Namespace) != -1) {
247 Console.WriteLine (t.Name);
252 if (t.FullName.StartsWith (prefix)) {
253 Console.WriteLine (t.FullName);
257 foreach (string assm in common_assemblies) {
260 Assembly a = GetAssembly (assm, true);
261 foreach (Type t in a.GetExportedTypes ()) {
263 if (t.Name.StartsWith (prefix)) {
264 if (Array.IndexOf (common_ns, t.Namespace) != -1) {
265 Console.WriteLine (t.Name);
270 if (t.FullName.StartsWith (prefix)) {
271 Console.WriteLine (t.FullName);
281 static void ShowAll (string assembly, bool show_private, bool filter_obsolete)
283 Assembly a = GetAssembly (assembly, true);
285 foreach (string ai in a.ToString ().Split (','))
286 Console.WriteLine (ai.Trim ());
288 Console.WriteLine ();
289 Type [] types = show_private ? a.GetTypes () : a.GetExportedTypes ();
290 Array.Sort (types, new TypeSorter ());
292 var sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
293 foreach (Type t in types) {
294 if (filter_obsolete && t.IsDefined (obsolete_attribute, false))
297 new Outline (universe, mscorlib, t, sw, true, show_private, filter_obsolete).OutlineType ();
302 static Options options = new Options ();
304 static void Main (string [] args)
306 if (!options.ProcessArgs (args))
309 if (options.Style == null)
310 mscorlib = universe.LoadFile (typeof (int).Assembly.Location);
312 mscorlib = universe.LoadFile (Path.Combine (options.PublicDir, "mscorlib.dll"));
314 obsolete_attribute = mscorlib.GetType ("System.ObsoleteAttribute");
316 if (options.AssemblyReference != null) {
317 assembly = options.AssemblyReference;
319 if (options.ShowAll){
320 ShowAll (assembly, options.ShowPrivate, options.FilterObsolete);
323 if (options.Type == null) {
324 if (options.PrintRefs)
325 PrintRefs (assembly);
327 PrintTypes (assembly, options.ShowPrivate, options.FilterObsolete);
333 string message = null;
334 string tname = options.Type;
339 if (options.Search) {
340 string matches = SearchTypes (tname, ref t, out count);
349 Console.WriteLine ("Found " + count + " types that match:");
350 Console.WriteLine (matches);
358 // Try some very common ones, dont load anything
359 foreach (string ns in v_common_ns) {
360 t = GetType (ns + "." + tname, true);
367 foreach (string assm in GetKnownAssemblyNames ()) {
369 Assembly a = GetAssembly (assm, false);
372 t = a.GetType (tname, false, true);
374 message = String.Format ("{0} is included in the {1} assembly.",
376 t.Assembly.GetName ().Name);
379 foreach (string ns in common_ns) {
380 t = a.GetType (ns + "." + tname, false, true);
382 message = String.Format ("{0} is included in the {1} assembly.",
384 t.Assembly.GetName ().Name);
388 } catch (Exception e){
389 Console.WriteLine ("Failure: " + e);
396 Console.WriteLine ("Could not find {0}", tname);
401 // This gets us nice buffering
403 StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
404 new Outline (universe, mscorlib, t, sw, options.DeclaredOnly, options.ShowPrivate, options.FilterObsolete).OutlineType ();
408 Console.WriteLine (message);