Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

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

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

‎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+
publicclassOverriddenMethodDescriptor
29+
{
30+
staticreadonlychar[]methodDescSplitChars=newchar[]{':'};
31+
32+
publicstringJavaPackageName{get;}
33+
publicstringNativeName{get;}
34+
publicstringJniSignature{get;}
35+
publicstringConnector{get;}
36+
publicstringManagedTypeName{get;}
37+
38+
publicOverriddenMethodDescriptor(stringjavaPackageName,stringmethodDescription)
39+
{
40+
JavaPackageName=javaPackageName;
41+
string[]parts=methodDescription.Split(methodDescSplitChars,4);
42+
43+
if(parts.Length<2){
44+
thrownewInvalidOperationException($"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
publicclassJavaCallableWrapperGenerator{
2959

3060
classJavaFieldInfo{
@@ -59,6 +89,7 @@ public string GetJavaAccess ()
5989
List<Signature>methods=newList<Signature>();
6090
List<Signature>ctors=newList<Signature>();
6191
List<JavaCallableWrapperGenerator>children;
92+
List<OverriddenMethodDescriptor>overriddenMethodDescriptors;
6293
readonlyIMetadataResolvercache;
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+
publicIList<OverriddenMethodDescriptor>OverriddenMethodDescriptors=>overriddenMethodDescriptors;
83115
publicstringApplicationJavaClass{get;set;}
84116
publicJavaPeerStyleCodeGenerationTarget{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
publicvoidGenerate(TextWriterwriter)
499531
{
532+
overriddenMethodDescriptors=newList<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+
stringjavaTypeName=$"{package}.{name}";
568+
AddOverridenMethods(methods);
569+
AddOverridenMethods(ctors);
570+
571+
voidAddOverridenMethods(List<Signature>list)
572+
{
573+
foreach(Signaturesiginlist){
574+
overriddenMethodDescriptors.Add(newOverriddenMethodDescriptor(javaTypeName,sig.Method));
575+
}
576+
}
533577
}
534578

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

0 commit comments

Comments
 (0)