Skip to content

Commit a4a7fda

Browse files
radekdoulikjonpryor
authored andcommitted
[jnimarshalmethod-gen] Update the originating assembly (#300)
Update the source assembly instead of creating a *new* assembly named `<AssemblyBaseName>-new.dll` I expected I would need to load the originating assembly in the separate AppDomain, but instead it was enough to load it with Cecil with reading parameters having `ReadWrite = true`. Because the referenced assemblies might not be writable, we need to keep loading them as read only, thus with default `ReadWrite = false`. For that I added new method to our Cecil assembly resolver, to add existing assembly definition to its cache. We first read the originating assemblies as `ReadWrite` and add them to the resolver cache.
1 parent c084a83 commit a4a7fda

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ run-android: $(ATESTS)
159159
run-test-jnimarshal: bin/Test$(CONFIGURATION)/Java.Interop.Export-Tests.dll bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB)
160160
MONO_TRACE_LISTENER=Console.Out \
161161
$(RUNTIME) bin/$(CONFIGURATION)/jnimarshalmethod-gen.exe -v -L $(JI_MONO_LIB_PATH)mono/4.5 -L $(JI_MONO_LIB_PATH)mono/4.5/Facades "$<"
162-
$(RM) "$<" "$(basename $<)-JniMarshalMethods.dll"
163-
mv "$(basename $<)-new.dll" "$<"
164-
mv "$(basename $<)-new.pdb" "$(basename $<).pdb"
162+
$(RM) "$(basename $<)-JniMarshalMethods.dll"
165163
$(call RUN_TEST,$<)
166164

167165

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ public Dictionary<string, AssemblyDefinition> ToResolverCache ()
110110
return new Dictionary<string, AssemblyDefinition>(cache);
111111
}
112112

113+
public bool AddToCache (AssemblyDefinition assembly)
114+
{
115+
var name = Path.GetFileNameWithoutExtension (assembly.MainModule.FileName);
116+
117+
if (cache.ContainsKey (name))
118+
return false;
119+
120+
cache [name] = assembly;
121+
122+
return true;
123+
}
124+
113125
public virtual AssemblyDefinition Load (string fileName, bool forceLoad = false)
114126
{
115127
if (!File.Exists (fileName))

tools/jnimarshalmethod-gen/App.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ public static int Main (string [] args)
6262
return 0;
6363
}
6464

65+
var readWriteParameters = new ReaderParameters {
66+
AssemblyResolver = resolver,
67+
ReadSymbols = true,
68+
ReadWrite = true,
69+
};
6570
foreach (var assembly in assemblies) {
6671
if (!File.Exists (assembly)) {
6772
Error ($"Path '{assembly}' does not exist.");
6873
return 1;
6974
}
7075

7176
resolver.SearchDirectories.Add (Path.GetDirectoryName (assembly));
72-
resolver.Load (assembly);
77+
var ad = AssemblyDefinition.ReadAssembly (assembly, readWriteParameters);
78+
resolver.AddToCache (ad);
7379

7480
try {
7581
CreateMarshalMethodAssembly (assembly);

tools/jnimarshalmethod-gen/TypeMover.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ public void Move ()
4646
return;
4747
}
4848

49-
var newName = $"{Path.Combine (Path.GetDirectoryName (Destination.MainModule.FileName), Path.GetFileNameWithoutExtension (Destination.MainModule.FileName))}-new{Path.GetExtension (Destination.MainModule.FileName)}";
50-
Destination.Write (newName, new WriterParameters () { WriteSymbols = true });
49+
Destination.Write (new WriterParameters () { WriteSymbols = true });
5150

5251
if (App.Verbose)
53-
App.ColorWriteLine ($"Wrote {newName} assembly", ConsoleColor.Cyan);
52+
App.ColorWriteLine ($"Wrote updated {Destination.MainModule.FileName} assembly", ConsoleColor.Cyan);
5453
}
5554

5655
static readonly string nestedName = "__<$>_jni_marshal_methods";

0 commit comments

Comments
 (0)