Skip to content

Commit

Permalink
[jcw-gen] use Assembly Name instead of FullName for typemaps (#227)
Browse files Browse the repository at this point in the history
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=61073

The Java-to-Managed typemaps list types such as:

	android/app/Activity
	Android.App.Activity, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065

Let’s assume you have an Android project with the following
assembly-level attribute:

	[assembly:AssemblyVersion("1.0.0.*")]

Then on *every* build, the typemap is invalidated because your version
number has been incremented.

The fix here is to use the assembly’s short name via `GetName ().Name`
or Mono.Cecil’s equivalent `AssemblyDefinition.Name.Name`. So the above
typemap would only be `Android.App.Activity, Mono.Android`. These
changes needed to happen in both `JavaNativeTypeManager` and
`TypeNameMapGenerator`.

The final fix is to find *every* instance of
`TypeDefinitionRocks.GetAssemblyQualifiedName` and use
`TypeDefinitionRocks.GetPartialAssemblyQualifiedName` in its place.
The latter also had the issue of needing to replace the `/` character
with `+`, so both methods return valid nested type names.
  • Loading branch information
jonathanpeppers authored and jonpryor committed Dec 20, 2017
1 parent 57d5d51 commit 429dc2a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public static bool ImplementsInterface (this TypeDefinition type, string interfa
public static string GetPartialAssemblyQualifiedName (this TypeReference type)
{
TypeDefinition def = type.Resolve ();
return string.Format ("{0}, {1}", type.FullName, (def ?? type).Module.Assembly.Name.Name);
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 ('/', '+'),
(def ?? type).Module.Assembly.Name.Name);
}

public static string GetAssemblyQualifiedName (this TypeReference type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ string GetManagedParameters (MethodDefinition ctor, string outerType)
if (sb.Length > 0)
sb.Append (':');
if (outerType != null && sb.Length == 0)
sb.Append (type.DeclaringType.GetAssemblyQualifiedName ());
sb.Append (type.DeclaringType.GetPartialAssemblyQualifiedName ());
else
sb.Append (pdef.ParameterType.GetAssemblyQualifiedName ());
sb.Append (pdef.ParameterType.GetPartialAssemblyQualifiedName ());
}
return sb.ToString ();
}
Expand Down Expand Up @@ -567,12 +567,12 @@ void GenerateBody (TextWriter sw)

if (GenerateOnCreateOverrides && JavaNativeTypeManager.IsApplication (type) && !methods.Any (m => m.Name == "onCreate"))
WriteApplicationOnCreate (sw, w => {
w.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, __md_methods);", type.GetAssemblyQualifiedName (), name);
w.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, __md_methods);", type.GetPartialAssemblyQualifiedName (), name);
w.WriteLine ("\t\tsuper.onCreate ();");
});
if (GenerateOnCreateOverrides && JavaNativeTypeManager.IsInstrumentation (type) && !methods.Any (m => m.Name == "onCreate"))
WriteInstrumentationOnCreate (sw, w => {
w.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, __md_methods);", type.GetAssemblyQualifiedName (), name);
w.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, __md_methods);", type.GetPartialAssemblyQualifiedName (), name);
w.WriteLine ("\t\tsuper.onCreate (arguments);");
});

Expand Down Expand Up @@ -600,7 +600,7 @@ static void GenerateRegisterType (TextWriter sw, JavaCallableWrapperGenerator se
sw.WriteLine ("\t\t\t\"\";");
if (!CannotRegisterInStaticConstructor (self.type))
sw.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, {2});",
self.type.GetAssemblyQualifiedName (), self.name, field);
self.type.GetPartialAssemblyQualifiedName (), self.name, field);
}

void GenerateFooter (TextWriter sw)
Expand Down Expand Up @@ -767,7 +767,7 @@ void GenerateConstructor (Signature ctor, TextWriter sw)
#endif
if (!CannotRegisterInStaticConstructor (type)) {
sw.WriteLine ("\t\tif (getClass () == {0}.class)", name);
sw.WriteLine ("\t\t\tmono.android.TypeManager.Activate (\"{0}\", \"{1}\", this, new java.lang.Object[] {{ {2} }});", type.GetAssemblyQualifiedName (), ctor.ManagedParameters, ctor.ActivateCall);
sw.WriteLine ("\t\t\tmono.android.TypeManager.Activate (\"{0}\", \"{1}\", this, new java.lang.Object[] {{ {2} }});", type.GetPartialAssemblyQualifiedName (), ctor.ManagedParameters, ctor.ActivateCall);
}
sw.WriteLine ("\t}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void WriteJavaToManaged (Stream output)
var typeMap = GetTypeMapping (
t => t.IsInterface || t.HasGenericParameters,
JavaNativeTypeManager.ToJniName,
t => t.GetAssemblyQualifiedName ());
t => t.GetPartialAssemblyQualifiedName ());

WriteBinaryMapping (output, typeMap);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ public void WriteManagedToJava (Stream output)

var typeMap = GetTypeMapping (
t => false,
t => t.GetAssemblyQualifiedName (),
t => t.GetPartialAssemblyQualifiedName (),
JavaNativeTypeManager.ToJniName);

WriteBinaryMapping (output, typeMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void GenerateIndirectApplication (
)
{
var actual = Generate (typeof (IndirectApplication), applicationJavaClass);
var expected = @"package md5fef72cac46d04ae5bdc90af5bb6221ad;
var expected = @"package md5f43cdfade412ae71b21bb70a5c2841ab;
public class IndirectApplication
Expand Down Expand Up @@ -144,7 +144,7 @@ public void monodroidClearReferences ()
public void GenerateExportedMembers ()
{
var actual = Generate (typeof (ExportsMembers));
var expected = @"package md5fef72cac46d04ae5bdc90af5bb6221ad;
var expected = @"package md5f43cdfade412ae71b21bb70a5c2841ab;
public class ExportsMembers
Expand All @@ -156,28 +156,28 @@ extends java.lang.Object
public static final String __md_methods;
static {
__md_methods =
""n_GetInstance:()Lmd5fef72cac46d04ae5bdc90af5bb6221ad/ExportsMembers;:__export__\n"" +
""n_GetInstance:()Lmd5f43cdfade412ae71b21bb70a5c2841ab/ExportsMembers;:__export__\n"" +
""n_GetValue:()Ljava/lang/String;:__export__\n"" +
""n_methodNamesNotMangled:()V:__export__\n"" +
""n_CompletelyDifferentName:(Ljava/lang/String;I)Ljava/lang/String;:__export__\n"" +
""n_methodThatThrows:()V:__export__\n"" +
""n_methodThatThrowsEmptyArray:()V:__export__\n"" +
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsMembers, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsMembers.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsMembers, Java.Interop.Tools.JavaCallableWrappers-Tests"", ExportsMembers.class, __md_methods);
}
public static md5fef72cac46d04ae5bdc90af5bb6221ad.ExportsMembers STATIC_INSTANCE = GetInstance ();
public static md5f43cdfade412ae71b21bb70a5c2841ab.ExportsMembers STATIC_INSTANCE = GetInstance ();
public java.lang.String VALUE = GetValue ();
public static md5fef72cac46d04ae5bdc90af5bb6221ad.ExportsMembers GetInstance ()
public static md5f43cdfade412ae71b21bb70a5c2841ab.ExportsMembers GetInstance ()
{
return n_GetInstance ();
}
private static native md5fef72cac46d04ae5bdc90af5bb6221ad.ExportsMembers n_GetInstance ();
private static native md5f43cdfade412ae71b21bb70a5c2841ab.ExportsMembers n_GetInstance ();
public java.lang.String GetValue ()
{
Expand Down Expand Up @@ -240,7 +240,7 @@ public void monodroidClearReferences ()
public void GenerateInnerClass ()
{
var actual = Generate (typeof (ExampleOuterClass));
var expected = @"package md5fef72cac46d04ae5bdc90af5bb6221ad;
var expected = @"package md5f43cdfade412ae71b21bb70a5c2841ab;
public class ExampleOuterClass
Expand All @@ -254,10 +254,10 @@ extends register.NonStaticOuterClass
static {
__md_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExampleOuterClass, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExampleOuterClass.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExampleOuterClass, Java.Interop.Tools.JavaCallableWrappers-Tests"", ExampleOuterClass.class, __md_methods);
__md_1_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExampleOuterClass+ExampleInnerClass, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExampleOuterClass_ExampleInnerClass.class, __md_1_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExampleOuterClass+ExampleInnerClass, Java.Interop.Tools.JavaCallableWrappers-Tests"", ExampleOuterClass_ExampleInnerClass.class, __md_1_methods);
}
private java.util.ArrayList refList;
Expand Down Expand Up @@ -316,7 +316,7 @@ extends java.lang.Object
static {
__md_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.RegisterName+DefaultNestedName, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", Name_DefaultNestedName.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.RegisterName+DefaultNestedName, Java.Interop.Tools.JavaCallableWrappers-Tests"", Name_DefaultNestedName.class, __md_methods);
}
private java.util.ArrayList refList;
Expand Down Expand Up @@ -354,7 +354,7 @@ extends java.lang.Object
static {
__md_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.RegisterName+OverrideNestedName, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", Name$Override.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.RegisterName+OverrideNestedName, Java.Interop.Tools.JavaCallableWrappers-Tests"", Name$Override.class, __md_methods);
}
private java.util.ArrayList refList;
Expand Down Expand Up @@ -392,23 +392,23 @@ extends java.lang.Object
static {
__md_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsConstructors.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", ExportsConstructors.class, __md_methods);
}
public ExportsConstructors ()
{
super ();
if (getClass () == ExportsConstructors.class)
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { });
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { });
}
public ExportsConstructors (int p0)
{
super (p0);
if (getClass () == ExportsConstructors.class)
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
}
private java.util.ArrayList refList;
Expand Down Expand Up @@ -446,31 +446,31 @@ extends java.lang.Object
static {
__md_methods =
"""";
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", ExportsThrowsConstructors.class, __md_methods);
mono.android.Runtime.register (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", ExportsThrowsConstructors.class, __md_methods);
}
public ExportsThrowsConstructors () throws java.lang.Throwable
{
super ();
if (getClass () == ExportsThrowsConstructors.class)
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { });
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { });
}
public ExportsThrowsConstructors (int p0) throws java.lang.Throwable
{
super (p0);
if (getClass () == ExportsThrowsConstructors.class)
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
}
public ExportsThrowsConstructors (java.lang.String p0)
{
super (p0);
if (getClass () == ExportsThrowsConstructors.class)
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"", """", this, new java.lang.Object[] { p0 });
mono.android.TypeManager.Activate (""Xamarin.Android.ToolsTests.ExportsThrowsConstructors, Java.Interop.Tools.JavaCallableWrappers-Tests"", """", this, new java.lang.Object[] { p0 });
}
private java.util.ArrayList refList;
Expand Down
Loading

0 comments on commit 429dc2a

Please sign in to comment.