Skip to content

Commit c51469e

Browse files
atsushienojonpryor
authored andcommitted
[generator] mark obfuscated types in generator. (#92)
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=44530 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 to bring back sanity, but instead of doing it in 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'. 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/3a7c404/JavaPackage.java#L78 Also, we had seen 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. To fix this issue, we process marking as part of StripNonBindables(). Also, it was too early to register those types to SymbolTable before stripping those types out, so changed relevant processing order. 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 c51469e

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

tools/generator/CodeGenerator.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,12 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
308308
opt.Gens = gens;
309309

310310
// disable interface default methods here, especially before validation.
311-
foreach (var gen in gens)
311+
gens = gens.Where (g => !g.IsObfuscated && g.Visibility != "private").ToList ();
312+
foreach (var gen in gens) {
312313
gen.StripNonBindables ();
314+
if (gen.IsGeneratable)
315+
AddTypeToTable (gen);
316+
}
313317

314318
Validate (gens, opt);
315319

@@ -355,6 +359,13 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
355359
gen_info.GenerateLibraryProjectFile (options, enumFiles);
356360
}
357361

362+
static void AddTypeToTable (GenBase gb)
363+
{
364+
SymbolTable.AddType (gb);
365+
foreach (var nt in gb.NestedTypes)
366+
AddTypeToTable (nt);
367+
}
368+
358369
static IEnumerable<GenBase> FlattenNestedTypes (IEnumerable<GenBase> gens)
359370
{
360371
foreach (var g in gens) {

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 && n.Visibility != "private").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

tools/generator/Parser.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ List<GenBase> ParsePackage (XmlElement ns, Predicate<XmlElement> p)
129129
nested [name] = gen;
130130
else
131131
result.Add (gen);
132-
SymbolTable.AddType (gen);
133132
}
134133

135134
foreach (string name in nested.Keys) {

0 commit comments

Comments
 (0)