Skip to content

[generator] Fix generated code that caused CS0169 warnings. #625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new JniPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class MyClass {

static readonly JniPeerMembers _members = new XAPeerMembers ("java/code/MyClass", typeof (MyClass));
internal static new IntPtr class_ref {
internal static IntPtr class_ref {
get {
return _members.JniPeerType.PeerReference.Handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[global::Android.Runtime.Register ("java/code/MyClass", DoNotGenerateAcw=true)]
public partial class MyClass {

internal static new IntPtr java_class_handle;
internal static new IntPtr class_ref {
internal static IntPtr java_class_handle;
internal static IntPtr class_ref {
get {
return JNIEnv.FindClass ("java/code/MyClass", ref java_class_handle);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,5 +1016,51 @@ public void WritePropertyExplicitInterfaceParameterName ()
var result = writer.ToString ().NormalizeLineEndings ();
Assert.False (result.Contains ("p0"));
}

[Test]
public void WriteClassExternalBase ()
{
// Tests the case where a class inherits from a class that is not in the same assembly.
// Specifically, the internal class_ref field does NOT need the new modifier.
// - This prevents a CS0109 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);

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 IntPtr class_ref"));
Assert.False (result.Contains ("internal static new IntPtr class_ref"));
}

[Test]
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"));
}
}
}
4 changes: 2 additions & 2 deletions tests/generator-Tests/Unit-Tests/SupportTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ public TestInterface (string argsType, string javaName) : base (new TestBaseSupp

static class SupportTypeBuilder
{
public static TestClass CreateClass (string className, CodeGenerationOptions options)
public static TestClass CreateClass (string className, CodeGenerationOptions options, string baseClass = "Object")
{
var @class = new TestClass ("Object", className);
var @class = new TestClass (baseClass, className);

var ctor_name = className.Contains ('.') ? className.Substring (className.LastIndexOf ('.')) : className;
@class.Ctors.Add (CreateConstructor (@class, ctor_name, options));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,12 @@ public void WriteClass (ClassGen @class, string indent, GenerationInfo gen_info)
writer.WriteLine ();
}

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

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