Skip to content

Commit

Permalink
[Java.Interop.Tools.JavaCallableWrappers] marshal method decl types (#…
Browse files Browse the repository at this point in the history
…987)

Context: dotnet/android#7004
Context: fb94d59

fb94d59 added code to collect all `override` method descriptors in a
list, so that the future marshal methods code generator can generate
correct code for them.  However, not all gathered descriptors contain
the name of the managed type which declares the overridden methods,
which is data that the native marshal method generator needs.

Update when `JavaCallableWrapperGenerator.OverriddenMethodDescriptors`
is populated so that the marshal method's declaring type is used.
  • Loading branch information
grendello authored May 19, 2022
1 parent e7bacc3 commit 02aa54e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static string GetPartialAssemblyQualifiedName (this TypeReference type, I
return string.Format ("{0}, {1}",
// Cecil likes to use '/' as the nested type separator, while
// Reflection uses '+' as the nested type separator. Use Reflection.
type.FullName.Replace ('/', '+'),
CecilTypeNameToReflectionTypeName (type.FullName),
type.GetPartialAssemblyName (resolver));
}

Expand All @@ -160,7 +160,7 @@ public static string GetAssemblyQualifiedName (this TypeReference type, IMetadat
return string.Format ("{0}, {1}",
// Cecil likes to use '/' as the nested type separator, while
// Reflection uses '+' as the nested type separator. Use Reflection.
type.FullName.Replace ('/', '+'),
CecilTypeNameToReflectionTypeName (type.FullName),
(def ?? type).Module.Assembly.Name.FullName);
}

Expand Down Expand Up @@ -188,5 +188,7 @@ public static string GetAssemblyQualifiedName (this TypeReference type, IMetadat

return null;
}

public static string? CecilTypeNameToReflectionTypeName (string? typeName) => typeName?.Replace ('/', '+');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public class OverriddenMethodDescriptor
public string JniSignature { get; }
public string Connector { get; }
public string ManagedTypeName { get; }
public string OriginalDescString { get; }

public OverriddenMethodDescriptor (string javaPackageName, string methodDescription)
public OverriddenMethodDescriptor (string javaPackageName, string methodDescription, string fallbackManagedTypeName)
{
OriginalDescString = methodDescription;
JavaPackageName = javaPackageName;
string[] parts = methodDescription.Split (methodDescSplitChars, 4);

Expand All @@ -49,9 +51,13 @@ public OverriddenMethodDescriptor (string javaPackageName, string methodDescript
if (parts.Length > 2) {
Connector = parts[2];
if (parts.Length > 3) {
ManagedTypeName = parts[3];
ManagedTypeName = TypeDefinitionRocks.CecilTypeNameToReflectionTypeName (parts[3]);
}
}

if (String.IsNullOrEmpty (ManagedTypeName)) {
ManagedTypeName = fallbackManagedTypeName;
}
}
}

Expand Down Expand Up @@ -529,7 +535,6 @@ string GetManagedParameters (MethodDefinition ctor, string outerType)

public void Generate (TextWriter writer)
{
overriddenMethodDescriptors = new List<OverriddenMethodDescriptor> ();
if (!string.IsNullOrEmpty (package)) {
writer.WriteLine ("package " + package + ";");
writer.WriteLine ();
Expand Down Expand Up @@ -563,17 +568,6 @@ public void Generate (TextWriter writer)
}

GenerateFooter (writer);

string javaTypeName = $"{package}.{name}";
AddOverridenMethods (methods);
AddOverridenMethods (ctors);

void AddOverridenMethods (List<Signature> list)
{
foreach (Signature sig in list) {
overriddenMethodDescriptors.Add (new OverriddenMethodDescriptor (javaTypeName, sig.Method));
}
}
}

public void Generate (string outputPath)
Expand Down Expand Up @@ -716,9 +710,17 @@ void GenerateBody (TextWriter sw)

void GenerateRegisterType (TextWriter sw, JavaCallableWrapperGenerator self, string field)
{
if (overriddenMethodDescriptors == null) {
overriddenMethodDescriptors = new List<OverriddenMethodDescriptor> ();
}

sw.WriteLine ("\t\t{0} = ", field);
foreach (Signature method in self.methods)
string managedTypeName = self.type.GetPartialAssemblyQualifiedName (cache);
string javaTypeName = $"{package}.{name}";
foreach (Signature method in self.methods) {
sw.WriteLine ("\t\t\t\"{0}\\n\" +", method.Method);
overriddenMethodDescriptors.Add (new OverriddenMethodDescriptor (javaTypeName, method.Method, managedTypeName));
}
sw.WriteLine ("\t\t\t\"\";");
if (CannotRegisterInStaticConstructor (self.type))
return;
Expand All @@ -732,7 +734,7 @@ void GenerateRegisterType (TextWriter sw, JavaCallableWrapperGenerator self, str
break;
}
sw.Write ("\t\t");
sw.WriteLine (format, self.type.GetPartialAssemblyQualifiedName (cache), self.name, field);
sw.WriteLine (format, managedTypeName, self.name, field);
}

void GenerateFooter (TextWriter sw)
Expand Down

0 comments on commit 02aa54e

Please sign in to comment.