Пример #1
0
        public void Generate(StreamWriter sw, string indent, CodeGenerationOptions opt, bool use_base, ClassGen type)
        {
            string jni_sig             = JniSignature;
            bool   gen_string_overload = Parameters.HasCharSequence && !type.ContainsCtor(jni_sig.Replace("java/lang/CharSequence", "java/lang/String"));

            System.Collections.Specialized.StringCollection call_cleanup = Parameters.GetCallCleanup(opt);
            opt.CodeGenerator.WriteConstructorIdField(this, sw, indent, opt);
            sw.WriteLine("{0}// Metadata.xml XPath constructor reference: path=\"{1}/constructor[@name='{2}'{3}]\"", indent, type.MetadataXPathReference, type.JavaSimpleName, Parameters.GetMethodXPathPredicate());
            sw.WriteLine("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]", indent, ".ctor", jni_sig, String.Empty, this.AdditionalAttributeString());
            GenerateCustomAttributes(sw, indent);
            sw.WriteLine("{0}{1} unsafe {2} ({3})\n{0}\t: {4} (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)",
                         indent, Visibility, Name, GenBase.GetSignature(this, opt), use_base ? "base" : "this");
            sw.WriteLine("{0}{{", indent);
            opt.CodeGenerator.WriteConstructorBody(this, sw, indent + "\t", opt, call_cleanup);
            sw.WriteLine("{0}}}", indent);
            sw.WriteLine();
            if (gen_string_overload)
            {
                sw.WriteLine("{0}[Register (\"{1}\", \"{2}\", \"{3}\"{4})]", indent, ".ctor", jni_sig, String.Empty, this.AdditionalAttributeString());
                sw.WriteLine("{0}{1} unsafe {2} ({3})\n{0}\t: {4} (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)",
                             indent, Visibility, Name, GenBase.GetSignature(this, opt).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string"), use_base ? "base" : "this");
                sw.WriteLine("{0}{{", indent);
                opt.CodeGenerator.WriteConstructorBody(this, sw, indent + "\t", opt, call_cleanup);
                sw.WriteLine("{0}}}", indent);
                sw.WriteLine();
            }
        }
Пример #2
0
 internal abstract void    WriteClassInvokerHandle(ClassGen type, StreamWriter sw, string indent, CodeGenerationOptions opt, string declaringType);
Пример #3
0
        public void GenerateEventsOrPropertiesForListener(StreamWriter sw, string indent, CodeGenerationOptions opt, ClassGen target)
        {
            var methods      = target.Methods.Concat(target.Properties.Where(p => p.Setter != null).Select(p => p.Setter));
            var props        = new HashSet <string> ();
            var refs         = new HashSet <string> ();
            var eventMethods = methods.Where(m => m.IsListenerConnector && m.EventName != String.Empty && m.ListenerType == this).Distinct();

            foreach (var method in eventMethods)
            {
                string name = method.CalculateEventName(target.ContainsName);
                if (String.IsNullOrEmpty(name))
                {
                    Report.Warning(0, Report.WarningInterfaceGen + 1, "empty event name in {0}.{1}.", FullName, method.Name);
                    continue;
                }
                if (opt.GetSafeIdentifier(name) != name)
                {
                    Report.Warning(0, Report.WarningInterfaceGen + 4, "event name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, method.Name);
                    continue;
                }
                var prop = target.Properties.FirstOrDefault(p => p.Setter == method);
                if (prop != null)
                {
                    string setter = "__Set" + prop.Name;
                    props.Add(prop.Name);
                    refs.Add(setter);
                    GenerateEventOrProperty(sw, indent, target, opt, name, setter,
                                            string.Format("__v => {0} = __v", prop.Name),
                                            string.Format("__v => {0} = null", prop.Name));
                }
                else
                {
                    refs.Add(method.Name);
                    string rm = null;
                    string remove;
                    if (method.Name.StartsWith("Set"))
                    {
                        remove = string.Format("__v => {0} (null)", method.Name);
                    }
                    else if (method.Name.StartsWith("Add") &&
                             (rm = "Remove" + method.Name.Substring("Add".Length)) != null &&
                             methods.Where(m => m.Name == rm).Any())
                    {
                        remove = string.Format("__v => {0} (__v)", rm);
                    }
                    else
                    {
                        remove = string.Format("__v => {{throw new NotSupportedException (\"Cannot unregister from {0}.{1}\");}}",
                                               FullName, method.Name);
                    }
                    GenerateEventOrProperty(sw, indent, target, opt, name, method.Name,
                                            method.Name,
                                            remove);
                }
            }

            foreach (var r in refs)
            {
                sw.WriteLine("{0}WeakReference weak_implementor_{1};", indent, r);
            }
            sw.WriteLine();
            sw.WriteLine("{0}{1}Implementor __Create{2}Implementor ()", indent, opt.GetOutputName(FullName), Name);
            sw.WriteLine("{0}{{", indent);
            sw.WriteLine("{0}\treturn new {1}Implementor ({2});", indent, opt.GetOutputName(FullName),
                         NeedsSender ? "this" : "");
            sw.WriteLine("{0}}}", indent);
        }
Пример #4
0
 internal abstract void    WriteClassHandle(ClassGen type, StreamWriter sw, string indent, CodeGenerationOptions opt, bool requireNew);
Пример #5
0
        public static ClassGen CreateClass(TypeDefinition t, CodeGenerationOptions opt)
        {
            var klass = new ClassGen(CreateGenBaseSupport(t, opt))
            {
                IsAbstract = t.IsAbstract,
                IsFinal    = t.IsSealed,
                IsShallow  = opt.UseShallowReferencedTypes,
            };

            foreach (var ifaceImpl in t.Interfaces)
            {
                var iface = ifaceImpl.InterfaceType;
                var def   = ifaceImpl.InterfaceType.Resolve();

                if (def != null && def.IsNotPublic)
                {
                    continue;
                }

                klass.AddImplementedInterface(iface.FullNameCorrected());
            }

            Action populate = () => {
                var implements_charsequence = t.Interfaces.Any(it => it.InterfaceType.FullName == "Java.Lang.CharSequence");

                foreach (var m in t.Methods)
                {
                    if (m.IsPrivate || m.IsAssembly || GetRegisterAttribute(m.CustomAttributes) == null)
                    {
                        continue;
                    }
                    if (implements_charsequence && t.Methods.Any(mm => mm.Name == m.Name + "Formatted"))
                    {
                        continue;
                    }
                    if (m.IsConstructor)
                    {
                        klass.Ctors.Add(CreateCtor(klass, m));
                    }
                    else
                    {
                        klass.AddMethod(CreateMethod(klass, m));
                    }
                }

                foreach (var f in t.Fields)
                {
                    if (!f.IsPrivate && GetRegisterAttribute(f.CustomAttributes) == null)
                    {
                        klass.AddField(CreateField(f));
                    }
                }
            };

            if (klass.IsShallow)
            {
                klass.PopulateAction = populate;
            }
            else
            {
                populate();
            }

            TypeReference nominal_base_type;

            for (nominal_base_type = t.BaseType; nominal_base_type != null && (nominal_base_type.HasGenericParameters || nominal_base_type.IsGenericInstance); nominal_base_type = nominal_base_type.Resolve().BaseType)
            {
                ;                 // iterate up to non-generic type, at worst System.Object.
            }
            klass.BaseType = nominal_base_type?.FullNameCorrected();

            return(klass);
        }