Example #1
0
        void AddProperties(Method method, CodeGenerationOptions opt)
        {
            foreach (var p in method.Parameters)
            {
                if (p.IsSender)
                {
                    continue;
                }

                // We've already added this property from a different overload
                if (Properties.Any(prop => prop.Name == p.PropertyName))
                {
                    continue;
                }

                Fields.Add(new FieldWriter {
                    Name = opt.GetSafeIdentifier(p.Name),
                    Type = new TypeReferenceWriter(opt.GetTypeReferenceName(p))
                });

                var prop = new PropertyWriter {
                    Name         = p.PropertyName,
                    PropertyType = new TypeReferenceWriter(opt.GetTypeReferenceName(p)),
                    IsPublic     = true,
                    HasGet       = true
                };

                prop.GetBody.Add($"return {opt.GetSafeIdentifier (p.Name)};");

                Properties.Add(prop);
            }
        }
Example #2
0
        public MethodExtensionAsyncWrapper(Method method, CodeGenerationOptions opt, string selfType)
        {
            Name     = method.AdjustedName + "Async";
            IsStatic = true;

            SetVisibility(method.Visibility);

            ReturnType = new TypeReferenceWriter("global::System.Threading.Tasks.Task");

            if (!method.IsVoid)
            {
                ReturnType.Name += "<" + opt.GetTypeReferenceName(method.RetVal) + ">";
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            Body.Add($"return global::System.Threading.Tasks.Task.Run (() => self.{method.AdjustedName} ({method.Parameters.GetCall (opt)}));");

            Parameters.Add(new MethodParameterWriter("self", new TypeReferenceWriter(selfType))
            {
                IsExtension = true
            });

            this.AddMethodParameters(method.Parameters, opt);
        }
        public InterfaceInvokerProperty(InterfaceGen iface, Property property, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            this.property = property;
            this.opt      = opt;

            Name         = property.AdjustedName;
            PropertyType = new TypeReferenceWriter(opt.GetTypeReferenceName(property));

            IsPublic = true;
            IsUnsafe = true;

            HasGet = property.Getter != null;

            if (property.Getter != null)
            {
                HasGet          = true;
                getter_callback = new MethodCallback(iface, property.Getter, opt, property.AdjustedName, false);
            }

            if (property.Setter != null)
            {
                HasSet          = true;
                setter_callback = new MethodCallback(iface, property.Setter, opt, property.AdjustedName, false);
            }

            context_this = context.ContextType.GetObjectHandleProperty("this");
        }
        public BoundMethodExtensionStringOverload(Method method, CodeGenerationOptions opt, string selfType)
        {
            this.method = method;
            this.opt    = opt;
            self_type   = selfType;

            Name     = method.Name;
            IsStatic = true;

            SetVisibility(method.Visibility);
            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string"));

            if (method.Deprecated != null)
            {
                Attributes.Add(new ObsoleteAttr(method.Deprecated.Replace("\"", "\"\"").Trim()));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            Parameters.Add(new MethodParameterWriter("self", new TypeReferenceWriter(selfType))
            {
                IsExtension = true
            });
            this.AddMethodParametersStringOverloads(method.Parameters, opt);
        }
Example #5
0
        public BoundInterfaceMethodDeclaration(Method method, string adapter, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name          = method.AdjustedName;
            ReturnType    = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));
            IsDeclaration = true;

            if (method.DeclaringType.IsGeneratable)
            {
                Comments.Add($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
            }
            if (method.Deprecated != null)
            {
                Attributes.Add(new ObsoleteAttr(method.Deprecated.Replace("\"", "\"\"")));
            }
            if (method.IsReturnEnumified)
            {
                Attributes.Add(new GeneratedEnumAttr(true));
            }
            if (method.IsInterfaceDefaultMethod)
            {
                Attributes.Add(new CustomAttr("[global::Java.Interop.JavaInterfaceDefaultMethod]"));
            }

            Attributes.Add(new RegisterAttr(method.JavaName, method.JniSignature, method.ConnectorName + ":" + method.GetAdapterName(opt, adapter), additionalProperties: method.AdditionalAttributeString()));

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);
            this.AddMethodParameters(method.Parameters, opt);
        }
        public BoundAbstractProperty(GenBase gen, Property property, CodeGenerationOptions opt)
        {
            Name         = property.AdjustedName;
            PropertyType = new TypeReferenceWriter(opt.GetTypeReferenceName(property.Getter.RetVal));

            SetVisibility(property.Getter.RetVal.IsGeneric ? "protected" : property.Getter.Visibility);

            IsAbstract = true;
            HasGet     = true;

            var baseProp = gen.BaseSymbol != null?gen.BaseSymbol.GetPropertyByName(property.Name, true) : null;

            if (baseProp != null)
            {
                IsOverride = true;
            }
            else
            {
                IsShadow = gen.RequiresNew(property);

                getter_callback = new MethodCallback(gen, property.Getter, opt, property.AdjustedName, false);

                if (property.Setter != null)
                {
                    setter_callback = new MethodCallback(gen, property.Setter, opt, property.AdjustedName, false);
                }
            }

            if (gen.IsGeneratable)
            {
                GetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Getter.JavaName}'{property.Getter.Parameters.GetMethodXPathPredicate ()}]\"");
            }
            if (property.Getter.IsReturnEnumified)
            {
                GetterAttributes.Add(new GeneratedEnumAttr(true));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(GetterAttributes, property.Getter, opt);

            GetterAttributes.Add(new RegisterAttr(property.Getter.JavaName, property.Getter.JniSignature, property.Getter.GetConnectorNameFull(opt), additionalProperties: property.Getter.AdditionalAttributeString()));

            SourceWriterExtensions.AddMethodCustomAttributes(GetterAttributes, property.Getter);

            if (property.Setter != null)
            {
                HasSet = true;

                if (gen.IsGeneratable)
                {
                    SetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Setter.JavaName}'{property.Setter.Parameters.GetMethodXPathPredicate ()}]\"");
                }

                SourceWriterExtensions.AddSupportedOSPlatform(SetterAttributes, property.Setter, opt);

                SourceWriterExtensions.AddMethodCustomAttributes(SetterAttributes, property.Setter);
                SetterAttributes.Add(new RegisterAttr(property.Setter.JavaName, property.Setter.JniSignature, property.Setter.GetConnectorNameFull(opt), additionalProperties: property.Setter.AdditionalAttributeString()));
            }
        }
        public void GetTypeReferenceName_Nullable()
        {
            var opt = new CodeGenerationOptions {
                SupportNullableReferenceTypes = true
            };

            var system_void = new ReturnValue(null, "void", "System.Void", false, false);

            system_void.Validate(opt, null, null);

            Assert.AreEqual("void", opt.GetTypeReferenceName(system_void));

            var primitive_void = new ReturnValue(null, "void", "void", false, false);

            primitive_void.Validate(opt, null, null);

            Assert.AreEqual("void", opt.GetTypeReferenceName(primitive_void));
        }
        public GenericExplicitInterfaceImplementationProperty(Property property, GenericSymbol gen, string adapter, Dictionary <string, string> mappings, CodeGenerationOptions opt)
        {
            Name = property.AdjustedName;

            PropertyType = new TypeReferenceWriter(opt.GetTypeReferenceName(property));
            ExplicitInterfaceImplementation = opt.GetOutputName(gen.Gen.FullName);

            Comments.Add($"// This method is explicitly implemented as a member of an instantiated {gen.FullName}");

            if (property.Getter != null)
            {
                HasGet = true;

                if (gen.Gen.IsGeneratable)
                {
                    GetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.Gen.MetadataXPathReference}/method[@name='{property.Getter.JavaName}'{property.Getter.Parameters.GetMethodXPathPredicate ()}]\"");
                }
                if (property.Getter.GenericArguments != null && property.Getter.GenericArguments.Any())
                {
                    GetterAttributes.Add(new CustomAttr(property.Getter.GenericArguments.ToGeneratedAttributeString()));
                }

                SourceWriterExtensions.AddSupportedOSPlatform(GetterAttributes, property.Getter, opt);

                GetterAttributes.Add(new RegisterAttr(property.Getter.JavaName, property.Getter.JniSignature, property.Getter.ConnectorName + ":" + property.Getter.GetAdapterName(opt, adapter), additionalProperties: property.Getter.AdditionalAttributeString()));

                GetBody.Add($"return {property.Name};");
            }

            if (property.Setter != null)
            {
                HasSet = true;

                if (gen.Gen.IsGeneratable)
                {
                    SetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.Gen.MetadataXPathReference}/method[@name='{property.Setter.JavaName}'{property.Setter.Parameters.GetMethodXPathPredicate ()}]\"");
                }
                if (property.Setter.GenericArguments != null && property.Setter.GenericArguments.Any())
                {
                    SetterAttributes.Add(new CustomAttr(property.Setter.GenericArguments.ToGeneratedAttributeString()));
                }

                SourceWriterExtensions.AddSupportedOSPlatform(SetterAttributes, property.Setter, opt);

                SetterAttributes.Add(new RegisterAttr(property.Setter.JavaName, property.Setter.JniSignature, property.Setter.ConnectorName + ":" + property.Setter.GetAdapterName(opt, adapter), additionalProperties: property.Setter.AdditionalAttributeString()));

                // Temporarily rename the parameter to "value"
                var pname = property.Setter.Parameters [0].Name;
                property.Setter.Parameters [0].Name = "value";
                SetBody.Add($"{property.Name} = {property.Setter.Parameters.GetGenericCall (opt, mappings)};");
                property.Setter.Parameters [0].Name = pname;
            }
        }
        public MethodExplicitInterfaceImplementation(GenBase iface, Method method, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name = method.Name;

            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));
            ExplicitInterfaceImplementation = opt.GetOutputName(iface.FullName);

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);

            this.AddMethodParameters(method.Parameters, opt);
        }
Example #10
0
        public BoundInterfacePropertyDeclaration(GenBase gen, Property property, string adapter, CodeGenerationOptions opt)
        {
            Name = property.AdjustedName;

            PropertyType   = new TypeReferenceWriter(opt.GetTypeReferenceName(property));
            IsAutoProperty = true;

            if (property.Getter != null)
            {
                HasGet = true;

                if (gen.IsGeneratable)
                {
                    GetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Getter.JavaName}'{property.Getter.Parameters.GetMethodXPathPredicate ()}]\"");
                }
                if (property.Getter.GenericArguments?.Any() == true)
                {
                    GetterAttributes.Add(new CustomAttr(property.Getter.GenericArguments.ToGeneratedAttributeString()));
                }

                SourceWriterExtensions.AddSupportedOSPlatform(GetterAttributes, property.Getter, opt);

                if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    GetterAttributes.Add(new RegisterAttr(property.Getter.JavaName, property.Getter.JniSignature, property.Getter.ConnectorName + ":" + property.Getter.GetAdapterName(opt, adapter), additionalProperties: property.Getter.AdditionalAttributeString()));
                }
            }

            if (property.Setter != null)
            {
                HasSet = true;

                if (gen.IsGeneratable)
                {
                    SetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Setter.JavaName}'{property.Setter.Parameters.GetMethodXPathPredicate ()}]\"");
                }
                if (property.Setter.GenericArguments?.Any() == true)
                {
                    SetterAttributes.Add(new CustomAttr(property.Setter.GenericArguments.ToGeneratedAttributeString()));
                }

                SourceWriterExtensions.AddSupportedOSPlatform(SetterAttributes, property.Setter, opt);

                if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    SetterAttributes.Add(new RegisterAttr(property.Setter.JavaName, property.Setter.JniSignature, property.Setter.ConnectorName + ":" + property.Setter.GetAdapterName(opt, adapter), additionalProperties: property.Setter.AdditionalAttributeString()));
                }
            }
        }
        public BoundMethodAbstractDeclaration(GenBase gen, Method method, CodeGenerationOptions opt, GenBase impl)
        {
            this.method = method;
            this.opt    = opt;

            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));
            this.AddMethodParameters(method.Parameters, opt);

            if (method.RetVal.IsGeneric && gen != null)
            {
                Name = method.Name;
                ExplicitInterfaceImplementation = opt.GetOutputName(gen.FullName);

                SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);

                Body.Add("throw new NotImplementedException ();");

                return;
            }

            Name = method.AdjustedName;

            IsAbstract = true;
            IsShadow   = impl.RequiresNew(method.Name, method);
            SetVisibility(method.Visibility);

            NewFirst = true;

            if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
            {
                method_callback = new MethodCallback(impl, method, opt, null, method.IsReturnCharSequence);
            }

            method.JavadocInfo?.AddJavadocs(Comments);

            if (method.DeclaringType.IsGeneratable)
            {
                Comments.Add($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
            {
                Attributes.Add(new RegisterAttr(method.JavaName, method.JniSignature, method.ConnectorName, additionalProperties: method.AdditionalAttributeString()));
            }

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);
        }
        public ExplicitInterfaceInvokerMethod(GenBase iface, Method method, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name = method.Name;

            IsUnsafe = true;

            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));
            ExplicitInterfaceImplementation = opt.GetOutputName(iface.FullName);

            this.AddMethodParameters(method.Parameters, opt);
            SourceWriterExtensions.AddMethodBody(Body, method, opt);
        }
Example #13
0
        public GenericExplicitInterfaceImplementationMethod(Method method, GenericSymbol gen, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;
            this.gen    = gen;

            Name = method.Name;

            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));
            ExplicitInterfaceImplementation = opt.GetOutputName(gen.Gen.FullName);

            Comments.Add($"// This method is explicitly implemented as a member of an instantiated {gen.FullName}");

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);
            this.AddMethodParameters(method.Parameters, opt);
        }
Example #14
0
        public InterfaceInvokerMethod(InterfaceGen iface, Method method, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            this.method = method;
            this.opt    = opt;

            Name       = method.AdjustedName;
            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));

            IsPublic = true;
            IsUnsafe = true;
            IsStatic = method.IsStatic;

            method_callback = new MethodCallback(iface, method, opt, null, method.IsReturnCharSequence);
            context_this    = context.ContextType.GetObjectHandleProperty("this");

            this.AddMethodParameters(method.Parameters, opt);
        }
        public BoundMethodStringOverload(Method method, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name     = method.Name;
            IsStatic = method.IsStatic;

            SetVisibility(method.Visibility);
            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string"));

            if (method.Deprecated != null)
            {
                Attributes.Add(new ObsoleteAttr(method.Deprecated.Replace("\"", "\"\"").Trim()));
            }

            this.AddMethodParametersStringOverloads(method.Parameters, opt);
        }
        public InterfaceEventHandlerImplMethod(InterfaceGen iface, Method method, List <string> handlers, CodeGenerationOptions opt)
        {
            this.iface   = iface;
            this.method  = method;
            this.opt     = opt;
            needs_sender = iface.NeedsSender;

            method_spec = iface.Methods.Count > 1 ? method.AdjustedName : string.Empty;
            args_name   = iface.GetArgsName(method);

            handlers.Add(method_spec);

            Name       = method.Name;
            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));

            IsPublic = true;

            this.AddMethodParameters(method.Parameters, opt);
        }
Example #17
0
        void AddInterfaceEventHandler(InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            if (!iface.IsListener)
            {
                return;
            }

            foreach (var method in iface.Methods.Where(m => m.EventName != string.Empty))
            {
                if (method.RetVal.IsVoid || method.IsEventHandlerWithHandledProperty)
                {
                    if (!method.IsSimpleEventHandler || method.IsEventHandlerWithHandledProperty)
                    {
                        var event_args_class = post_sibling_types.OfType <InterfaceEventArgsClass> ().SingleOrDefault(c => c.Name == iface.GetArgsName(method));

                        // Check if there's an existing EventArgs class to add to
                        if (event_args_class is null)
                        {
                            event_args_class = new InterfaceEventArgsClass(iface, method);
                            post_sibling_types.Add(event_args_class);
                        }

                        event_args_class.AddMembersFromMethod(iface, method, opt);
                    }
                }
                else
                {
                    var del = new DelegateWriter {
                        Name     = iface.GetEventDelegateName(method),
                        Type     = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal)),
                        IsPublic = true
                    };

                    SourceWriterExtensions.AddMethodParameters(del, method.Parameters, opt);

                    post_sibling_types.Add(del);
                }
            }

            post_sibling_types.Add(new InterfaceEventHandlerImplClass(iface, opt, context));
        }
        public MethodAsyncWrapper(Method method, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name     = method.AdjustedName + "Async";
            IsStatic = method.IsStatic;

            SetVisibility(method.Visibility);

            ReturnType = new TypeReferenceWriter("global::System.Threading.Tasks.Task");

            if (!method.IsVoid)
            {
                ReturnType.Name += "<" + opt.GetTypeReferenceName(method.RetVal) + ">";
            }

            Body.Add($"return global::System.Threading.Tasks.Task.Run (() => {method.AdjustedName} ({method.Parameters.GetCall (opt)}));");

            this.AddMethodParameters(method.Parameters, opt);
        }
Example #19
0
        public BoundProperty(GenBase gen, Property property, CodeGenerationOptions opt, bool withCallbacks = true, bool forceOverride = false)
        {
            Name         = property.AdjustedName;
            PropertyType = new TypeReferenceWriter(opt.GetTypeReferenceName(property.Getter.RetVal));

            SetVisibility(gen is InterfaceGen ? string.Empty : property.Getter.IsAbstract && property.Getter.RetVal.IsGeneric ? "protected" : (property.Setter ?? property.Getter).Visibility);

            IsUnsafe = true;
            HasGet   = true;

            var is_virtual = property.Getter.IsVirtual && (property.Setter == null || property.Setter.IsVirtual);

            if (is_virtual && withCallbacks)
            {
                IsVirtual = true;
                IsShadow  = gen.RequiresNew(property);

                if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    getter_callback = new MethodCallback(gen, property.Getter, opt, property.AdjustedName, false);
                }

                if (property.Setter != null && opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    setter_callback = new MethodCallback(gen, property.Setter, opt, property.AdjustedName, false);
                }
            }

            if (forceOverride || ShouldForceOverride(property))
            {
                IsVirtual  = false;
                IsOverride = true;
            }

            if ((property.Getter ?? property.Setter).IsStatic)
            {
                IsStatic   = true;
                IsVirtual  = false;
                IsOverride = false;
            }
            else if (gen.BaseSymbol != null)
            {
                // It should be using AdjustedName instead of Name, but ICharSequence ("Formatted") properties are not caught by this...
                var base_prop = gen.BaseSymbol.GetPropertyByName(property.Name, true);

                // If the matching base getter we found is a DIM, we do not override it, it should stay virtual
                if (base_prop != null && !base_prop.Getter.IsInterfaceDefaultMethod)
                {
                    IsVirtual  = false;
                    IsOverride = true;
                }
            }

            // Allow user to override our virtual/override logic
            if (!forceOverride && (property.Getter ?? property.Setter).ManagedOverride?.ToLowerInvariant() == "virtual")
            {
                IsVirtual  = true;
                IsOverride = false;
            }
            else if (!forceOverride && (property.Getter ?? property.Setter).ManagedOverride?.ToLowerInvariant() == "override")
            {
                IsVirtual  = false;
                IsOverride = true;
            }

            // Unlike [Register], [Obsolete] cannot be put on property accessors, so we can apply them only under limited condition...
            if (property.Getter.Deprecated != null && (property.Setter == null || property.Setter.Deprecated != null))
            {
                Attributes.Add(new ObsoleteAttr(property.Getter.Deprecated.Replace("\"", "\"\"").Trim() + (property.Setter != null && property.Setter.Deprecated != property.Getter.Deprecated ? " " + property.Setter.Deprecated.Replace("\"", "\"\"").Trim() : null)));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, property.Getter, opt);

            SourceWriterExtensions.AddMethodCustomAttributes(GetterAttributes, property.Getter);

            if (gen.IsGeneratable)
            {
                GetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Getter.JavaName}'{property.Getter.Parameters.GetMethodXPathPredicate ()}]\"");
            }

            if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
            {
                GetterAttributes.Add(new RegisterAttr(property.Getter.JavaName, property.Getter.JniSignature, property.Getter.IsVirtual ? property.Getter.GetConnectorNameFull(opt) : string.Empty, additionalProperties: property.Getter.AdditionalAttributeString()));
            }

            SourceWriterExtensions.AddMethodBody(GetBody, property.Getter, opt);

            if (property.Setter != null)
            {
                HasSet = true;

                if (gen.IsGeneratable)
                {
                    SetterComments.Add($"// Metadata.xml XPath method reference: path=\"{gen.MetadataXPathReference}/method[@name='{property.Setter.JavaName}'{property.Setter.Parameters.GetMethodXPathPredicate ()}]\"");
                }

                SourceWriterExtensions.AddSupportedOSPlatform(SetterAttributes, property.Setter, opt);

                SourceWriterExtensions.AddMethodCustomAttributes(SetterAttributes, property.Setter);
                if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
                {
                    SetterAttributes.Add(new RegisterAttr(property.Setter.JavaName, property.Setter.JniSignature, property.Setter.IsVirtual ? property.Setter.GetConnectorNameFull(opt) : string.Empty, additionalProperties: property.Setter.AdditionalAttributeString()));
                }

                var pname = property.Setter.Parameters [0].Name;
                property.Setter.Parameters [0].Name = "value";
                SourceWriterExtensions.AddMethodBody(SetBody, property.Setter, opt);
                property.Setter.Parameters [0].Name = pname;
            }
            else if (property.GenerateDispatchingSetter)
            {
                HasSet = true;
                SetterComments.Add("// This is a dispatching setter");
                SetBody.Add($"Set{property.Name} (value);");
            }

            AddJavadocs(property);
        }
Example #20
0
        public BoundFieldAsProperty(GenBase type, Field field, CodeGenerationOptions opt)
        {
            this.field = field;
            this.opt   = opt;

            Name = field.Name;

            var fieldType = field.Symbol.IsArray ? "IList<" + field.Symbol.ElementType + ">" + opt.NullableOperator : opt.GetTypeReferenceName(field);

            PropertyType = new TypeReferenceWriter(fieldType);

            Comments.Add($"// Metadata.xml XPath field reference: path=\"{type.MetadataXPathReference}/field[@name='{field.JavaName}']\"");

            if (field.IsEnumified)
            {
                Attributes.Add(new GeneratedEnumAttr());
            }

            Attributes.Add(new RegisterAttr(field.JavaName, additionalProperties: field.AdditionalAttributeString()));

            if (field.IsDeprecated)
            {
                Attributes.Add(new ObsoleteAttr(field.DeprecatedComment, field.IsDeprecatedError)
                {
                    NoAtSign = true
                });
            }

            SetVisibility(field.Visibility);
            UseExplicitPrivateKeyword = true;

            IsStatic = field.IsStatic;

            HasGet = true;

            if (!field.IsConst)
            {
                HasSet = true;
            }
        }
Example #21
0
        void AddConstructor(InterfaceGen iface, Method method, CodeGenerationOptions opt)
        {
            var ctor = new ConstructorWriter {
                Name     = iface.GetArgsName(method),
                IsPublic = true
            };

            if (method.IsEventHandlerWithHandledProperty)
            {
                ctor.Parameters.Add(new MethodParameterWriter("handled", TypeReferenceWriter.Bool));
                ctor.Body.Add("this.handled = handled;");
            }

            foreach (var p in method.Parameters)
            {
                if (p.IsSender)
                {
                    continue;
                }

                ctor.Parameters.Add(new MethodParameterWriter(p.Name, new TypeReferenceWriter(opt.GetTypeReferenceName(p))));
                ctor.Body.Add($"this.{opt.GetSafeIdentifier (p.Name)} = {opt.GetSafeIdentifier (p.Name)};");
            }

            Constructors.Add(ctor);
        }
Example #22
0
        public BoundMethod(GenBase type, Method method, CodeGenerationOptions opt, bool generateCallbacks)
        {
            JavaMethod = method;

            if (generateCallbacks && method.IsVirtual)
            {
                callback = new MethodCallback(type, method, opt, null, method.IsReturnCharSequence);
            }

            Name = method.AdjustedName;

            IsStatic = method.IsStatic;
            IsSealed = method.IsOverride && method.IsFinal;
            IsUnsafe = true;

            SetVisibility(type is InterfaceGen && !IsStatic ? string.Empty : method.Visibility);

            // TODO: Clean up this logic
            var is_explicit = opt.SupportDefaultInterfaceMethods && type is InterfaceGen && method.OverriddenInterfaceMethod != null;
            var virt_ov     = is_explicit ? string.Empty : method.IsOverride ? (opt.SupportDefaultInterfaceMethods && method.OverriddenInterfaceMethod != null ? " virtual" : " override") : method.IsVirtual ? " virtual" : string.Empty;

            IsVirtual  = virt_ov.Trim() == "virtual";
            IsOverride = virt_ov.Trim() == "override";

            // When using DIM, don't generate "virtual sealed" methods, remove both modifiers instead
            if (opt.SupportDefaultInterfaceMethods && method.OverriddenInterfaceMethod != null && IsVirtual && IsSealed)
            {
                IsVirtual = false;
                IsSealed  = false;
            }

            if (is_explicit)
            {
                ExplicitInterfaceImplementation = GetDeclaringTypeOfExplicitInterfaceMethod(method.OverriddenInterfaceMethod);
            }

            if ((IsVirtual || !IsOverride) && type.RequiresNew(method.AdjustedName, method))
            {
                IsShadow = true;
            }

            // Allow user to override our virtual/override logic
            if (method.ManagedOverride?.ToLowerInvariant() == "virtual")
            {
                IsVirtual  = true;
                IsOverride = false;
            }
            else if (method.ManagedOverride?.ToLowerInvariant() == "override")
            {
                IsVirtual  = false;
                IsOverride = true;
            }

            ReturnType = new TypeReferenceWriter(opt.GetTypeReferenceName(method.RetVal));

            method.JavadocInfo?.AddJavadocs(Comments);

            if (method.DeclaringType.IsGeneratable)
            {
                Comments.Add($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
            }

            if (method.Deprecated.HasValue())
            {
                Attributes.Add(new ObsoleteAttr(method.Deprecated.Replace("\"", "\"\"")));
            }

            if (method.IsReturnEnumified)
            {
                Attributes.Add(new GeneratedEnumAttr(true));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            Attributes.Add(new RegisterAttr(method.JavaName, method.JniSignature, method.IsVirtual ? method.GetConnectorNameFull(opt) : string.Empty, additionalProperties: method.AdditionalAttributeString()));

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);
            this.AddMethodParameters(method.Parameters, opt);

            SourceWriterExtensions.AddMethodBody(Body, method, opt);
        }
Example #23
0
        public static void AddMethodParameters(this ITakeParameters method, ParameterList parameters, CodeGenerationOptions opt)
        {
            foreach (var p in parameters)
            {
                var para = new MethodParameterWriter(opt.GetSafeIdentifier(p.Name), new TypeReferenceWriter(opt.GetTypeReferenceName(p)));

                if (p.IsEnumified)
                {
                    para.Attributes.Add(new GeneratedEnumAttr());
                }
                if (p.Annotation != null)
                {
                    para.Attributes.Add(new CustomAttr(p.Annotation));
                }

                method.Parameters.Add(para);
            }
        }
Example #24
0
        // This replaces any `Java.Lang.ICharSequence` parameters with `string`.
        public static void AddMethodParametersStringOverloads(this MethodWriter method, ParameterList parameters, CodeGenerationOptions opt)
        {
            foreach (var p in parameters)
            {
                var para = new MethodParameterWriter(opt.GetSafeIdentifier(p.Name), new TypeReferenceWriter(opt.GetTypeReferenceName(p).Replace("Java.Lang.ICharSequence", "string").Replace("global::string", "string")));

                if (p.IsEnumified)
                {
                    para.Attributes.Add(new GeneratedEnumAttr());
                }
                if (p.Annotation != null)
                {
                    para.Attributes.Add(new CustomAttr(p.Annotation));
                }

                method.Parameters.Add(para);
            }
        }
        protected override void WriteBody(CodeWriter writer)
        {
            // generate nothing
            if (method.EventName == string.Empty)
            {
                return;
            }

            if (method.IsVoid)
            {
                writer.WriteLine($"var __h = {method_spec}Handler;");
                writer.WriteLine($"if (__h != null)");
                writer.WriteLine($"\t__h ({(needs_sender ? "sender" : method.Parameters.SenderName)}, new {args_name} ({method.Parameters.CallDropSender}));");
                return;
            }

            if (method.IsEventHandlerWithHandledProperty)
            {
                writer.WriteLine($"var __h = {method_spec}Handler;");
                writer.WriteLine($"if (__h == null)");
                writer.WriteLine($"\treturn {method.RetVal.DefaultValue};");

                var call = method.Parameters.CallDropSender;
                writer.WriteLine($"var __e = new {args_name} (true{(call.Length != 0 ? ", " : "")}{call});");
                writer.WriteLine($"__h ({(needs_sender ? "sender" : method.Parameters.SenderName)}, __e);");
                writer.WriteLine($"return __e.Handled;");
                return;
            }

            writer.WriteLine($"var __h = {method_spec}Handler;");
            writer.WriteLine($"return __h != null ? __h ({method.Parameters.GetCall (opt)}) : default ({opt.GetTypeReferenceName (method.RetVal)});");
        }