-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop, Java.Interop.Export] Add JniValueMarshalerAttribute
Mentioned in ff4053c was a feature to complete Issue #8: > Need to permit use of custom attributes on return types, parameter > types, "inline" into marshal methods. Let's do that. :-) The new Java.Interop.JniValueMarshalerAttribute custom attribute can be applied to: * Types: classes, enums, interfaces, structs This allows: [JniValueMarshaler (typeof (MyCustomMarshaler))] public class MySpecialClass /* NOT JavaObject! */ { } public partial class MyCustomMarshaler : JniValueMarshaler<MySpecialClass> { // ... } JniRuntime.JniValueManager.GetValueMarshaler(Type) has been updated to check for the JniValueMarshalerAttribute and, when present, will return a new instance of the specified JniValueMarshaler instance: var marshaler = JniRuntime.CurrentRuntime.ValueManager.GetValueMarshaler (typeof (MySpecialClass)); // marshaler ISA MyCustomMarshaler JniValueMarshalerAttribute can also be applied to: * Method parameters * Method return types Java.Interop.Export has been updated to check for the JniValueMarshalerAttribute custom attribute to marshal parameters and return types, allowing: // e.g. code from an existing library public class ExistingType { } public partial class ExistingTypeValueMarshaler : JniValueMarshaler<ExistingType> { // ... } [JavaCallable] public static [return: JniValueMarshaler (typeof (ExistingTypeValueMarshaler))] ExistingType Foo ([JniValueMarshaler (typeof (ExistingTypeValueMarshaler))] ExistingType value) { return value; } This allows one-off specification or overriding of value marshalers for method parameter and return types, particularly useful if you want to marshal a type that you don't control, and thus can't alter to contain a [JniValueMarshaler] declaration.
- Loading branch information
Showing
11 changed files
with
533 additions
and
63 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
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
29 changes: 29 additions & 0 deletions
29
src/Java.Interop/Java.Interop/JniValueMarshalerAttribute.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,29 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Java.Interop { | ||
|
||
[AttributeUsage (Targets, AllowMultiple=false)] | ||
public class JniValueMarshalerAttribute : Attribute { | ||
|
||
const AttributeTargets Targets = | ||
AttributeTargets.Class | AttributeTargets.Enum | | ||
AttributeTargets.Interface | AttributeTargets.Struct | | ||
AttributeTargets.Parameter | AttributeTargets.ReturnValue; | ||
|
||
public JniValueMarshalerAttribute (Type marshalerType) | ||
{ | ||
if (marshalerType == null) | ||
throw new ArgumentNullException (nameof (marshalerType)); | ||
if (!typeof (JniValueMarshaler).GetTypeInfo ().IsAssignableFrom (marshalerType.GetTypeInfo ())) | ||
throw new ArgumentException ( | ||
string.Format ("`{0}` must inherit from JniValueMarshaler!", marshalerType.FullName), | ||
nameof (marshalerType)); | ||
|
||
MarshalerType = marshalerType; | ||
} | ||
|
||
public Type MarshalerType {get;} | ||
} | ||
} | ||
|
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
Oops, something went wrong.