Skip to content

Commit 6608c59

Browse files
mbloweyjpobst
authored andcommitted
[generator] Fix generated code that caused CS0169 warnings. (#625)
* Modified the generator tool so that it no longer adds a 'new' modifier to internal fields (like class_ref) when the class's base is in an external assembly. - This prevents a CS0109 warnings during binding generation. - Updated expected generator output files for existing tests. - Added two new tests that verify the expected output when the base class is in an external assembly and when the base class is in the same assembly as the derived class. (cherry picked from commit ce8dc40)
1 parent 0537eb1 commit 6608c59

File tree

11 files changed

+63
-20
lines changed

11 files changed

+63
-20
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypeMethodsClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypePropertiesClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedTypeMethodsClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedTypePropertiesClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteClass.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public partial class MyClass {
44

55
static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/MyClass", typeof (MyClass));
6-
internal static new IntPtr class_ref {
6+
internal static IntPtr class_ref {
77
get {
88
return _members.JniPeerType.PeerReference.Handle;
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XamarinAndroid/WriteClass.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
[global::Android.Runtime.Register ("java/code/MyClass", DoNotGenerateAcw=true)]
33
public partial class MyClass {
44

5-
internal static new IntPtr java_class_handle;
6-
internal static new IntPtr class_ref {
5+
internal static IntPtr java_class_handle;
6+
internal static IntPtr class_ref {
77
get {
88
return JNIEnv.FindClass ("java/code/MyClass", ref java_class_handle);
99
}

tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,5 +1016,51 @@ public void WritePropertyExplicitInterfaceParameterName ()
10161016
var result = writer.ToString ().NormalizeLineEndings ();
10171017
Assert.False (result.Contains ("p0"));
10181018
}
1019+
1020+
[Test]
1021+
public void WriteClassExternalBase ()
1022+
{
1023+
// Tests the case where a class inherits from a class that is not in the same assembly.
1024+
// Specifically, the internal class_ref field does NOT need the new modifier.
1025+
// - This prevents a CS0109 warning from being generated.
1026+
1027+
options.SymbolTable.AddType (new TestClass (null, "Java.Lang.Object"));
1028+
1029+
var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options, "Java.Lang.Object");
1030+
@class.Validate (options, new GenericParameterDefinitionList (), generator.Context);
1031+
1032+
generator.Context.ContextTypes.Push (@class);
1033+
generator.WriteClass (@class, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
1034+
generator.Context.ContextTypes.Pop ();
1035+
1036+
var result = writer.ToString ().NormalizeLineEndings ();
1037+
Assert.True (result.Contains ("internal static IntPtr class_ref"));
1038+
Assert.False (result.Contains ("internal static new IntPtr class_ref"));
1039+
}
1040+
1041+
[Test]
1042+
public void WriteClassInternalBase ()
1043+
{
1044+
// Tests the case where a class inherits from Java.Lang.Object and is in the same assembly.
1045+
// Specifically, the internal class_ref field does need the new modifier.
1046+
// - This prevents a CS0108 warning from being generated.
1047+
1048+
options.SymbolTable.AddType (new TestClass (null, "Java.Lang.Object"));
1049+
1050+
var @class = SupportTypeBuilder.CreateClass ("java.code.MyClass", options, "Java.Lang.Object");
1051+
@class.Validate (options, new GenericParameterDefinitionList (), generator.Context);
1052+
1053+
// FromXml is set to true when a class is set to true when the api.xml contains an entry for the class.
1054+
// Therefore, if a class's base has FromXml set to true, the class and its base will be in the same C# assembly.
1055+
@class.BaseGen.FromXml = true;
1056+
1057+
generator.Context.ContextTypes.Push (@class);
1058+
generator.WriteClass (@class, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
1059+
generator.Context.ContextTypes.Pop ();
1060+
1061+
var result = writer.ToString ().NormalizeLineEndings ();
1062+
Assert.True (result.Contains ("internal static new IntPtr class_ref"));
1063+
Assert.False (result.Contains ("internal static IntPtr class_ref"));
1064+
}
10191065
}
10201066
}

tests/generator-Tests/Unit-Tests/SupportTypes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ public TestInterface (string argsType, string javaName) : base (new TestBaseSupp
215215

216216
static class SupportTypeBuilder
217217
{
218-
public static TestClass CreateClass (string className, CodeGenerationOptions options)
218+
public static TestClass CreateClass (string className, CodeGenerationOptions options, string baseClass = "Object")
219219
{
220-
var @class = new TestClass ("Object", className);
220+
var @class = new TestClass (baseClass, className);
221221

222222
var ctor_name = className.Contains ('.') ? className.Substring (className.LastIndexOf ('.')) : className;
223223
@class.Ctors.Add (CreateConstructor (@class, ctor_name, options));

tools/generator/Java.Interop.Tools.Generator.CodeGeneration/CodeGenerator.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,12 @@ public void WriteClass (ClassGen @class, string indent, GenerationInfo gen_info)
126126
writer.WriteLine ();
127127
}
128128

129-
bool requireNew = @class.InheritsObject;
130-
if (!requireNew) {
131-
for (var bg = @class.BaseGen; bg != null && bg is ClassGen classGen && classGen.FromXml; bg = bg.BaseGen) {
132-
if (bg.InheritsObject) {
133-
requireNew = true;
134-
break;
135-
}
136-
}
137-
}
129+
// @class.InheritsObject is true unless @class refers to java.lang.Object or java.lang.Throwable. (see ClassGen constructor)
130+
// If @class's base class is defined in the same api.xml file, then it requires the new keyword to overshadow the internal
131+
// members of its baseclass since the two classes will be defined in the same assembly. If the base class is not from the
132+
// same api.xml file, the new keyword is not needed because the internal access modifier already prevents it from being seen.
133+
bool baseFromSameAssembly = @class?.BaseGen?.FromXml ?? false;
134+
bool requireNew = @class.InheritsObject && baseFromSameAssembly;
138135
WriteClassHandle (@class, indent, requireNew);
139136

140137
WriteClassConstructors (@class, indent + "\t");

0 commit comments

Comments
 (0)