Skip to content

Commit 8766a35

Browse files
committed
Update code to implemented some requested changes
1 parent cb33a0d commit 8766a35

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,7 @@ void Run (DirectoryAssemblyResolver res)
183183
if (!userAssemblies.ContainsKey (name))
184184
userAssemblies.Add (name, asm.ItemSpec);
185185
#if ENABLE_MARSHAL_METHODS
186-
if (!Debug) {
187-
StoreMarshalAssemblyPath (name, asm);
188-
}
186+
StoreMarshalAssemblyPath (name, asm);
189187
#endif
190188
}
191189

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ void AddEnvironment ()
444444
#if ENABLE_MARSHAL_METHODS
445445
var marshalMethodsState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<MarshalMethodsState> (GenerateJavaStubs.MarshalMethodsRegisterTaskKey, RegisteredTaskObjectLifetime.Build);
446446

447-
var marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator () {
447+
var marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator {
448448
NumberOfAssembliesInApk = assemblyCount,
449449
UniqueAssemblyNames = uniqueAssemblyNames,
450450
MarshalMethods = marshalMethodsState?.MarshalMethods,

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsAssemblyRewriter.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace Xamarin.Android.Tasks
1010
{
1111
class MarshalMethodsAssemblyRewriter
1212
{
13-
IDictionary<string, MarshalMethodEntry> methods;
13+
IDictionary<string, IList<MarshalMethodEntry>> methods;
1414
ICollection<AssemblyDefinition> uniqueAssemblies;
1515
IDictionary <string, HashSet<string>> assemblyPaths;
1616

17-
public MarshalMethodsAssemblyRewriter (IDictionary<string, MarshalMethodEntry> methods, ICollection<AssemblyDefinition> uniqueAssemblies, IDictionary <string, HashSet<string>> assemblyPaths)
17+
public MarshalMethodsAssemblyRewriter (IDictionary<string, IList<MarshalMethodEntry>> methods, ICollection<AssemblyDefinition> uniqueAssemblies, IDictionary <string, HashSet<string>> assemblyPaths)
1818
{
1919
this.methods = methods ?? throw new ArgumentNullException (nameof (methods));
2020
this.uniqueAssemblies = uniqueAssemblies ?? throw new ArgumentNullException (nameof (uniqueAssemblies));
@@ -23,17 +23,20 @@ public MarshalMethodsAssemblyRewriter (IDictionary<string, MarshalMethodEntry> m
2323

2424
public void Rewrite (DirectoryAssemblyResolver resolver)
2525
{
26+
MethodDefinition unmanagedCallersOnlyAttributeCtor = GetUnmanagedCallersOnlyAttributeConstructor (resolver);
2627
var unmanagedCallersOnlyAttributes = new Dictionary<AssemblyDefinition, CustomAttribute> ();
2728
foreach (AssemblyDefinition asm in uniqueAssemblies) {
28-
unmanagedCallersOnlyAttributes.Add (asm, GetUnmanagedCallersOnlyAttribute (asm, resolver));
29+
unmanagedCallersOnlyAttributes.Add (asm, CreateImportedUnmanagedCallersOnlyAttribute (asm, unmanagedCallersOnlyAttributeCtor));
2930
}
3031

3132
Console.WriteLine ("Adding the [UnmanagedCallersOnly] attribute to native callback methods and removing unneeded fields+methods");
32-
foreach (MarshalMethodEntry method in methods.Values) {
33-
Console.WriteLine ($"\t{method.NativeCallback.FullName} (token: 0x{method.NativeCallback.MetadataToken.RID:x})");
34-
method.NativeCallback.CustomAttributes.Add (unmanagedCallersOnlyAttributes [method.NativeCallback.Module.Assembly]);
35-
method.Connector.DeclaringType.Methods.Remove (method.Connector);
36-
method.CallbackField?.DeclaringType.Fields.Remove (method.CallbackField);
33+
foreach (IList<MarshalMethodEntry> methodList in methods.Values) {
34+
foreach (MarshalMethodEntry method in methodList) {
35+
Console.WriteLine ($"\t{method.NativeCallback.FullName} (token: 0x{method.NativeCallback.MetadataToken.RID:x})");
36+
method.NativeCallback.CustomAttributes.Add (unmanagedCallersOnlyAttributes [method.NativeCallback.Module.Assembly]);
37+
method.Connector.DeclaringType.Methods.Remove (method.Connector);
38+
method.CallbackField?.DeclaringType.Fields.Remove (method.CallbackField);
39+
}
3740
}
3841

3942
Console.WriteLine ();
@@ -75,8 +78,10 @@ public void Rewrite (DirectoryAssemblyResolver resolver)
7578

7679
Console.WriteLine ();
7780
Console.WriteLine ("Method tokens:");
78-
foreach (MarshalMethodEntry method in methods.Values) {
79-
Console.WriteLine ($"\t{method.NativeCallback.FullName} (token: 0x{method.NativeCallback.MetadataToken.RID:x})");
81+
foreach (IList<MarshalMethodEntry> methodList in methods.Values) {
82+
foreach (MarshalMethodEntry method in methodList) {
83+
Console.WriteLine ($"\t{method.NativeCallback.FullName} (token: 0x{method.NativeCallback.MetadataToken.RID:x})");
84+
}
8085
}
8186

8287
void MoveFile (string source, string target)
@@ -99,7 +104,7 @@ ICollection<string> GetAssemblyPaths (AssemblyDefinition asm)
99104
return paths;
100105
}
101106

102-
CustomAttribute GetUnmanagedCallersOnlyAttribute (AssemblyDefinition targetAssembly, DirectoryAssemblyResolver resolver)
107+
MethodDefinition GetUnmanagedCallersOnlyAttributeConstructor (DirectoryAssemblyResolver resolver)
103108
{
104109
AssemblyDefinition asm = resolver.Resolve ("System.Runtime.InteropServices");
105110
TypeDefinition unmanagedCallersOnlyAttribute = null;
@@ -118,16 +123,24 @@ CustomAttribute GetUnmanagedCallersOnlyAttribute (AssemblyDefinition targetAssem
118123
}
119124
}
120125

121-
MethodDefinition attrConstructor = null;
126+
if (unmanagedCallersOnlyAttribute == null) {
127+
throw new InvalidOperationException ("Unable to find the System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute type");
128+
}
129+
122130
foreach (MethodDefinition md in unmanagedCallersOnlyAttribute.Methods) {
123131
if (!md.IsConstructor) {
124132
continue;
125133
}
126134

127-
attrConstructor = md;
135+
return md;
128136
}
129137

130-
return new CustomAttribute (targetAssembly.MainModule.ImportReference (attrConstructor));
138+
throw new InvalidOperationException ("Unable to find the System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute type constructor");
139+
}
140+
141+
CustomAttribute CreateImportedUnmanagedCallersOnlyAttribute (AssemblyDefinition targetAssembly, MethodDefinition unmanagedCallersOnlyAtributeCtor)
142+
{
143+
return new CustomAttribute (targetAssembly.MainModule.ImportReference (unmanagedCallersOnlyAtributeCtor));
131144
}
132145
}
133146
}

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsClassifier.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Xamarin.Android.Tasks
1313
#if ENABLE_MARSHAL_METHODS
1414
public sealed class MarshalMethodEntry
1515
{
16-
public TypeDefinition TopType { get; }
16+
public TypeDefinition DeclaringType { get; }
1717
public MethodDefinition NativeCallback { get; }
1818
public MethodDefinition Connector { get; }
1919
public MethodDefinition RegisteredMethod { get; }
@@ -24,18 +24,27 @@ public sealed class MarshalMethodEntry
2424
public string JniMethodSignature { get; }
2525

2626
public MarshalMethodEntry (TypeDefinition topType, MethodDefinition nativeCallback, MethodDefinition connector, MethodDefinition
27-
registeredMethod, MethodDefinition implementedMethod, FieldDefinition callbackField, string jniTypeName,
28-
string jniName, string jniSignature)
27+
registeredMethod, MethodDefinition implementedMethod, FieldDefinition callbackField, string jniTypeName,
28+
string jniName, string jniSignature)
2929
{
30-
TopType = topType ?? throw new ArgumentNullException (nameof (topType));
30+
DeclaringType = topType ?? throw new ArgumentNullException (nameof (topType));
3131
NativeCallback = nativeCallback ?? throw new ArgumentNullException (nameof (nativeCallback));
3232
Connector = connector ?? throw new ArgumentNullException (nameof (connector));
3333
RegisteredMethod = registeredMethod ?? throw new ArgumentNullException (nameof (registeredMethod));
3434
ImplementedMethod = implementedMethod ?? throw new ArgumentNullException (nameof (implementedMethod));
35-
CallbackField = callbackField;
36-
JniTypeName = jniTypeName;
37-
JniMethodName = jniName;
38-
JniMethodSignature = jniSignature;
35+
CallbackField = callbackField; // we don't require the callback field to exist
36+
JniTypeName = EnsureNonEmpty (jniTypeName, nameof (jniTypeName));
37+
JniMethodName = EnsureNonEmpty (jniName, nameof (jniName));
38+
JniMethodSignature = EnsureNonEmpty (jniSignature, nameof (jniSignature));
39+
}
40+
41+
string EnsureNonEmpty (string s, string argName)
42+
{
43+
if (String.IsNullOrEmpty (s)) {
44+
throw new ArgumentException ("must not be null or empty", argName);
45+
}
46+
47+
return s;
3948
}
4049
}
4150
#endif

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct MarshalMethodsManagedClass
3838

3939
public ICollection<string> UniqueAssemblyNames { get; set; }
4040
public int NumberOfAssembliesInApk { get; set; }
41-
public IDictionary<string, MarshalMethodEntry> MarshalMethods { get; set; }
41+
public IDictionary<string, IList<MarshalMethodEntry>> MarshalMethods { get; set; }
4242

4343
StructureInfo<TypeMappingReleaseNativeAssemblyGenerator.MonoImage> monoImage;
4444
StructureInfo<MonoClass> monoClass;

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Xamarin.Android.Tasks
66
{
77
sealed class MarshalMethodsState
88
{
9-
public IDictionary<string, MarshalMethodEntry> MarshalMethods { get; }
9+
public IDictionary<string, IList<MarshalMethodEntry>> MarshalMethods { get; }
1010

11-
public MarshalMethodsState (IDictionary<string, MarshalMethodEntry> marshalMethods)
11+
public MarshalMethodsState (IDictionary<string, IList<MarshalMethodEntry>> marshalMethods)
1212
{
1313
MarshalMethods = marshalMethods ?? throw new ArgumentNullException (nameof (marshalMethods));
1414
}

0 commit comments

Comments
 (0)