Skip to content

Commit e23f6d5

Browse files
committed
[generator] (class-parse regression) mark obfuscated types in generator.
Basically this is part of the problems reported at https://bugzilla.xamarin.com/show_bug.cgi?id=44263 In good old `jar2xml` era, we marked "obfuscated" types at jar2xml so that those types are not generated in the API XML. class-parse is not that clever and generates everything as is. We could make changes to api-xml-adjuster, but instead of doing the same as jar2xml, we had better bring in flexibility to obfuscation marking. That is - - If there is `obfuscated='false'` attribute specification by API metadata fixup, then do not mark as obfuscated in any case. - If not, process any lowercase-named types and nested types as obfuscated, such as 'a', 'SomeType$a', or 'SomeType$b$a'. We had been seeing some obfuscated types like `b$a` as NOT marked as obfuscated in the past, because we were checking only a,aa,aaa,aaaa and aaaaa as the parent type for nested types. That should be fixed. Due to code structures, we cannot take the same code path as jar2xml (we don't hold list of sibling classes when processing an XML element). https://github.com/xamarin/jar2xml/blob/master/JavaPackage.java#L78 To fix this issue, we process marking as part of StripNonBindables(). Fixing bug #44263 then exposed another problem; StripNonBindables() was not complete and it was not processing nested C# types at all. Thus we had generated non-bindable code before. This change fixes this issue at the same time, for fixing the bug. Thus, this will result in "API breakage" looking changes in the end, but the bindings were invalid and should be removed without compatibility concern.
1 parent de64e7b commit e23f6d5

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

tools/generator/CodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
304304
if (gens == null) {
305305
return;
306306
}
307+
gens = gens.Where (g => !g.IsObfuscated && g.Visibility != "private").ToList ();
307308
apiSource = p.ApiSource;
308309
opt.Gens = gens;
309310

tools/generator/GenBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public bool IsDeprecated {
5353
get { return support.IsDeprecated; }
5454
}
5555

56+
public bool IsObfuscated {
57+
get { return support.IsObfuscated; }
58+
}
59+
5660
public string DeprecatedComment {
5761
get { return support.DeprecatedComment; }
5862
}
@@ -718,6 +722,9 @@ public void StripNonBindables ()
718722
// have to "implement" those methods because they are declared and you have to implement
719723
// any declared methods in C#. That is going to be problematic a lot.
720724
methods = methods.Where (m => !m.IsInterfaceDefaultMethod).ToList ();
725+
nested_types = nested_types.Where (n => !n.IsObfuscated).ToList ();
726+
foreach (var n in nested_types)
727+
n.StripNonBindables ();
721728
}
722729

723730
public void FillProperties ()

tools/generator/GenBaseSupport.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public abstract class GenBaseSupport
1818
public abstract string DeprecatedComment { get; }
1919
public abstract bool IsGeneratable { get; }
2020
public abstract bool IsGeneric { get; }
21+
public abstract bool IsObfuscated { get; }
2122
public abstract string FullName { get; set; }
2223
public abstract string Name { get; set; }
2324
public abstract string Namespace { get; }
@@ -92,6 +93,10 @@ public override bool IsAcw {
9293
public override bool IsDeprecated {
9394
get { return deprecated; }
9495
}
96+
97+
public override bool IsObfuscated {
98+
get { return false; } // obfuscated types have no chance to be already bound in managed types.
99+
}
95100

96101
public override string DeprecatedComment {
97102
get { return deprecatedComment; }
@@ -198,8 +203,8 @@ public XmlGenBaseSupport (XmlElement pkg, XmlElement elem)
198203
name = TypeNamePrefix + raw_name;
199204
full_name = String.Format ("{0}.{1}{2}", ns, idx > 0 ? StringRocks.TypeToPascalCase (java_name.Substring (0, idx + 1)) : String.Empty, name);
200205
}
201-
//marshaler = elem.XGetAttribute ("marshaler");
202-
//if (!string.IsNullOrEmpty (marshaler)) throw new Exception ();
206+
207+
obfuscated = IsObfuscatedName (java_name) && elem.XGetAttribute ("obfuscated") != "false";
203208
}
204209

205210
public override bool IsAcw {
@@ -219,6 +224,11 @@ public override string DeprecatedComment {
219224
public override bool IsGeneratable {
220225
get { return true; }
221226
}
227+
228+
bool obfuscated;
229+
public override bool IsObfuscated {
230+
get { return obfuscated; }
231+
}
222232

223233
string java_name;
224234
public override string JavaSimpleName {
@@ -291,6 +301,17 @@ public override bool ValidateNamespace ()
291301
}
292302
return true;
293303
}
304+
305+
bool IsObfuscatedName (String name)
306+
{
307+
if (name.StartsWith ("R.", StringComparison.Ordinal))
308+
return false;
309+
int idx = name.LastIndexOf ('.');
310+
string last = idx < 0 ? name : name.Substring (idx + 1);
311+
if (last.Any (c => (c < 'a' || 'z' < c) && (c < '0' || '9' < c)))
312+
return false;
313+
return true;
314+
}
294315
}
295316

296317
public class InterfaceXmlGenBaseSupport : XmlGenBaseSupport

0 commit comments

Comments
 (0)