2004-03-14 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
16 class MonoP {
17         static void Main (string [] args)
18         {
19                 if (args.Length != 1) {
20                         Console.WriteLine ("monop <class name>");
21                         return;
22                 }
23                 
24                 IndentedTextWriter o = new IndentedTextWriter (Console.Out, "    ");
25                 
26                 string tname = args [0];
27                 Type t = Type.GetType (tname);
28                 
29                 o.Write ("public class {0}", t.Name);
30                 
31                 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
32                 Type parent = t.BaseType;
33                 
34                 if ((parent != null && parent != typeof (object))|| interfaces.Length != 0) {
35                         bool first = true;
36                         o.Write (" : ");
37                         
38                         if (parent != null && parent != typeof (object)) {
39                                 o.Write (PName (parent));
40                                 first = false;
41                         }
42                         
43                         foreach (Type intf in interfaces) {
44                                 if (!first) o.Write (", ");
45                                 first = false;
46                                 
47                                 o.Write (PName (intf));
48                         }
49                 }
50                 
51                 o.WriteLine (" {"); o.Indent++;
52                 
53                 foreach (ConstructorInfo ci in t.GetConstructors ())
54                         o.WriteLine ("{0} ({1});", t.Name, PPParams (ci.GetParameters ()));
55                 
56                 o.WriteLine ();
57                 
58                 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
59                         if ((m.Attributes & MethodAttributes.SpecialName) != 0)
60                                 continue;
61                         
62                         o.WriteLine (PPMethod (m));
63                 }
64                 
65                 o.WriteLine ();
66                 
67                 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
68                         ParameterInfo [] idxp = pi.GetIndexParameters ();
69                         
70                         if ((pi.CanRead ? pi.GetGetMethod () : pi.GetSetMethod ()).IsStatic)
71                                 o.Write ("static ");
72                         
73                         o.Write (PName (pi.PropertyType));
74                         o.Write (" ");
75                         
76                         if (idxp.Length == 0)
77                                 o.Write (pi.Name);
78                         else
79                                 o.Write ("this [{0}]", PPParams (idxp));
80                         
81                         o.WriteLine (" {");
82                         o.Indent ++;
83                         
84                         if (pi.CanRead) o.WriteLine ("get;");
85                         if (pi.CanWrite) o.WriteLine ("set;");
86                         
87                         o.Indent --;
88                         o.WriteLine ("}");
89                 }
90                 
91                 
92                 o.Indent--; o.WriteLine ("}");
93         }
94         
95         public static string PPParams (ParameterInfo [] p) {
96         
97                 string parms = "";
98                 for (int i = 0; i < p.Length; ++i) {
99                         if (i > 0)
100                                 parms = parms + ", ";
101                                 
102                         parms += PName (p[i].ParameterType) + " " + p [i].Name;
103                 }
104                 return parms;
105         }
106         
107         public static string PPMethod (MethodInfo mi) {
108                 
109                 return (mi.IsStatic ? "static " : "") + PName (mi.ReturnType) + " " + mi.Name + " (" + PPParams (mi.GetParameters ()) + ");";
110         }
111
112         
113         public static string PName (Type t)
114         {
115                 string type = t.FullName;
116                 if (!type.StartsWith ("System."))
117                         return type;
118                 
119                 if (t.HasElementType) {
120                         Type et = t.GetElementType ();
121                         if (t.IsArray)
122                                 return PName (et) + " []";
123                         if (t.IsPointer)
124                                 return PName (et) + " *";
125                         if (t.IsByRef)
126                                 return "ref " + PName (et);
127                 }
128
129                 switch (type) {
130                 case "System.Byte": return "byte";
131                 case "System.SByte": return "sbyte";
132                 case "System.Int16": return "short";
133                 case "System.Int32": return "int";
134                 case "System.Int64": return "long";
135                         
136                 case "System.UInt16": return "ushort";
137                 case "System.UInt32": return "uint";
138                 case "System.UInt64": return "ulong";
139                         
140                 case "System.Single":  return "float";
141                 case "System.Double":  return "double";
142                 case "System.Decimal": return "decimal";
143                 case "System.Boolean": return "bool";
144                 case "System.Char":    return "char";
145                 case "System.String":  return "string";
146                         
147                 case "System.Object":  return "object";
148                 case "System.Void":  return "void";
149                 }
150
151                 if (type.LastIndexOf(".") == 6)
152                         return type.Substring(7);
153                 
154                 return type;
155         }
156 }
157
158 public delegate int ComparerFunc (object a, object b);
159         
160 public class Comparer : IComparer  {
161         ComparerFunc cmp;
162         
163         public Comparer (ComparerFunc f)
164         {
165                 this.cmp = f;
166         }
167         
168         public int Compare (object a, object b)
169         {
170                 return cmp (a, b);
171         }
172         
173         static int CompareMemberInfo (object a, object b)
174         {
175                 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
176         }
177         
178         static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
179         
180         public static MemberInfo [] Sort (MemberInfo [] inf)
181         {
182                 Array.Sort (inf, MemberInfoComparer);
183                 return inf;
184         }
185         
186         static int CompareMethodBase (object a, object b)
187         {
188                 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
189                 
190                 if (aa.IsStatic == bb.IsStatic)
191                         return CompareMemberInfo (a, b);
192                 
193                 if (aa.IsStatic)
194                         return -1;
195                 
196                 return 1;
197         }
198         
199         static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
200         
201         public static MethodBase [] Sort (MethodBase [] inf)
202         {
203                 Array.Sort (inf, MethodBaseComparer);
204                 return inf;
205         }
206         
207         static int ComparePropertyInfo (object a, object b)
208         {
209                 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
210                 
211                 bool astatic = (aa.CanRead ? aa.GetGetMethod () : aa.GetSetMethod ()).IsStatic;
212                 bool bstatic = (bb.CanRead ? bb.GetGetMethod () : bb.GetSetMethod ()).IsStatic;
213                 
214                 if (astatic == bstatic)
215                         return CompareMemberInfo (a, b);
216                 
217                 if (astatic)
218                         return -1;
219                 
220                 return 1;
221         }
222         
223         static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
224         
225         public static PropertyInfo [] Sort (PropertyInfo [] inf)
226         {
227                 Array.Sort (inf, PropertyInfoComparer);
228                 return inf;
229         }
230 }