Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Created February 7, 2024 02:51
Show Gist options
  • Save jonpryor/417294863fc9ff17c0521c04a607829e to your computer and use it in GitHub Desktop.
Save jonpryor/417294863fc9ff17c0521c04a607829e to your computer and use it in GitHub Desktop.
Fix those pesky Expression.Parameter() warnings!
diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs
index 58898be5..cada2bca 100644
--- a/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs
+++ b/src/Java.Interop/Java.Interop/JniRuntime.JniMarshalMemberBuilder.cs
@@ -9,6 +9,8 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
+using Java.Interop.Expressions;
+
namespace Java.Interop {
partial class JniRuntime {
@@ -175,6 +177,21 @@ namespace Java.Interop {
methodParameters [0].ParameterType == typeof (IntPtr) &&
methodParameters [1].ParameterType == typeof (IntPtr);
}
+
+ internal virtual JniExpressionValueMarshaler GetExpressionValueMarshaler (Type type)
+ {
+ throw new NotSupportedException ();
+ }
+
+ internal virtual Expression DefaultReturnObjectReferenceToJni (JniValueMarshaler marshaler, JniValueMarshalerContext context, string? namePrefix, Expression sourceValue)
+ {
+ throw new NotSupportedException ();
+ }
+
+ internal virtual Expression DefaultDisposeObjectReference (Expression sourceValue)
+ {
+ throw new NotSupportedException ();
+ }
}
}
diff --git a/src/Java.Interop/Java.Interop/JniValueMarshaler.cs b/src/Java.Interop/Java.Interop/JniValueMarshaler.cs
index d3351c35..0989d30c 100644
--- a/src/Java.Interop/Java.Interop/JniValueMarshaler.cs
+++ b/src/Java.Interop/Java.Interop/JniValueMarshaler.cs
@@ -38,6 +38,13 @@ namespace Java.Interop.Expressions {
ValueManager = vm;
}
}
+
+ internal abstract class JniExpressionValueMarshaler {
+
+ public abstract Expression CreateParameterToManagedExpression (JniValueMarshaler marshaler, JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type? targetType);
+ public abstract Expression CreateReturnValueFromManagedExpression (JniValueMarshaler marshaler, JniValueMarshalerContext context, ParameterExpression sourceValue);
+ public abstract Expression CreateParameterFromManagedExpression (JniValueMarshaler marshaler, JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize);
+ }
}
namespace Java.Interop {
@@ -147,72 +154,40 @@ namespace Java.Interop {
public virtual Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize = 0, Type? targetType = null)
{
- Func<IntPtr, Type, object?> m = CreateValue;
-
- var self = CreateSelf (context, sourceValue);
-
- var call = Expression.Call (self, m.GetMethodInfo (), sourceValue, Expression.Constant (targetType, typeof (Type)));
- return targetType == null
- ? (Expression) call
- : Expression.Convert (call, targetType);
- }
-
- Expression CreateSelf (JniValueMarshalerContext context, ParameterExpression sourceValue)
- {
- var self = Expression.Variable (GetType (), sourceValue.Name + "_marshaler");
- context.LocalVariables.Add (self);
- context.CreationStatements.Add (Expression.Assign (self, Expression.New (GetType ())));
- return self;
+ return JniEnvironment.Runtime.MarshalMemberBuilder?.GetExpressionValueMarshaler (MarshalType)
+ ?.CreateParameterToManagedExpression (
+ this, context, sourceValue, synchronize, targetType
+ ) ?? throw new NotSupportedException ("No MarshalMemberBuilder support!");
}
public virtual Expression CreateReturnValueFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue)
{
- CreateParameterFromManagedExpression (context, sourceValue, 0);
- var s = context.LocalVariables [sourceValue.Name + "_state"];
- return ReturnObjectReferenceToJni (context, sourceValue.Name, Expression.Property (s, "ReferenceValue"));
+ return JniEnvironment.Runtime.MarshalMemberBuilder?.GetExpressionValueMarshaler (MarshalType)
+ ?.CreateReturnValueFromManagedExpression (
+ this, context, sourceValue
+ ) ?? throw new NotSupportedException ("No MarshalMemberBuilder support!");
}
protected Expression ReturnObjectReferenceToJni (JniValueMarshalerContext context, string? namePrefix, Expression sourceValue)
{
- Func<JniObjectReference, IntPtr> m = JniEnvironment.References.NewReturnToJniRef;
- var r = Expression.Variable (MarshalType, namePrefix + "_rtn");
- if (context == null)
- throw new ArgumentNullException (nameof (context));
- context.LocalVariables.Add (r);
- context.CreationStatements.Add (
- Expression.Assign (r,
- Expression.Call (m.GetMethodInfo (), sourceValue)));
- return r;
+ return JniEnvironment.Runtime.MarshalMemberBuilder?.DefaultReturnObjectReferenceToJni (
+ this, context, namePrefix, sourceValue
+ ) ?? throw new NotSupportedException ("No MarshalMemberBuilder support!");
}
- delegate void DestroyArgumentStateCb (object value, ref JniValueMarshalerState state, ParameterAttributes synchronize);
public virtual Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
{
- Func<object, ParameterAttributes, JniValueMarshalerState> c = CreateArgumentState;
- DestroyArgumentStateCb d = DestroyArgumentState;
-
- var self = CreateSelf (context, sourceValue);
- var state = Expression.Variable (typeof(JniValueMarshalerState), sourceValue.Name + "_state");
- var ret = Expression.Variable (MarshalType, sourceValue.Name + "_val");
-
- context.LocalVariables.Add (state);
- context.LocalVariables.Add (ret);
- context.CreationStatements.Add (Expression.Assign (state, Expression.Call (self, c.GetMethodInfo (), Expression.Convert (sourceValue, typeof (object)), Expression.Constant (synchronize, typeof (ParameterAttributes)))));
- context.CreationStatements.Add (
- Expression.Assign (ret,
- Expression.Property (
- Expression.Property (state, "ReferenceValue"),
- "Handle")));
- context.CleanupStatements.Add (Expression.Call (self, d.GetMethodInfo (), Expression.Convert (sourceValue, typeof (object)), state, Expression.Constant (synchronize)));
-
- return ret;
+ return JniEnvironment.Runtime.MarshalMemberBuilder?.GetExpressionValueMarshaler (MarshalType)
+ ?.CreateParameterFromManagedExpression (
+ this, context, sourceValue, synchronize
+ ) ?? throw new NotSupportedException ("No MarshalMemberBuilder support!");
}
- delegate void DisposeObjRef (ref JniObjectReference r);
protected static Expression DisposeObjectReference (Expression sourceValue)
{
- DisposeObjRef m = JniObjectReference.Dispose;
- return Expression.Call (m.GetMethodInfo (), sourceValue);
+ return JniEnvironment.Runtime.MarshalMemberBuilder?.DefaultDisposeObjectReference (
+ sourceValue
+ ) ?? throw new NotSupportedException ("No MarshalMemberBuilder support!");
}
}
diff --git a/src/Java.Interop/Properties/AssemblyInfo.cs b/src/Java.Interop/Properties/AssemblyInfo.cs
index e394f102..b5f34206 100644
--- a/src/Java.Interop/Properties/AssemblyInfo.cs
+++ b/src/Java.Interop/Properties/AssemblyInfo.cs
@@ -8,6 +8,13 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark ("Microsoft Corporation")]
[assembly: AssemblyMetadata ("IsTrimmable", "True")]
+[assembly: InternalsVisibleTo (
+ "Java.Interop.Export, PublicKey=" +
+ "0024000004800000940000000602000000240000525341310004000011000000438ac2a5acfbf1" +
+ "6cbd2b2b47a62762f273df9cb2795ceccdf77d10bf508e69e7a362ea7a45455bbf3ac955e1f2e2" +
+ "814f144e5d817efc4c6502cc012df310783348304e3ae38573c6d658c234025821fda87a0be8a0" +
+ "d504df564e2c93b2b878925f42503e9d54dfef9f9586d9e6f38a305769587b1de01f6c0410328b" +
+ "2c9733db")]
[assembly: InternalsVisibleTo (
"Java.Interop.GenericMarshaler, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000011000000438ac2a5acfbf1" +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment