Skip to content

Commit 45aff90

Browse files
atsushienojonpryor
authored andcommitted
[generator] less aggressive obfuscation marking (#121)
This is a fixup for c51469e, as well as bugfix for: https://bugzilla.xamarin.com/show_bug.cgi?id=51337 What's happening behind bug #51337 was this: BINDINGSGENERATOR: warning BG8800: Unknown parameter type org.osmdroid.ResourceProxy.bitmap in method GetBitmap in managed type OsmDroid.DefaultResourceProxyImpl. This was, because, "bitmap" is all lowercased and was marked as obfuscated (because, WHY NAME A CLASS ALL IN LOWERCASE!?). Such classes should not exist, or should be marked as "obfuscated='false'". However what bug #51337 implies is that people are not going to make this additional markup for generator improvements. What this generator change does is then - a hack. A hack to mark "classes with very short names" as obfuscated, instead of "all lowercase or number". As commented on c51469e, there isn't good way to check API element siblings (it can check sibling names every time at all expensive calculation). So we just count the sibling nodes and if name length is short enough to fit within the number of classes, we mark as obfucated. As some post-first-commit thought, the name check is done only for very short names i.e. only those within 3 letters. Also, the new proguard in Gradle task seems to obfuscates the names as "zzXXXXX" . They should be marked as obfuscated too.
1 parent 835aaf7 commit 45aff90

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

tools/generator/GenBaseSupport.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public XmlGenBaseSupport (XmlElement pkg, XmlElement elem)
204204
full_name = String.Format ("{0}.{1}{2}", ns, idx > 0 ? StringRocks.TypeToPascalCase (java_name.Substring (0, idx + 1)) : String.Empty, name);
205205
}
206206

207-
obfuscated = IsObfuscatedName (java_name) && elem.XGetAttribute ("obfuscated") != "false";
207+
obfuscated = IsObfuscatedName (pkg.ChildNodes.Count, java_name) && elem.XGetAttribute ("obfuscated") != "false";
208208
}
209209

210210
public override bool IsAcw {
@@ -302,12 +302,21 @@ public override bool ValidateNamespace ()
302302
return true;
303303
}
304304

305-
bool IsObfuscatedName (String name)
305+
bool IsObfuscatedName (int threshold, string name)
306306
{
307307
if (name.StartsWith ("R.", StringComparison.Ordinal))
308308
return false;
309309
int idx = name.LastIndexOf ('.');
310310
string last = idx < 0 ? name : name.Substring (idx + 1);
311+
// probably new proguard within Gradle tasks, used in recent GooglePlayServices in 2016 or later.
312+
if (last.StartsWith ("zz", StringComparison.Ordinal))
313+
return true;
314+
// do not expect any name with more than 3 letters is an 'obfuscated' one.
315+
if (last.Length > 3)
316+
return false;
317+
// Only short ones ('a', 'b', 'c' ... 'aa', 'ab', ... 'zzz') are the targets.
318+
if (!(last.Length == 3 && threshold > 26*26 || last.Length == 2 && threshold > 26 || last.Length == 1))
319+
return false;
311320
if (last.Any (c => (c < 'a' || 'z' < c) && (c < '0' || '9' < c)))
312321
return false;
313322
return true;

tools/generator/Tests-Core/api-cp.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<package name="java.lang">
44
<class abstract="false" deprecated="not deprecated" final="false" name="Object" static="false" visibility="public">
55
</class>
6+
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object" final="false" name="String" static="false" visibility="public">
7+
</class>
68
</package>
79
<package name="xamarin.test.invalidnames">
810
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object" final="false" name="InvalidNameMembers" static="false" visibility="public">
@@ -13,6 +15,12 @@
1315
<method abstract="false" deprecated="not deprecated" final="false" name="invalid$name" native="false" return="int" static="false" synchronized="false" visibility="public">
1416
</method>
1517
</class>
18+
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object" final="false" name="a" static="false" visibility="public">
19+
</class>
20+
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object" final="false" name="in" static="false" visibility="public">
21+
</class>
22+
<class abstract="false" deprecated="not deprecated" extends="java.lang.Object" extends-generic-aware="java.lang.Object" final="false" name="zzTop" static="false" visibility="public">
23+
</class>
1624
</package>
1725
</api>
1826

tools/generator/Tests-Core/expected.cp/GeneratedFiles.projitems

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<ItemGroup>
88
<Compile Include="$(MSBuildThisFileDirectory)\Java.Interop.__TypeRegistrations.cs" />
99
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.Object.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)\Java.Lang.String.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.Invalidnames.In.cs" />
1012
<Compile Include="$(MSBuildThisFileDirectory)\Xamarin.Test.Invalidnames.InvalidNameMembers.cs" />
1113
<Compile Include="$(MSBuildThisFileDirectory)\__NamespaceMapping__.cs" />
1214
</ItemGroup>

tools/generator/Tests-Core/expected.cp/Java.Interop.__TypeRegistrations.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public static void RegisterPackages ()
1414
#endif // def MONODROID_TIMING
1515
Java.Interop.TypeManager.RegisterPackages (
1616
new string[]{
17+
"xamarin/test/invalidnames",
1718
},
1819
new Converter<string, Type>[]{
20+
lookup_xamarin_test_invalidnames_package,
1921
});
2022
#if MONODROID_TIMING
2123
var end = DateTime.Now;
@@ -30,5 +32,17 @@ static Type Lookup (string[] mappings, string javaType)
3032
return null;
3133
return Type.GetType (managedType);
3234
}
35+
36+
static string[] package_xamarin_test_invalidnames_mappings;
37+
static Type lookup_xamarin_test_invalidnames_package (string klass)
38+
{
39+
if (package_xamarin_test_invalidnames_mappings == null) {
40+
package_xamarin_test_invalidnames_mappings = new string[]{
41+
"xamarin/test/invalidnames/in:Xamarin.Test.Invalidnames.In",
42+
};
43+
}
44+
45+
return Lookup (package_xamarin_test_invalidnames_mappings, klass);
46+
}
3347
}
3448
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Android.Runtime;
4+
5+
namespace Java.Lang {
6+
7+
// Metadata.xml XPath class reference: path="/api/package[@name='java.lang']/class[@name='String']"
8+
[global::Android.Runtime.Register ("java/lang/String", DoNotGenerateAcw=true)]
9+
public partial class String : Java.Lang.Object {
10+
11+
protected String (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer) {}
12+
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Android.Runtime;
4+
5+
namespace Xamarin.Test.Invalidnames {
6+
7+
// Metadata.xml XPath class reference: path="/api/package[@name='xamarin.test.invalidnames']/class[@name='in']"
8+
[global::Android.Runtime.Register ("xamarin/test/invalidnames/in", DoNotGenerateAcw=true)]
9+
public partial class In : Java.Lang.Object {
10+
11+
protected In (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer) {}
12+
13+
}
14+
}

0 commit comments

Comments
 (0)