Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/generator-Tests/Unit-Tests/SealedProtectedFixupsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Linq;
using System.Xml.Linq;
using Java.Interop.Tools.Generator.Transformation;
using MonoDroid.Generation;
using NUnit.Framework;

namespace generatortests
{
[TestFixture]
public class SealedProtectedFixupsTests
{
private CodeGenerationOptions options = new CodeGenerationOptions ();

[Test]
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);
}

[Test]
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);
}

[Test]
public void FixProtectedField ()
{
var klass = CreateSealedClass ();

var field = new Field {
Name = "MyConstant",
TypeName = "int",
Visibility = "protected"
};

klass.Fields.Add (field);

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

Assert.AreEqual ("private", field.Visibility);
}

private ClassGen CreateSealedClass ()
{
var klass = SupportTypeBuilder.CreateClass ("my.example.class", options);
klass.IsFinal = true;
return klass;
}
}
}
2 changes: 2 additions & 0 deletions tools/generator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
foreach (GenBase gen in gens)
gen.FixupExplicitImplementation ();

SealedProtectedFixups.Fixup (gens);

GenerateAnnotationAttributes (gens, annotations_zips);

//SymbolTable.Dump ();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MonoDroid.Generation;

namespace Java.Interop.Tools.Generator.Transformation
{
public static class SealedProtectedFixups
{
public static void Fixup (List<GenBase> gens)
{
foreach (var c in gens.OfType<ClassGen> ().Where (c => c.IsFinal)) {
foreach (var m in c.Methods.Where (m => m.Visibility == "protected" && !m.IsOverride))
m.Visibility = "private";

foreach (var p in c.Properties.Where (p => p.Getter?.Visibility == "protected" && !p.Getter.IsOverride))
p.Getter.Visibility = "private";

foreach (var p in c.Properties.Where (p => p.Setter?.Visibility == "protected" && !p.Setter.IsOverride))
p.Setter.Visibility = "private";

foreach (var p in c.Fields.Where (f => f.Visibility == "protected"))
p.Visibility = "private";
}
}
}
}
1 change: 1 addition & 0 deletions tools/generator/generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="Java.Interop.Tools.Generator.ObjectModel\NamespaceMapping.cs" />
<Compile Include="Java.Interop.Tools.Generator.ObjectModel\Parameter.cs" />
<Compile Include="Java.Interop.Tools.Generator.ObjectModel\ParameterList.cs" />
<Compile Include="Java.Interop.Tools.Generator.Transformation\SealedProtectedFixups.cs" />
<Compile Include="Java.Interop.Tools.Generator.Transformation\KotlinFixups.cs" />
<Compile Include="Java.Interop.Tools.Generator.Transformation\Parser.cs" />
<Compile Include="Utilities\AncestorDescendantCache.cs" />
Expand Down