2 // monop -- a semi-clone of javap
5 // Ben Maurer (bmaurer@users.sourceforge.net)
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
17 static void Main (string [] args)
19 if (args.Length != 1) {
20 Console.WriteLine ("monop <class name>");
24 IndentedTextWriter o = new IndentedTextWriter (Console.Out, " ");
26 string tname = args [0];
27 Type t = Type.GetType (tname);
29 o.Write ("public class {0}", t.Name);
31 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
32 Type parent = t.BaseType;
34 if ((parent != null && parent != typeof (object))|| interfaces.Length != 0) {
38 if (parent != null && parent != typeof (object)) {
39 o.Write (PName (parent));
43 foreach (Type intf in interfaces) {
44 if (!first) o.Write (", ");
47 o.Write (PName (intf));
51 o.WriteLine (" {"); o.Indent++;
53 foreach (ConstructorInfo ci in t.GetConstructors ())
54 o.WriteLine ("{0} ({1});", t.Name, PPParams (ci.GetParameters ()));
58 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
59 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
62 o.WriteLine (PPMethod (m));
67 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
68 ParameterInfo [] idxp = pi.GetIndexParameters ();
70 if ((pi.CanRead ? pi.GetGetMethod () : pi.GetSetMethod ()).IsStatic)
73 o.Write (PName (pi.PropertyType));
79 o.Write ("this [{0}]", PPParams (idxp));
84 if (pi.CanRead) o.WriteLine ("get;");
85 if (pi.CanWrite) o.WriteLine ("set;");
92 o.Indent--; o.WriteLine ("}");
95 public static string PPParams (ParameterInfo [] p) {
98 for (int i = 0; i < p.Length; ++i) {
100 parms = parms + ", ";
102 parms += PName (p[i].ParameterType) + " " + p [i].Name;
107 public static string PPMethod (MethodInfo mi) {
109 return (mi.IsStatic ? "static " : "") + PName (mi.ReturnType) + " " + mi.Name + " (" + PPParams (mi.GetParameters ()) + ");";
113 public static string PName (Type t)
115 string type = t.FullName;
116 if (!type.StartsWith ("System."))
119 if (t.HasElementType) {
120 Type et = t.GetElementType ();
122 return PName (et) + " []";
124 return PName (et) + " *";
126 return "ref " + PName (et);
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";
136 case "System.UInt16": return "ushort";
137 case "System.UInt32": return "uint";
138 case "System.UInt64": return "ulong";
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";
147 case "System.Object": return "object";
148 case "System.Void": return "void";
151 if (type.LastIndexOf(".") == 6)
152 return type.Substring(7);
158 public delegate int ComparerFunc (object a, object b);
160 public class Comparer : IComparer {
163 public Comparer (ComparerFunc f)
168 public int Compare (object a, object b)
173 static int CompareMemberInfo (object a, object b)
175 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
178 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
180 public static MemberInfo [] Sort (MemberInfo [] inf)
182 Array.Sort (inf, MemberInfoComparer);
186 static int CompareMethodBase (object a, object b)
188 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
190 if (aa.IsStatic == bb.IsStatic)
191 return CompareMemberInfo (a, b);
199 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
201 public static MethodBase [] Sort (MethodBase [] inf)
203 Array.Sort (inf, MethodBaseComparer);
207 static int ComparePropertyInfo (object a, object b)
209 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
211 bool astatic = (aa.CanRead ? aa.GetGetMethod () : aa.GetSetMethod ()).IsStatic;
212 bool bstatic = (bb.CanRead ? bb.GetGetMethod () : bb.GetSetMethod ()).IsStatic;
214 if (astatic == bstatic)
215 return CompareMemberInfo (a, b);
223 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
225 public static PropertyInfo [] Sort (PropertyInfo [] inf)
227 Array.Sort (inf, PropertyInfoComparer);