-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] MSBuild support for C#8 DIM emission (#…
…3533) Context: dotnet/java-interop#25 Use [C#8 Default Interface Members][0] to improve bindings of Java 8 interface constructs, such as [default interface methods][1] and static methods. This allows the following Java type: // Java publc interface Example { void requiredMethod(); default void optionalMethod() { } public static void staticMethod() { } } to be bound as: // C# public partial interface IExample : IJavaPeerable { [Register("requiredMethod", "()V", …)] void RequiredMethod (); [Register("optionalMethod", "()V", …)] public void OptionalMethod() { /* … */ } [Register("staticMethod", "()V", …)] public static void StaticMethod() { /* … */ } } To enable use of C#8 default interface member generation, the following two MSBuild properties must be set: * `$(LangVersion)`, which controls the C# compiler [language syntax version][2], must be set to `preview`. * `$(_EnableInterfaceMembers)` must be set to `True`. For example: <PropertyGroup> <LangVersion>preview</LangVersion> <_EnableInterfaceMembers>True</_EnableInterfaceMembers> </PropertyGroup> In the future, this will key entirely off of the `$(LangVersion)` property and the `$(_EnableInterfaceMembers)` property will be removed. [0]: https://github.com/dotnet/csharplang/blob/f7952cdddf85316a4beec493a0ecc14fcb3241c8/proposals/csharp-8.0/default-interface-methods.md [1]: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html [2]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/langversion-compiler-option
- Loading branch information
Showing
13 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/DimBindingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using Com.Xamarin.Android; | ||
using Java.Lang; | ||
using NUnit.Framework; | ||
|
||
namespace Xamarin.Android.JcwGenTests | ||
{ | ||
[TestFixture] | ||
public class DimTest | ||
{ | ||
[Test] | ||
public void TestDefaultInterfaceMethods () | ||
{ | ||
var empty = new EmptyOverrideClass (); | ||
var iface = empty as IDefaultMethodsInterface; | ||
|
||
Assert.AreEqual (0, iface.Foo ()); | ||
Assert.AreEqual (2, iface.Bar); | ||
Assert.DoesNotThrow (() => iface.Bar = 5); | ||
|
||
Assert.AreEqual (0, iface.InvokeFoo ()); | ||
|
||
Assert.Throws<UnsupportedOperationException> (() => iface.ToImplement ()); | ||
} | ||
|
||
[Test] | ||
public void TestOverriddenDefaultInterfaceMethods () | ||
{ | ||
var over = new ImplementedOverrideClass (); | ||
var iface = over as IDefaultMethodsInterface; | ||
|
||
Assert.AreEqual (6, over.Foo ()); | ||
Assert.AreEqual (100, over.Bar); | ||
Assert.DoesNotThrow (() => over.Bar = 5); | ||
|
||
Assert.AreEqual (6, iface.InvokeFoo ()); | ||
} | ||
|
||
[Test] | ||
public void TestManagedEmptyDefaultInterfaceMethods () | ||
{ | ||
// Test using empty C# implementing interface | ||
var empty = new ManagedEmptyDefault (); | ||
var iface = empty as IDefaultMethodsInterface; | ||
|
||
Assert.AreEqual (0, iface.Foo ()); | ||
|
||
Assert.AreEqual (0, iface.InvokeFoo ()); | ||
} | ||
|
||
[Test] | ||
public void TestManagedOverriddenDefaultInterfaceMethods () | ||
{ | ||
// Test using method overridden in C# | ||
var over = new ManagedOverrideDefault (); | ||
var iface = over as IDefaultMethodsInterface; | ||
|
||
Assert.AreEqual (15, over.Foo ()); | ||
Assert.AreEqual (15, iface.Foo ()); | ||
|
||
Assert.AreEqual (15, iface.InvokeFoo ()); | ||
} | ||
|
||
[Test] | ||
public void TestStaticMethods () | ||
{ | ||
Assert.AreEqual (10, IStaticMethodsInterface.Foo ()); | ||
|
||
Assert.AreEqual (3, IStaticMethodsInterface.Value); | ||
Assert.DoesNotThrow (() => IStaticMethodsInterface.Value = 5); | ||
} | ||
|
||
[Test] | ||
public void TestChainedDefaultInterfaceMethods () | ||
{ | ||
var over = new ImplementedChainOverrideClass (); | ||
var iface = over as IDefaultMethodsInterface; | ||
|
||
Assert.AreEqual (6, over.Foo ()); | ||
Assert.AreEqual (100, over.Bar); | ||
Assert.DoesNotThrow (() => over.Bar = 5); | ||
|
||
Assert.AreEqual (6, iface.InvokeFoo ()); | ||
} | ||
|
||
class ManagedEmptyDefault : Java.Lang.Object, IDefaultMethodsInterface | ||
{ | ||
} | ||
|
||
class ManagedOverrideDefault : Java.Lang.Object, IDefaultMethodsInterface | ||
{ | ||
public int Foo () => 15; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Properties/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...inding/Xamarin.Android.McwGen-Tests/java/com/xamarin/android/DefaultMethodsInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.xamarin.android; | ||
|
||
public interface DefaultMethodsInterface | ||
{ | ||
default int foo () { return 0; } | ||
default int getBar () { return 2; } | ||
default void setBar (int value) { } | ||
default int toImplement () { throw new UnsupportedOperationException (); } | ||
default int invokeFoo () { return foo (); } | ||
} |
5 changes: 5 additions & 0 deletions
5
...Gen-Binding/Xamarin.Android.McwGen-Tests/java/com/xamarin/android/EmptyOverrideClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.xamarin.android; | ||
|
||
public class EmptyOverrideClass implements DefaultMethodsInterface | ||
{ | ||
} |
18 changes: 18 additions & 0 deletions
18
.../Xamarin.Android.McwGen-Tests/java/com/xamarin/android/ImplementedChainOverrideClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.xamarin.android; | ||
|
||
public class ImplementedChainOverrideClass extends EmptyOverrideClass | ||
{ | ||
@Override | ||
public int foo () { | ||
return 6; | ||
} | ||
|
||
@Override | ||
public int getBar () { | ||
return 100; | ||
} | ||
|
||
@Override | ||
public void setBar (int value) { | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nding/Xamarin.Android.McwGen-Tests/java/com/xamarin/android/ImplementedOverrideClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.xamarin.android; | ||
|
||
public class ImplementedOverrideClass implements DefaultMethodsInterface | ||
{ | ||
@Override | ||
public int foo () { | ||
return 6; | ||
} | ||
|
||
@Override | ||
public int getBar () { | ||
return 100; | ||
} | ||
|
||
@Override | ||
public void setBar (int value) { | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...Binding/Xamarin.Android.McwGen-Tests/java/com/xamarin/android/StaticMethodsInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.xamarin.android; | ||
|
||
public interface StaticMethodsInterface | ||
{ | ||
static int foo () { return 10; } | ||
static int getValue () { return 3; } | ||
static void setValue (int value) { } | ||
} |