void Generate(ref TypeBuilder tBuilder, Method m) { if (m.IsPrivate) { return; } MethodAttributes attr = m.MethodAttributes; Descriptor[] args = m.Arguments; Type[] parameterTypes = new Type[args.Length]; for (int i = 0; i < args.Length; i++) { Type t = GetType(args[i]); if (t == null) { // the type is not in the cache // so we can skip the whole method Console.WriteLine("WARNING: skipped method {0} in {1}", m, tBuilder); return; } parameterTypes[i] = t; } Type returnType = GetType(m.Return); if (returnType == null) { // the type is not in the cache // so we can skip the whole method Console.WriteLine("WARNING: skipped method {0} in {1}", m, tBuilder); return; } string mName = m.Name; if (mName.Equals("<init>") || mName.Equals("<clinit>")) { // TODO emit constructor return; } MethodBuilder mBuilder = tBuilder.DefineMethod( mName, attr, returnType, parameterTypes); // no implementation for interfaces if (tBuilder.IsInterface) { return; } // at least generate a return (to make method valid) ILGenerator methodIL = mBuilder.GetILGenerator(); // TODO emit real method code methodIL.Emit(OpCodes.Ret); }
public void BuildClass(Class c, ClassFactory factory) { // accessFlags c.AccessFlags = (Class.AccessFlag) accessFlags; // thisClass c.InternalName = GetClassName(thisClass); // superClass c.AddBaseType(factory.LoadClass(GetClassName(superClass))); // interfaces foreach (ushort iface in interfaces) { c.AddBaseType(factory.LoadClass(GetClassName(iface))); } // fields foreach (FieldInfo info in fields) { Field f = new Field(c); f.AccessFlags = (Field.AccessFlag) info.AccessFlags; f.Name = GetUtf8(info.NameIndex); f.Signature = GetUtf8(info.DescriptorIndex); f.Descriptor = ProcessDescriptor(f.Signature, factory); c.AddField(f); } // methods foreach (MethodInfo info in methods) { Method m = new Method(c); m.AccessFlags = (Method.AccessFlag) info.AccessFlags; m.Name = GetUtf8(info.NameIndex); m.Signature = GetUtf8(info.DescriptorIndex); ProcessMethodDescriptorString(m, m.Signature, factory); c.AddMethod(m); } }
void ProcessMethodDescriptorString(Method m, string value, ClassFactory factory) { string args = value.Substring(1, value.IndexOf(')') - 1); int index = 0; while (index < args.Length) { string arg = args.Substring(index); Descriptor d = ProcessDescriptor (arg, factory); if (d.IsBasicType) { index++; } else if (d.IsObjectType) { index += d.Class.InternalName.Length + 2; // Ljava/util/List; } else { index++; // [ Descriptor componentType = d; while ((componentType = componentType.Component).IsArrayType) { index++; // [ } if (componentType.IsBasicType) { index++; // [B } else { index += componentType.Class.InternalName.Length + 2; // Ljava/util/List; } } m.AddArgument(d); } m.Return = ProcessDescriptor(value.Substring(value.IndexOf(')') + 1), factory); }
public void AddMethod(Method m) { if (methods.Contains(m)){ throw new ApplicationException("Duplicate Method: " + m); } methods.Add(m); }