コード例 #1
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());
        }
コード例 #2
0
        private ClassGen CreateSealedClass()
        {
            var klass = SupportTypeBuilder.CreateClass("my.example.class", options);

            klass.IsFinal = true;
            return(klass);
        }
コード例 #3
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"));
        }
コード例 #4
0
        public void GetContextTypeMember()
        {
            var klass = new TestClass("Object", "java.code.MyClass");

            klass.AddMethod(SupportTypeBuilder.CreateMethod(klass, "Echo", new CodeGenerationOptions(), "uint", false, false, new Parameter("value", "uint", "uint", false)));
            klass.AddField(new TestField("string", "Foo"));

            var context = new CodeGeneratorContext();

            context.ContextTypes.Push(klass);

            Assert.AreEqual("java.code.MyClass", context.GetContextTypeMember());

            context.ContextMethod = klass.Methods.Single();

            Assert.AreEqual("java.code.MyClass.Echo (uint)", context.GetContextTypeMember());

            context.ContextMethod = null;
            context.ContextField  = klass.Fields.Single();

            Assert.AreEqual("java.code.MyClass.Foo", context.GetContextTypeMember());

            context.ContextMethod = klass.Methods.Single();
            context.ContextField  = null;
            context.ContextTypes.Clear();

            Assert.AreEqual("Echo (uint)", context.GetContextTypeMember());
        }
コード例 #5
0
        public void WriteClassInternalBase()
        {
            // Tests the case where a class inherits from Java.Lang.Object and is in the same assembly.
            // Specifically, the internal class_ref field does need the new modifier.
            // - This prevents a CS0108 warning from being generated.

            options.SymbolTable.AddType(new TestClass(null, "Java.Lang.Object"));

            var @class = SupportTypeBuilder.CreateClass("java.code.MyClass", options, "Java.Lang.Object");

            @class.Validate(options, new GenericParameterDefinitionList(), generator.Context);

            // FromXml is set to true when a class is set to true when the api.xml contains an entry for the class.
            // Therefore, if a class's base has FromXml set to true, the class and its base will be in the same C# assembly.
            @class.BaseGen.FromXml = true;

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

            var result = writer.ToString().NormalizeLineEndings();

            Assert.True(result.Contains("internal static new IntPtr class_ref"));
            Assert.False(result.Contains("internal static IntPtr class_ref"));
        }
コード例 #6
0
        public void WriteParameterListCallArgsForInvoker()
        {
            var list = SupportTypeBuilder.CreateParameterList(options);

            generator.WriteParameterListCallArgs(list, string.Empty, true);

            Assert.AreEqual(GetTargetedExpected(nameof(WriteParameterListCallArgsForInvoker)), writer.ToString().NormalizeLineEndings());
        }
コード例 #7
0
        public void WritePropertyStringVariant()
        {
            var @class = SupportTypeBuilder.CreateClassWithProperty("java.lang.Object", "com.mypackage.foo", "MyProperty", "int", options);

            generator.WritePropertyStringVariant(@class.Properties.First(), string.Empty);

            Assert.AreEqual(GetExpected(nameof(WritePropertyStringVariant)), writer.ToString().NormalizeLineEndings());
        }
コード例 #8
0
        public void WriteInterfaceDeclaration()
        {
            var iface = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);

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

            Assert.AreEqual(GetTargetedExpected(nameof(WriteInterfaceDeclaration)), writer.ToString().NormalizeLineEndings());
        }
コード例 #9
0
        public void WriteInterfaceListenerEventWithHandlerArgument()
        {
            var iface = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfaceListenerEvent(iface, string.Empty, "MyName", "MyNameSpec", "MyMethodName", "MyFullDelegateName", true, "MyWrefSuffix", "AddMyName", "RemoveMyName", true);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetExpected(nameof(WriteInterfaceListenerEventWithHandlerArgument)), writer.ToString().NormalizeLineEndings());
        }
コード例 #10
0
        public void WriteInterfaceListenerProperty()
        {
            var iface = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfaceListenerProperty(iface, string.Empty, "MyName", "MyNameSpec", "MyMethodName", "MyConnectorFmt", "MyFullDelegateName");
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetExpected(nameof(WriteInterfaceListenerProperty)), writer.ToString().NormalizeLineEndings());
        }
コード例 #11
0
        public void WriteClassProperties()
        {
            var @class = SupportTypeBuilder.CreateClass("java.code.MyClass", options);

            generator.Context.ContextTypes.Push(@class);
            generator.WriteImplementedProperties(@class.Properties, string.Empty, @class.IsFinal, @class);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteClassProperties)), writer.ToString().NormalizeLineEndings());
        }
コード例 #12
0
        public void WriteClassInvoker()
        {
            var @class = SupportTypeBuilder.CreateClass("java.code.MyClass", options);

            generator.Context.ContextTypes.Push(@class);
            generator.WriteClassInvoker(@class, string.Empty);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteClassInvoker)), writer.ToString().NormalizeLineEndings());
        }
コード例 #13
0
        public void WriteInterfaceExtensionsDeclaration()
        {
            var iface = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);

            options.ContextTypes.Push(iface);
            generator.WriteInterfaceExtensionsDeclaration(iface, string.Empty, "java.code.DeclaringType");
            options.ContextTypes.Pop();

            Assert.AreEqual(GetExpected(nameof(WriteInterfaceExtensionsDeclaration)), writer.ToString().NormalizeLineEndings());
        }
コード例 #14
0
        public void WriteInterfaceProperties()
        {
            var iface = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);

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

            Assert.AreEqual(GetExpected(nameof(WriteInterfaceProperties)), writer.ToString().NormalizeLineEndings());
        }
コード例 #15
0
        public void WriteInterfacePropertyInvokersWithSkips()
        {
            // This test should skip the first Property because members contains the Property
            var iface   = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);
            var members = new HashSet <string> (new [] { iface.Properties [0].Name });

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfacePropertyInvokers(iface, iface.Properties, string.Empty, members);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetExpected(nameof(WriteInterfacePropertyInvokersWithSkips)), writer.ToString().NormalizeLineEndings());
        }
コード例 #16
0
        public void WriteInterfacePropertyInvokers()
        {
            // This test should generate all the properties (members is empty)
            var iface   = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);
            var members = new HashSet <string> ();

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfacePropertyInvokers(iface, iface.Properties, string.Empty, members);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetExpected(nameof(WriteInterfacePropertyInvokers)), writer.ToString().NormalizeLineEndings());
        }
コード例 #17
0
        public void WriteClassPropertyInvokers()
        {
            // This test should generate all the properties (members is empty)
            var @class  = SupportTypeBuilder.CreateClass("java.code.MyClass", options);
            var members = new HashSet <string> ();

            generator.Context.ContextTypes.Push(@class);
            generator.WriteClassPropertyInvokers(@class, @class.Properties, string.Empty, members);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteClassPropertyInvokers)), writer.ToString().NormalizeLineEndings());
        }
コード例 #18
0
        public void WriteClassMethodInvokersWithSkips()
        {
            // This test should skip the first Method because members contains the Method
            var @class  = SupportTypeBuilder.CreateClass("java.code.MyClass", options);
            var members = new HashSet <string> (new [] { @class.Methods [0].Name });

            generator.Context.ContextTypes.Push(@class);
            generator.WriteClassMethodInvokers(@class, @class.Methods, string.Empty, members, null);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(GetTargetedExpected(nameof(WriteClassMethodInvokersWithSkips)), writer.ToString().NormalizeLineEndings());
        }
コード例 #19
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());
        }
コード例 #20
0
        public void WriteInterfaceEventHandlerImplContent()
        {
            var iface    = SupportTypeBuilder.CreateInterface("java.code.IMyInterface", options);
            var handlers = new List <string> ();

            generator.Context.ContextTypes.Push(iface);
            generator.WriteInterfaceEventHandlerImplContent(iface, iface.Methods [0], string.Empty, true, string.Empty, handlers);
            generator.Context.ContextTypes.Pop();

            Assert.AreEqual(1, handlers.Count);
            Assert.AreEqual("GetCountForKey", handlers [0]);
            Assert.AreEqual(GetExpected(nameof(WriteInterfaceEventHandlerImplContent)), writer.ToString().NormalizeLineEndings());
        }
コード例 #21
0
        void TestParameterlessMethods(string name)
        {
            var c = SupportTypeBuilder.CreateClass("java.myClass", options);
            var m = SupportTypeBuilder.CreateMethod(c, name, options);

            // Yes
            Assert.True(c.RequiresNew(m.Name, m));

            // No because > 0 parameters
            m.Parameters.Add(new Parameter("value", "int", "int", false));

            Assert.False(c.RequiresNew(m.Name, m));
        }
コード例 #22
0
        public void HandleAlwaysRequiresNew()
        {
            // The same name as a property always requires new, no matter the parameters
            var c = SupportTypeBuilder.CreateClass("java.myClass", options);
            var m = SupportTypeBuilder.CreateMethod(c, "Handle", options);

            // Yes
            Assert.True(c.RequiresNew(m.Name, m));

            // Yes, even with parameters
            m.Parameters.Add(new Parameter("value", "int", "int", false));

            Assert.True(c.RequiresNew(m.Name, m));
        }
コード例 #23
0
        public void FixProtectedType()
        {
            var klass = CreateSealedClass();

            var type = SupportTypeBuilder.CreateClass("my.example.class.inner", options);

            type.Visibility = "protected";

            klass.NestedTypes.Add(type);

            SealedProtectedFixups.Fixup(new [] { (GenBase)klass }.ToList());

            Assert.AreEqual("private", type.Visibility);
        }
コード例 #24
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);
        }
コード例 #25
0
        public void FixProtectedMethod()
        {
            var klass = CreateSealedClass();

            var method = SupportTypeBuilder.CreateMethod(klass, "ToString", options);

            klass.Methods.Add(method);

            method.Visibility = "protected";
            method.IsOverride = false;

            SealedProtectedFixups.Fixup(new [] { (GenBase)klass }.ToList());

            Assert.AreEqual("private", method.Visibility);
        }
コード例 #26
0
        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());
        }
コード例 #27
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);
        }
コード例 #28
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());
        }
コード例 #29
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());
        }
コード例 #30
0
        public void FixProtectedProperty()
        {
            var klass = CreateSealedClass();

            var method = SupportTypeBuilder.CreateProperty(klass, "Handle", "int", options);

            klass.Properties.Add(method);

            method.Getter.Visibility = "protected";
            method.Getter.IsOverride = false;

            method.Setter.Visibility = "protected";
            method.Setter.IsOverride = false;

            SealedProtectedFixups.Fixup(new [] { (GenBase)klass }.ToList());

            Assert.AreEqual("private", method.Getter.Visibility);
            Assert.AreEqual("private", method.Setter.Visibility);
        }