Skip to content

Commit 5b18729

Browse files
jonathanpeppersjonpryor
authored andcommitted
[generator] fix C# compiler warning in every Invoker (#217)
Context: https://github.com/xamarin/AndroidSupportComponents When compiling the Xamarin support libraries from source, its binding projects generate thousands of warnings. Most of these warnings are noise that could be fixed, and `generator` is just overdue for some improvement. My first steps to improve this: - By default `generator-Tests` should fail if there is a C# compiler warning - Add an `AllowWarnings` option for `BaseGeneratorTest`, since there are a few tests that generate C# compiler warnings that aren’t addressed yet. The hope is this feature could be dropped from the tests eventually. - Fix the most common warning: `Invoker` types have a warning of a missing `new` keyword on the generated `class_ref` field Since `Java.Lang.Object` has a static `class_ref` property, all `Invoker` types will produce this warning. An `Invoker` is generated for any `interface` or `abstract` class, so you can see how a large binding project could run into this warning many times.
1 parent e1ad506 commit 5b18729

19 files changed

+23
-20
lines changed

tools/generator/InterfaceGen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void GenerateInvoker (StreamWriter sw, string indent, CodeGenerationOptions opt)
224224
sw.WriteLine ("{0}internal class {1}Invoker : global::Java.Lang.Object, {1} {{", indent, Name);
225225
sw.WriteLine ();
226226
opt.CodeGenerator.WriteInterfaceInvokerHandle (this, sw, indent + "\t", opt, Name + "Invoker");
227-
sw.WriteLine ("{0}\tIntPtr class_ref;", indent);
227+
sw.WriteLine ("{0}\tnew IntPtr class_ref;", indent);
228228
sw.WriteLine ();
229229
sw.WriteLine ("{0}\tpublic static {1} GetObject (IntPtr handle, JniHandleOwnership transfer)", indent, Name);
230230
sw.WriteLine ("{0}\t{{", indent);

tools/generator/Tests/BaseGeneratorTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void Setup ()
3030
protected CodeGeneratorOptions Options = null;
3131
protected Assembly BuiltAssembly = null;
3232
protected List<string> AdditionalSourceDirectories;
33+
protected bool AllowWarnings;
3334

3435
public void Execute ()
3536
{
@@ -41,7 +42,7 @@ public void Execute ()
4142
bool hasErrors;
4243
string compilerOutput;
4344
BuiltAssembly = Compiler.Compile (Options, "Mono.Android", AdditionalSourceDirectories,
44-
out hasErrors, out compilerOutput);
45+
out hasErrors, out compilerOutput, AllowWarnings);
4546
Assert.AreEqual (false, hasErrors, compilerOutput);
4647
Assert.IsNotNull (BuiltAssembly);
4748
}

tools/generator/Tests/Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static CodeDomProvider GetCodeDomProvider ()
3333

3434
public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions options,
3535
string assemblyFileName, IEnumerable<string> AdditionalSourceDirectories,
36-
out bool hasErrors, out string output)
36+
out bool hasErrors, out string output, bool allowWarnings)
3737
{
3838
var generatedCodePath = options.ManagedCallableWrapperSourceOutputDirectory;
3939
var sourceFiles = Directory.EnumerateFiles (generatedCodePath, "*.cs",
@@ -73,7 +73,7 @@ public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions opti
7373
hasErrors = false;
7474

7575
foreach (CompilerError message in results.Errors) {
76-
hasErrors = hasErrors || (!message.IsWarning);
76+
hasErrors |= !message.IsWarning || !allowWarnings;
7777
}
7878
output = string.Join (Environment.NewLine, results.Output.Cast<string> ());
7979

tools/generator/Tests/Interfaces.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Interfaces : BaseGeneratorTest
99
[Test]
1010
public void Generated_OK ()
1111
{
12+
AllowWarnings = true;
1213
RunAllTargets (
1314
outputRelativePath: "TestInterface",
1415
apiDescriptionFile: "expected/TestInterface/TestInterface.xml",

tools/generator/Tests/NormalMethods.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class NormalMethods : BaseGeneratorTest
99
[Test]
1010
public void GeneratedOK ()
1111
{
12+
AllowWarnings = true;
1213
RunAllTargets (
1314
outputRelativePath: "NormalMethods",
1415
apiDescriptionFile: "expected/NormalMethods/NormalMethods.xml",

tools/generator/Tests/expected.ji/Adapters/Xamarin.Test.IAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override IntPtr ThresholdClass {
3232
get { return _members.ManagedPeerType; }
3333
}
3434

35-
IntPtr class_ref;
35+
new IntPtr class_ref;
3636

3737
public static IAdapter GetObject (IntPtr handle, JniHandleOwnership transfer)
3838
{

tools/generator/Tests/expected.ji/Adapters/Xamarin.Test.ISpinnerAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override IntPtr ThresholdClass {
3232
get { return _members.ManagedPeerType; }
3333
}
3434

35-
IntPtr class_ref;
35+
new IntPtr class_ref;
3636

3737
public static ISpinnerAdapter GetObject (IntPtr handle, JniHandleOwnership transfer)
3838
{

tools/generator/Tests/expected.ji/GenericArguments/Com.Google.Android.Exoplayer.Drm.IExoMediaDrm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override IntPtr ThresholdClass {
3737
get { return _members.ManagedPeerType; }
3838
}
3939

40-
IntPtr class_ref;
40+
new IntPtr class_ref;
4141

4242
public static IExoMediaDrmOnEventListener GetObject (IntPtr handle, JniHandleOwnership transfer)
4343
{
@@ -217,7 +217,7 @@ protected override IntPtr ThresholdClass {
217217
get { return _members.ManagedPeerType; }
218218
}
219219

220-
IntPtr class_ref;
220+
new IntPtr class_ref;
221221

222222
public static IExoMediaDrm GetObject (IntPtr handle, JniHandleOwnership transfer)
223223
{

tools/generator/Tests/expected.ji/NestedTypes/Xamarin.Test.NotificationCompatBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override IntPtr ThresholdClass {
4444
get { return _members.ManagedPeerType; }
4545
}
4646

47-
IntPtr class_ref;
47+
new IntPtr class_ref;
4848

4949
public static IFactory GetObject (IntPtr handle, JniHandleOwnership transfer)
5050
{

tools/generator/Tests/expected.ji/TestInterface/Test.ME.IGenericInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override IntPtr ThresholdClass {
3737
get { return _members.ManagedPeerType; }
3838
}
3939

40-
IntPtr class_ref;
40+
new IntPtr class_ref;
4141

4242
public static IGenericInterface GetObject (IntPtr handle, JniHandleOwnership transfer)
4343
{

0 commit comments

Comments
 (0)