Example #1
0
        public void WriteSealedOverriddenDefaultMethod()
        {
            // Create an interface with a default method
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");

            iface.Methods.Add(new TestMethod(iface, "DoSomething").SetDefaultInterfaceMethod());
            options.SymbolTable.AddType(iface);

            // Create a type that inherits the interface, overriding the method as final
            var klass = new TestClass("java.code.IMyInterface", "java.code.MyClass");

            klass.AddImplementedInterface("java.code.IMyInterface");
            klass.Methods.Add(new TestMethod(iface, "DoSomething").SetFinal());

            iface.Validate(options, new GenericParameterDefinitionList(), generator.Context);
            klass.Validate(options, new GenericParameterDefinitionList(), generator.Context);

            klass.FixupMethodOverrides(options);

            generator.Context.ContextTypes.Push(klass);
            generator.WriteClass(klass, string.Empty, new GenerationInfo(string.Empty, string.Empty, "MyAssembly"));
            generator.Context.ContextTypes.Pop();

            // The method should not be marked as 'virtual sealed'
            Assert.False(writer.ToString().Contains("virtual sealed"));
        }
Example #2
0
        public void WriteConstSugarInterfaceFields()
        {
            // Need a JLO class "FromXml" to trigger ConstSugar logic. (ie: this is "building" Mono.Android.dll)
            var klass = new TestClass("java.lang.Object", "java.lang.Object")
            {
                FromXml = true,
            };

            options.SymbolTable.AddType(klass);

            // This is an interface that only has fields (IsConstSugar)
            // We treat   a little differenly because they don't need to interop with Java
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");

            // These interface fields are supported and should be in the output
            iface.Fields.Add(new TestField("int", "MyConstantField").SetConstant().SetValue("7"));
            iface.Fields.Add(new TestField("java.lang.String", "MyConstantStringField").SetConstant().SetValue("\"hello\""));
            iface.Fields.Add(new TestField("int", "MyDeprecatedField").SetConstant().SetValue("7").SetDeprecated());

            // These interface fields are not supported and should be ignored
            iface.Fields.Add(new TestField("int", "MyDeprecatedEnumField").SetConstant().SetValue("MyEnumValue").SetDeprecated("This constant will be removed in the future version."));
            iface.Fields.Add(new TestField("int", "MyStaticField").SetStatic().SetValue("7"));

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfaceDeclaration(iface, string.Empty, new GenerationInfo(null, null, null));
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteConstSugarInterfaceFields)), writer.ToString().NormalizeLineEndings());
        }
Example #3
0
        public void WriteInterfaceDefaultMethod()
        {
            // Create an interface with a default method
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");

            iface.Methods.Add(new TestMethod(iface, "DoSomething").SetDefaultInterfaceMethod());

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.WriteInterfaceDeclaration(iface, string.Empty);

            Assert.AreEqual(GetTargetedExpected(nameof(WriteInterfaceDefaultMethod)), writer.ToString().NormalizeLineEndings());
        }
Example #4
0
        public void ValidateInterfaceMethods()
        {
            var options = new CodeGenerationOptions {
                SupportDefaultInterfaceMethods = true
            };
            var iface = SupportTypeBuilder.CreateEmptyInterface("My.Test.Interface");

            iface.Methods.Add(SupportTypeBuilder.CreateMethod(iface, "DoAbstractThing", options));
            iface.Methods.Add(SupportTypeBuilder.CreateMethod(iface, "DoDefaultThing", options).SetDefaultInterfaceMethod());

            // The interface should be valid
            Assert.True(iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext()));

            Assert.AreEqual(2, iface.Methods.Count);
        }
        public void WriteInterfaceFields()
        {
            // This is an interface that has both fields and method declarations
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");

            iface.Fields.Add(new TestField("int", "MyConstantField").SetConstant().SetValue("7"));
            iface.Methods.Add(new TestMethod(iface, "DoSomething").SetAbstract());

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfaceDeclaration(iface, string.Empty);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteInterfaceFields)), writer.ToString().NormalizeLineEndings());
        }
Example #6
0
        public void ValidateInvalidAbstractInterfaceMethods()
        {
            var options = new CodeGenerationOptions {
                SupportDefaultInterfaceMethods = true
            };
            var iface = SupportTypeBuilder.CreateEmptyInterface("My.Test.Interface");

            iface.Methods.Add(SupportTypeBuilder.CreateMethod(iface, "DoAbstractThing", options, "potato"));
            iface.Methods.Add(SupportTypeBuilder.CreateMethod(iface, "DoDefaultThing", options).SetDefaultInterfaceMethod());

            // The interface should be invalid because an abstract method is invalid
            Assert.False(iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext()));

            // The invalid abstract method should be removed, leaving just the valid default method
            Assert.AreEqual(1, iface.Methods.Count);
        }
Example #7
0
        public void WriteInterfaceDefaultPropertyGetterOnly()
        {
            // Create an interface with a default method
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");
            var prop  = SupportTypeBuilder.CreateProperty(iface, "Value", "int", options);

            prop.Getter.IsInterfaceDefaultMethod = true;
            prop.Setter = null;

            iface.Properties.Add(prop);

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.WriteInterfaceDeclaration(iface, string.Empty);

            Assert.AreEqual(GetTargetedExpected(nameof(WriteInterfaceDefaultPropertyGetterOnly)), writer.ToString().NormalizeLineEndings());
        }
Example #8
0
        public void WriteStaticInterfaceProperty()
        {
            // Create an interface with a static property
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");
            var prop  = SupportTypeBuilder.CreateProperty(iface, "Value", "int", options);

            prop.Getter.IsStatic  = true;
            prop.Getter.IsVirtual = false;
            prop.Setter.IsStatic  = true;
            prop.Setter.IsVirtual = false;

            iface.Properties.Add(prop);

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.WriteInterfaceDeclaration(iface, string.Empty, new GenerationInfo(null, null, null));

            Assert.AreEqual(GetTargetedExpected(nameof(WriteStaticInterfaceProperty)), writer.ToString().NormalizeLineEndings());
        }
Example #9
0
        public void WriteInterfaceRedeclaredDefaultMethod()
        {
            // Create an interface with a default method
            var iface = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface");

            iface.Methods.Add(new TestMethod(iface, "DoSomething").SetDefaultInterfaceMethod());
            options.SymbolTable.AddType(iface);

            // Create a second interface that inherits the first, declaring the method as not default
            var iface2 = SupportTypeBuilder.CreateEmptyInterface("java.code.IMyInterface2");

            iface2.AddImplementedInterface("java.code.IMyInterface");
            iface2.Methods.Add(new TestMethod(iface, "DoSomething"));

            iface.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());
            iface2.Validate(options, new GenericParameterDefinitionList(), new CodeGeneratorContext());

            generator.WriteInterfaceDeclaration(iface2, string.Empty);

            // IMyInterface2 should generate the method as abstract, not a default method
            Assert.AreEqual(GetExpected(nameof(WriteInterfaceRedeclaredDefaultMethod)), writer.ToString().NormalizeLineEndings());
        }