Skip to content

[generator] Ignore types without names. #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/generator-Tests/Unit-Tests/XmlApiImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,41 @@ public void IgnoreKotlinInternalMembers ()
Assert.AreEqual (0, klass.Fields.Count);
Assert.AreEqual (0, klass.Methods.Count);
}

[Test]
public void IgnoreTypesWithInvalidNames ()
{
var xml = XDocument.Parse (@"
<api>
<package name='com.example.test' jni-name='com/example/test'>
<class name='' visibility='public' />
<class name='Document.' visibility='public' />
<interface name='' visibility='public' />
<interface name='Document.' visibility='public' />
</package>
</api>");

var gens = XmlApiImporter.Parse (xml, opt);

// None of these should be parsed because they have invalid names
Assert.AreEqual (0, gens.Count);
}

[Test]
public void IgnoreUserObfuscatedTypes ()
{
var xml = XDocument.Parse (@"
<api>
<package name='com.example.test' jni-name='com/example/test'>
<class name='MyClass' visibility='public' obfuscated='true' />
<interface name='MyInterface' visibility='public' obfuscated='true' />
</package>
</api>");

var gens = XmlApiImporter.Parse (xml, opt);

// None of these should be parsed because the user has said they are obfuscated
Assert.AreEqual (0, gens.Count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ public static List<GenBase> ParsePackage (XElement ns, CodeGenerationOptions opt

switch (elem.Name.LocalName) {
case "class":
if (elem.XGetAttribute ("obfuscated") == "true")
continue;
gen = CreateClass (ns, elem, options);
if (ShouldBind (elem))
gen = CreateClass (ns, elem, options);
break;
case "interface":
if (elem.XGetAttribute ("obfuscated") == "true")
continue;
gen = CreateInterface (ns, elem, options);
if (ShouldBind (elem))
gen = CreateInterface (ns, elem, options);
break;
default:
Report.LogCodedWarning (0, Report.WarningUnexpectedPackageChildNode, elem.Name.ToString ());
Expand Down Expand Up @@ -522,5 +520,20 @@ static void SetLineInfo (ISourceLineInfo model, XNode node, CodeGenerationOption
model.LinePosition = info.LinePosition;
}
}

static bool ShouldBind (XElement elem)
{
// Don't bind things the user has said are "obfuscated"
if (elem.XGetAttribute ("obfuscated") == "true")
return false;

var java_name = elem.XGetAttribute ("name");

// Ignore types that do not have a name (nested classes would end in a period like "Document.")
if (!java_name.HasValue () || java_name.EndsWith (".", StringComparison.Ordinal))
return false;

return true;
}
}
}