Example #1
0
 void AddNestedSiblingTypes(InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
 {
     // Generate sibling types for nested types we don't want to nest
     foreach (var nest in iface.NestedTypes.Where(t => t.Unnest))
     {
         pre_sibling_types.Add(SourceWriterExtensions.BuildManagedTypeModel(nest, opt, context, genInfo));
     }
 }
        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()));
            }
        }
        protected override void WriteSetterBody(CodeWriter writer)
        {
            var pname = property.Setter.Parameters [0].Name;

            property.Setter.Parameters [0].Name = "value";

            SourceWriterExtensions.WriteMethodInvokerBody(writer, property.Setter, opt, context_this);

            property.Setter.Parameters [0].Name = pname;
        }
Example #4
0
 void AddNestedTypes(InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
 {
     // Generate nested types for supported nested types.  This is a new addition in C#8.
     // Prior to this, types nested in an interface had to be generated as sibling types.
     // The "Unnest" property is used to support backwards compatibility with pre-C#8 bindings.
     foreach (var nest in iface.NestedTypes.Where(t => !t.Unnest))
     {
         NestedTypes.Add(SourceWriterExtensions.BuildManagedTypeModel(nest, opt, context, genInfo));
     }
 }
Example #5
0
        public InterfaceListenerEventHandlerHelper(InterfaceGen iface, Method method, string add, CodeGenerationOptions opt)
        {
            Name = add + "_Event_With_Handler_Helper";
            Parameters.Add(new MethodParameterWriter("value", new TypeReferenceWriter(opt.GetOutputName(iface.FullName))));
            ReturnType = TypeReferenceWriter.Void;

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            Body.Add($"{add} (value, null);");
        }
Example #6
0
        void AddNestedTypes(ClassGen klass, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo genInfo)
        {
            foreach (var nest in klass.NestedTypes)
            {
                if (klass.BaseGen?.ContainsNestedType(nest) == true && nest is ClassGen c)
                {
                    c.NeedsNew = true;
                }

                NestedTypes.Add(SourceWriterExtensions.BuildManagedTypeModel(nest, opt, context, genInfo));
            }
        }
        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;
            }
        }
Example #8
0
        void AddFields(InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            // Interface fields are only supported with DIM
            if (!opt.SupportInterfaceConstants && !opt.SupportDefaultInterfaceMethods)
            {
                return;
            }

            var seen   = new HashSet <string> ();
            var fields = iface.GetGeneratableFields(opt).ToList();

            SourceWriterExtensions.AddFields(this, iface, fields, seen, opt, context);
        }
        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);
        }
        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);
        }
Example #11
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 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);
        }
        // Historically .NET has not allowed interface implemented fields or constants, so we
        // initially worked around that by moving them to an abstract class, generally
        // IMyInterface -> MyInterfaceConsts
        // This was later expanded to accomodate static interface methods, creating a more appropriately named class
        // IMyInterface -> MyInterface
        // In this case the XXXConsts class is [Obsolete]'d and simply inherits from the newer class
        // in order to maintain backward compatibility.
        // If we're creating a binding that supports DIM, we remove the XXXConsts class as they've been
        // [Obsolete:iserror] for a long time, and we add [Obsolete] to the interface "class".
        public InterfaceMemberAlternativeClass(InterfaceGen iface, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            var should_obsolete = opt.SupportInterfaceConstants && opt.SupportDefaultInterfaceMethods;

            Name = iface.HasManagedName
                                ? iface.Name.Substring(1) + "Consts"
                                : iface.Name.Substring(1);

            Inherits = "Java.Lang.Object";

            IsPublic   = true;
            IsAbstract = true;

            UsePriorityOrder = true;

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, iface, opt);

            Attributes.Add(new RegisterAttr(iface.RawJniName, noAcw: true, additionalProperties: iface.AdditionalAttributeString())
            {
                AcwLast = true
            });

            if (should_obsolete)
            {
                Attributes.Add(new ObsoleteAttr($"Use the '{iface.FullName}' type. This class will be removed in a future release.")
                {
                    WriteGlobal = true, NoAtSign = true
                });
            }

            Constructors.Add(new ConstructorWriter {
                Name = Name, IsInternal = true
            });

            var needs_class_ref = AddFields(iface, should_obsolete, opt, context);

            AddMethods(iface, should_obsolete, opt);

            if (needs_class_ref || iface.Methods.Where(m => m.IsStatic).Any())
            {
                Fields.Add(new PeerMembersField(opt, iface.RawJniName, Name, false));
            }

            if (!iface.HasManagedName && !opt.SupportInterfaceConstants)
            {
                sibling_classes.Add(new InterfaceConstsForwardClass(iface));
            }
        }
Example #14
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 #15
0
        void AddMethods(ClassGen klass, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            var methodsToDeclare = klass.Methods.AsEnumerable();

            // This does not exclude overrides (unlike virtual methods) because we're not sure
            // if calling the base interface default method via JNI expectedly dispatches to
            // the derived method.
            var defaultMethods = klass.GetAllDerivedInterfaces()
                                 .SelectMany(i => i.Methods)
                                 .Where(m => m.IsInterfaceDefaultMethod)
                                 .Where(m => !klass.ContainsMethod(m, false, false));

            var overrides = defaultMethods.Where(m => m.OverriddenInterfaceMethod != null);

            var overridens = defaultMethods.Where(m => overrides.Where(_ => _.Name == m.Name && _.JniSignature == m.JniSignature)
                                                  .Any(mm => mm.DeclaringType.GetAllDerivedInterfaces().Contains(m.DeclaringType)));

            methodsToDeclare = opt.SupportDefaultInterfaceMethods ? methodsToDeclare : methodsToDeclare.Concat(defaultMethods.Except(overridens)).Where(m => m.DeclaringType.IsGeneratable);

            foreach (var m in methodsToDeclare)
            {
                var virt = m.IsVirtual;
                m.IsVirtual = !klass.IsFinal && virt;

                if (m.IsAbstract && m.OverriddenInterfaceMethod == null && (opt.SupportDefaultInterfaceMethods || !m.IsInterfaceDefaultMethod))
                {
                    AddAbstractMethodDeclaration(klass, m, null);
                }
                else
                {
                    AddMethod(klass, m, opt);
                }

                context.ContextGeneratedMethods.Add(m);
                m.IsVirtual = virt;
            }

            var methods = klass.Methods.Concat(klass.Properties.Where(p => p.Setter != null).Select(p => p.Setter));

            foreach (var type in methods.Where(m => m.IsListenerConnector && m.EventName != string.Empty).Select(m => m.ListenerType).Distinct())
            {
                AddInlineComment($"#region \"Event implementation for {type.FullName}\"");
                SourceWriterExtensions.AddInterfaceListenerEventsAndProperties(this, type, klass, opt);
                AddInlineComment("#endregion");
            }
        }
        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);

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

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

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, field, opt);

            if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
            {
                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 #17
0
        public BoundPropertyStringVariant(Property property, CodeGenerationOptions opt)
        {
            var is_array = property.Getter.RetVal.IsArray;

            Name = property.Name;

            PropertyType = new TypeReferenceWriter("string" + (is_array ? "[]" : string.Empty))
            {
                Nullable = opt.SupportNullableReferenceTypes
            };

            SetVisibility((property.Setter ?? property.Getter).Visibility);

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

            HasGet = true;

            if (is_array)
            {
                GetBody.Add($"return CharSequence.ArrayToStringArray ({property.AdjustedName});");
            }
            else
            {
                GetBody.Add($"return {property.AdjustedName} == null ? null : {property.AdjustedName}.ToString ();");
            }

            if (property.Setter is null)
            {
                return;
            }

            HasSet = true;

            if (is_array)
            {
                SetBody.Add($"global::Java.Lang.ICharSequence[] jlsa = CharSequence.ArrayFromStringArray (value);");
                SetBody.Add($"{property.AdjustedName} = jlsa;");
                SetBody.Add($"foreach (var jls in jlsa) if (jls != null) jls.Dispose ();");
            }
            else
            {
                SetBody.Add($"var jls = value == null ? null : new global::Java.Lang.String (value);");
                SetBody.Add($"{property.AdjustedName} = jls;");
                SetBody.Add($"if (jls != null) jls.Dispose ();");
            }
        }
        // static Delegate GetByteValueExactHandler ()
        // {
        //  if (cb_byteValueExact == null)
        //      cb_byteValueExact = JNINativeWrapper.CreateDelegate ((_JniMarshal_PP_B) n_ByteValueExact);
        //  return cb_byteValueExact;
        // }
        public GetDelegateHandlerMethod(Method method, CodeGenerationOptions opt)
        {
            this.method = method;
            this.opt    = opt;

            Name       = method.ConnectorName;
            ReturnType = TypeReferenceWriter.Delegate;

            IsStatic  = true;
            IsPrivate = method.IsInterfaceDefaultMethod;

            if (!string.IsNullOrWhiteSpace(method.Deprecated))
            {
                Attributes.Add(new ObsoleteAttr());
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);
        }
        public InterfaceConstsClass(ClassGen klass, HashSet <string> seen, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            Name = "InterfaceConsts";

            IsPublic = true;
            IsStatic = true;

            UsePriorityOrder = true;

            foreach (var iface in klass.GetAllImplementedInterfaces()
                     .Except(klass.BaseGen?.GetAllImplementedInterfaces() ?? new InterfaceGen [0])
                     .Where(i => i.Fields.Count > 0))
            {
                AddInlineComment($"// The following are fields from: {iface.JavaName}");

                SourceWriterExtensions.AddFields(this, iface, iface.Fields, seen, opt, context);
            }
        }
Example #20
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(opt, "this");

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            this.AddMethodParameters(method.Parameters, opt);
        }
        public BoundConstructor(ClassGen klass, Ctor constructor, bool useBase, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            this.constructor = constructor;
            this.opt         = opt;
            this.context     = context;

            Name = klass.Name;

            constructor.JavadocInfo?.AddJavadocs(Comments);
            Comments.Add(string.Format("// Metadata.xml XPath constructor reference: path=\"{0}/constructor[@name='{1}'{2}]\"", klass.MetadataXPathReference, klass.JavaSimpleName, constructor.Parameters.GetMethodXPathPredicate()));

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, constructor, opt);

            if (opt.CodeGenerationTarget != CodeGenerationTarget.JavaInterop1)
            {
                Attributes.Add(new RegisterAttr(".ctor", constructor.JniSignature, string.Empty, additionalProperties: constructor.AdditionalAttributeString()));
            }

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

            if (constructor.CustomAttributes != null)
            {
                Attributes.Add(new CustomAttr(constructor.CustomAttributes));
            }

            if (constructor.Annotation != null)
            {
                Attributes.Add(new CustomAttr(constructor.Annotation));
            }

            SetVisibility(constructor.Visibility);
            IsUnsafe = true;

            BaseCall = opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1
                                ? $"{(useBase ? "base" : "this")} (ref *InvalidJniObjectReference, JniObjectReferenceOptions.None)"
                                : $"{(useBase ? "base" : "this")} (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)";
            context_this = context.ContextType.GetObjectHandleProperty(opt, "this");

            this.AddMethodParameters(constructor.Parameters, opt);
        }
Example #22
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 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()));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

            method.JavadocInfo?.AddJavadocs(Comments);

            this.AddMethodParametersStringOverloads(method.Parameters, opt);
        }
        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]"));
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

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

            method.JavadocInfo?.AddJavadocs(Comments);

            SourceWriterExtensions.AddMethodCustomAttributes(Attributes, method);
            this.AddMethodParameters(method.Parameters, opt);
        }
Example #25
0
        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) + ">";
            }

            SourceWriterExtensions.AddSupportedOSPlatform(Attributes, method, opt);

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

            this.AddMethodParameters(method.Parameters, opt);
        }
        bool AddInterfaceFields(InterfaceGen iface, List <Field> fields, HashSet <string> seen, CodeGenerationOptions opt, CodeGeneratorContext context)
        {
            var needs_property = false;

            foreach (var f in fields)
            {
                if (iface.ContainsName(f.Name))
                {
                    Report.LogCodedWarning(0, SourceWriterExtensions.GetFieldCollisionMessage(iface, f), f, iface.FullName, f.Name, iface.JavaName);
                    continue;
                }

                if (seen.Contains(f.Name))
                {
                    Report.LogCodedWarning(0, Report.WarningDuplicateField, f, iface.FullName, f.Name, iface.JavaName);
                    continue;
                }

                if (f.Validate(opt, iface.TypeParameters, context))
                {
                    seen.Add(f.Name);
                    needs_property = needs_property || f.NeedsProperty;

                    if (f.NeedsProperty)
                    {
                        Properties.Add(new BoundFieldAsProperty(iface, f, opt));
                    }
                    else
                    {
                        Fields.Add(new BoundField(iface, f, opt));
                    }
                }
            }

            return(needs_property);
        }
Example #27
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);
        }
 protected override void WriteBody(CodeWriter writer)
 {
     SourceWriterExtensions.WriteMethodStringOverloadBody(writer, method, opt, false);
 }
Example #29
0
        public BoundClass(ClassGen klass, CodeGenerationOptions opt, CodeGeneratorContext context, GenerationInfo generationInfo)
        {
            context.ContextTypes.Push(klass);
            context.ContextGeneratedMethods = new List <Method> ();

            generationInfo.TypeRegistrations.Add(new KeyValuePair <string, string> (klass.RawJniName, klass.AssemblyQualifiedName));

            var is_enum = klass.base_symbol != null && klass.base_symbol.FullName == "Java.Lang.Enum";

            if (is_enum)
            {
                generationInfo.Enums.Add(klass.RawJniName.Replace('/', '.') + ":" + klass.Namespace + ":" + klass.JavaSimpleName);
            }

            this.opt = opt;

            Name = klass.Name;

            SetVisibility(klass.Visibility);
            IsShadow   = klass.NeedsNew;
            IsAbstract = klass.IsAbstract;
            IsSealed   = klass.IsFinal;
            IsPartial  = true;

            UsePriorityOrder = true;

            AddImplementedInterfaces(klass);

            Comments.Add($"// Metadata.xml XPath class reference: path=\"{klass.MetadataXPathReference}\"");

            if (klass.IsDeprecated)
            {
                Attributes.Add(new ObsoleteAttr(klass.DeprecatedComment)
                {
                    WriteAttributeSuffix = true
                });
            }

            Attributes.Add(new RegisterAttr(klass.RawJniName, null, null, true, klass.AdditionalAttributeString())
            {
                UseGlobal = true, UseShortForm = true
            });

            if (klass.TypeParameters != null && klass.TypeParameters.Any())
            {
                Attributes.Add(new CustomAttr(klass.TypeParameters.ToGeneratedAttributeString()));
            }

            // Figure out our base class
            string obj_type = null;

            if (klass.base_symbol != null)
            {
                obj_type = klass.base_symbol is GenericSymbol gs &&
                           gs.IsConcrete ? gs.GetGenericType(null) : opt.GetOutputName(klass.base_symbol.FullName);
            }

            if (klass.InheritsObject && obj_type != null)
            {
                Inherits = obj_type;
            }

            // Handle fields
            var seen = new HashSet <string> ();

            SourceWriterExtensions.AddFields(this, klass, klass.Fields, seen, opt, context);

            var ic = new InterfaceConstsClass(klass, seen, opt, context);

            if (ic.ShouldGenerate)
            {
                NestedTypes.Add(ic);
            }

            // Sibling classes
            if (!klass.AssemblyQualifiedName.Contains('/'))
            {
                foreach (InterfaceExtensionInfo nestedIface in klass.GetNestedInterfaceTypes())
                {
                    if (nestedIface.Type.Methods.Any(m => m.CanHaveStringOverload) || nestedIface.Type.Methods.Any(m => m.Asyncify))
                    {
                        sibling_types.Add(new InterfaceExtensionsClass(nestedIface.Type, nestedIface.DeclaringType, opt));
                    }
                }
            }

            if (klass.IsAbstract)
            {
                sibling_types.Add(new ClassInvokerClass(klass, opt));
            }

            AddNestedTypes(klass, opt, context, generationInfo);
            AddBindingInfrastructure(klass);
            AddConstructors(klass, opt, context);
            AddProperties(klass, opt);
            AddMethods(klass, opt, context);
            AddAbstractMembers(klass, opt, context);
            AddExplicitGenericInterfaceMembers(klass, opt);
            AddCharSequenceEnumerator(klass);

            context.ContextGeneratedMethods.Clear();
            context.ContextTypes.Pop();
        }
 protected override void WriteGetterBody(CodeWriter writer)
 {
     SourceWriterExtensions.WriteMethodInvokerBody(writer, property.Getter, opt, context_this);
 }