Skip to content

Commit fb94d59

Browse files
authored
[Java.Interop.Tools.JavaCallableWrappers] Collect overriden methods (#985)
We are prototyping a new marshal method generator in xamarin-android which emits native functions with [JNI native method names][0]. This would remove the need for `Runtime.register()` invocations from Java Callable Wrappers and all the related Reflection-heavy marshal method registration code; see 4787e01 for some details for how the current Reflection approach works. In order to emit native functions which have the correct names, we need to know additional information, such as the package name and type name of the Java `native` method, method signature, etc. Update `JavaCallableWrapperGenerator` to collect this additional information, making it available via a new `JavaCallableWrapperGenerator.OverriddenMethods` property. [0]: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#resolving_native_method_names
1 parent 3fcce74 commit fb94d59

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@ public enum JavaPeerStyle {
2525
JavaInterop1,
2626
}
2727

28+
public class OverriddenMethodDescriptor
29+
{
30+
static readonly char[] methodDescSplitChars = new char[] { ':' };
31+
32+
public string JavaPackageName { get; }
33+
public string NativeName { get; }
34+
public string JniSignature { get; }
35+
public string Connector { get; }
36+
public string ManagedTypeName { get; }
37+
38+
public OverriddenMethodDescriptor (string javaPackageName, string methodDescription)
39+
{
40+
JavaPackageName = javaPackageName;
41+
string[] parts = methodDescription.Split (methodDescSplitChars, 4);
42+
43+
if (parts.Length < 2) {
44+
throw new InvalidOperationException ($"Unexpected format for method description. Expected at least 2 parts, got {parts.Length} from: '{methodDescription}'");
45+
}
46+
47+
NativeName = parts[0];
48+
JniSignature = parts[1];
49+
if (parts.Length > 2) {
50+
Connector = parts[2];
51+
if (parts.Length > 3) {
52+
ManagedTypeName = parts[3];
53+
}
54+
}
55+
}
56+
}
57+
2858
public class JavaCallableWrapperGenerator {
2959

3060
class JavaFieldInfo {
@@ -59,6 +89,7 @@ public string GetJavaAccess ()
5989
List<Signature> methods = new List<Signature> ();
6090
List<Signature> ctors = new List<Signature> ();
6191
List<JavaCallableWrapperGenerator> children;
92+
List<OverriddenMethodDescriptor> overriddenMethodDescriptors;
6293
readonly IMetadataResolver cache;
6394

6495
[Obsolete ("Use the TypeDefinitionCache overload for better performance.")]
@@ -80,6 +111,7 @@ public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[
80111
}
81112
}
82113

114+
public IList<OverriddenMethodDescriptor> OverriddenMethodDescriptors => overriddenMethodDescriptors;
83115
public string ApplicationJavaClass { get; set; }
84116
public JavaPeerStyle CodeGenerationTarget { get; set; }
85117

@@ -89,7 +121,7 @@ public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[
89121

90122
/// <summary>
91123
/// The Java source code to be included in Instrumentation.onCreate
92-
///
124+
///
93125
/// Originally came from MonoRuntimeProvider.java delimited by:
94126
/// // Mono Runtime Initialization {{{
95127
/// // }}}
@@ -497,6 +529,7 @@ string GetManagedParameters (MethodDefinition ctor, string outerType)
497529

498530
public void Generate (TextWriter writer)
499531
{
532+
overriddenMethodDescriptors = new List<OverriddenMethodDescriptor> ();
500533
if (!string.IsNullOrEmpty (package)) {
501534
writer.WriteLine ("package " + package + ";");
502535
writer.WriteLine ();
@@ -530,6 +563,17 @@ public void Generate (TextWriter writer)
530563
}
531564

532565
GenerateFooter (writer);
566+
567+
string javaTypeName = $"{package}.{name}";
568+
AddOverridenMethods (methods);
569+
AddOverridenMethods (ctors);
570+
571+
void AddOverridenMethods (List<Signature> list)
572+
{
573+
foreach (Signature sig in list) {
574+
overriddenMethodDescriptors.Add (new OverriddenMethodDescriptor (javaTypeName, sig.Method));
575+
}
576+
}
533577
}
534578

535579
public void Generate (string outputPath)
@@ -958,5 +1002,3 @@ public string GetDestinationPath (string outputPath)
9581002
}
9591003
}
9601004
}
961-
962-

0 commit comments

Comments
 (0)