Skip to content

Commit 34f05a1

Browse files
committed
[JavaCallableWrappers] Begin modularizing jcw process.
1 parent 07c7300 commit 34f05a1

File tree

9 files changed

+723
-583
lines changed

9 files changed

+723
-583
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
6-
<LangVersion>8.0</LangVersion>
6+
<LangVersion>10.0</LangVersion>
77
<Nullable>enable</Nullable>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<SignAssembly>true</SignAssembly>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Mono.Cecil;
2+
3+
namespace Java.Interop.Tools.JavaCallableWrappers
4+
{
5+
public abstract class JavaCallableMethodClassifier
6+
{
7+
public abstract bool ShouldBeDynamicallyRegistered (TypeDefinition topType, MethodDefinition registeredMethod, MethodDefinition implementedMethod, CustomAttribute? registerAttribute);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
using Mono.Cecil;
4+
using Java.Interop.Tools.TypeNameMappings;
5+
6+
using MethodAttributes = Mono.Cecil.MethodAttributes;
7+
using static Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager;
8+
9+
namespace Java.Interop.Tools.JavaCallableWrappers
10+
{
11+
public partial class JavaCallableWrapperGenerator {
12+
internal class JavaFieldInfo {
13+
public JavaFieldInfo (MethodDefinition method, string fieldName, IMetadataResolver resolver)
14+
{
15+
this.FieldName = fieldName;
16+
InitializerName = method.Name;
17+
TypeName = JavaNativeTypeManager.ReturnTypeFromSignature (GetJniSignature (method, resolver))?.Type
18+
?? throw new ArgumentException ($"Could not get JNI signature for method `{method.Name}`", nameof (method));
19+
IsStatic = method.IsStatic;
20+
Access = method.Attributes & MethodAttributes.MemberAccessMask;
21+
Annotations = GetAnnotationsString ("\t", method.CustomAttributes, resolver);
22+
}
23+
24+
public MethodAttributes Access { get; private set; }
25+
public bool IsStatic { get; private set; }
26+
public string TypeName { get; private set; }
27+
public string FieldName { get; private set; }
28+
public string InitializerName { get; private set; }
29+
public string Annotations { get; private set; }
30+
31+
public string GetJavaAccess ()
32+
{
33+
return JavaCallableWrapperGenerator.GetJavaAccess (Access);
34+
}
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Text;
3+
4+
using Mono.Cecil;
5+
6+
using Android.Runtime;
7+
using Java.Interop.Tools.Diagnostics;
8+
using Java.Interop.Tools.TypeNameMappings;
9+
10+
using MethodAttributes = Mono.Cecil.MethodAttributes;
11+
using static Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager;
12+
13+
namespace Java.Interop.Tools.JavaCallableWrappers
14+
{
15+
16+
public partial class JavaCallableWrapperGenerator {
17+
internal class Signature {
18+
19+
public Signature (MethodDefinition method, RegisterAttribute register, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) : this (method, register, null, null, cache, shouldBeDynamicallyRegistered) {}
20+
21+
public Signature (MethodDefinition method, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true)
22+
: this (register.Name, register.Signature, register.Connector, managedParameters, outerType, null)
23+
{
24+
Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache);
25+
IsDynamicallyRegistered = shouldBeDynamicallyRegistered;
26+
}
27+
28+
public Signature (MethodDefinition method, ExportAttribute export, string? managedParameters, IMetadataResolver cache)
29+
: this (method.Name, GetJniSignature (method, cache), "__export__", null, null, export.SuperArgumentsString)
30+
{
31+
IsExport = true;
32+
IsStatic = method.IsStatic;
33+
JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask);
34+
ThrownTypeNames = export.ThrownNames;
35+
JavaNameOverride = export.Name;
36+
ManagedParameters = managedParameters;
37+
Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache);
38+
}
39+
40+
public Signature (MethodDefinition method, ExportFieldAttribute exportField, IMetadataResolver cache)
41+
: this (method.Name, GetJniSignature (method, cache), "__export__", null, null, null)
42+
{
43+
if (method.HasParameters)
44+
Diagnostic.Error (4205, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4205);
45+
if (method.ReturnType.MetadataType == MetadataType.Void)
46+
Diagnostic.Error (4208, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4208);
47+
IsExport = true;
48+
IsStatic = method.IsStatic;
49+
JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask);
50+
51+
// annotations are processed within JavaFieldInfo, not the initializer method. So we don't generate them here.
52+
}
53+
54+
public Signature (string name, string? signature, string? connector, string? managedParameters, string? outerType, string? superCall)
55+
{
56+
ManagedParameters = managedParameters;
57+
JniSignature = signature ?? throw new ArgumentNullException ("`connector` cannot be null.", nameof (connector));
58+
Method = "n_" + name + ":" + JniSignature + ":" + connector;
59+
Name = name;
60+
61+
var jnisig = JniSignature;
62+
int closer = jnisig.IndexOf (')');
63+
string ret = jnisig.Substring (closer + 1);
64+
retval = JavaNativeTypeManager.Parse (ret)?.Type;
65+
string jniparms = jnisig.Substring (1, closer - 1);
66+
if (string.IsNullOrEmpty (jniparms) && string.IsNullOrEmpty (superCall))
67+
return;
68+
var parms = new StringBuilder ();
69+
var scall = new StringBuilder ();
70+
var acall = new StringBuilder ();
71+
bool first = true;
72+
int i = 0;
73+
foreach (JniTypeName jti in JavaNativeTypeManager.FromSignature (jniparms)) {
74+
if (outerType != null) {
75+
acall.Append (outerType).Append (".this");
76+
outerType = null;
77+
continue;
78+
}
79+
string? parmType = jti.Type;
80+
if (!first) {
81+
parms.Append (", ");
82+
scall.Append (", ");
83+
acall.Append (", ");
84+
}
85+
first = false;
86+
parms.Append (parmType).Append (" p").Append (i);
87+
scall.Append ("p").Append (i);
88+
acall.Append ("p").Append (i);
89+
++i;
90+
}
91+
this.parms = parms.ToString ();
92+
this.call = superCall != null ? superCall : scall.ToString ();
93+
this.ActivateCall = acall.ToString ();
94+
}
95+
96+
string? call;
97+
public string? SuperCall {
98+
get { return call; }
99+
}
100+
101+
public string? ActivateCall {get; private set;}
102+
103+
public readonly string Name;
104+
public readonly string? JavaNameOverride;
105+
public string JavaName {
106+
get { return JavaNameOverride ?? Name; }
107+
}
108+
109+
string? parms;
110+
public string? Params {
111+
get { return parms; }
112+
}
113+
114+
string? retval;
115+
public string? Retval {
116+
get { return retval; }
117+
}
118+
119+
public string? ThrowsDeclaration {
120+
get { return ThrownTypeNames?.Length > 0 ? " throws " + String.Join (", ", ThrownTypeNames) : null; }
121+
}
122+
123+
public readonly string? JavaAccess;
124+
public readonly string? ManagedParameters;
125+
public readonly string JniSignature;
126+
public readonly string Method;
127+
public readonly bool IsExport;
128+
public readonly bool IsStatic;
129+
public readonly bool IsDynamicallyRegistered = true;
130+
public readonly string []? ThrownTypeNames;
131+
public readonly string? Annotations;
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)