Merge "check-flagged-apis: parse API versions XML" into main

This commit is contained in:
Mårten Kongstad 2024-04-17 14:41:32 +00:00 committed by Gerrit Code Review
commit 7978682841
2 changed files with 53 additions and 0 deletions

View file

@ -52,6 +52,18 @@ private val PARSED_FLAGS =
ByteArrayInputStream(binaryProto.toByteArray()) ByteArrayInputStream(binaryProto.toByteArray())
}() }()
private val API_VERSIONS =
"""
<?xml version="1.0" encoding="utf-8"?>
<api version="3">
<class name="android/Clazz" since="1">
<method name="&lt;init>()V"/>
<field name="FOO"/>
</class>
</api>
"""
.trim()
@RunWith(DeviceJUnit4ClassRunner::class) @RunWith(DeviceJUnit4ClassRunner::class)
class CheckFlaggedApisTest : BaseHostJUnit4Test() { class CheckFlaggedApisTest : BaseHostJUnit4Test() {
@Test @Test
@ -67,4 +79,11 @@ class CheckFlaggedApisTest : BaseHostJUnit4Test() {
val actual = parseFlagValues(PARSED_FLAGS) val actual = parseFlagValues(PARSED_FLAGS)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@Test
fun testParseApiVersions() {
val expected: Set<Symbol> = setOf(Symbol("android.Clazz.FOO"))
val actual = parseApiVersions(API_VERSIONS.byteInputStream())
assertEquals(expected, actual)
}
} }

View file

@ -28,6 +28,8 @@ import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.path import com.github.ajalt.clikt.parameters.types.path
import java.io.InputStream import java.io.InputStream
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.Node
/** /**
* Class representing the fully qualified name of a class, method or field. * Class representing the fully qualified name of a class, method or field.
@ -108,6 +110,16 @@ The tool will exit with a non-zero exit code if any flagged APIs are found to be
""") """)
.path(mustExist = true, canBeDir = false, mustBeReadable = true) .path(mustExist = true, canBeDir = false, mustBeReadable = true)
.required() .required()
private val apiVersionsPath by
option("--api-versions")
.help(
"""
Path to API versions XML file.
Usually named xml-versions.xml.
Tip: `m sdk dist` will generate a file that includes all platform and mainline APIs.
""")
.path(mustExist = true, canBeDir = false, mustBeReadable = true)
.required()
override fun run() { override fun run() {
@Suppress("UNUSED_VARIABLE") @Suppress("UNUSED_VARIABLE")
@ -117,6 +129,8 @@ The tool will exit with a non-zero exit code if any flagged APIs are found to be
} }
@Suppress("UNUSED_VARIABLE") @Suppress("UNUSED_VARIABLE")
val flags = flagValuesPath.toFile().inputStream().use { parseFlagValues(it) } val flags = flagValuesPath.toFile().inputStream().use { parseFlagValues(it) }
@Suppress("UNUSED_VARIABLE")
val exportedSymbols = apiVersionsPath.toFile().inputStream().use { parseApiVersions(it) }
throw ProgramResult(0) throw ProgramResult(0)
} }
} }
@ -151,4 +165,24 @@ internal fun parseFlagValues(input: InputStream): Map<Flag, Boolean> {
{ it.getState() == Aconfig.flag_state.ENABLED }) { it.getState() == Aconfig.flag_state.ENABLED })
} }
internal fun parseApiVersions(input: InputStream): Set<Symbol> {
fun Node.getAttribute(name: String): String? = getAttributes()?.getNamedItem(name)?.getNodeValue()
val output = mutableSetOf<Symbol>()
val factory = DocumentBuilderFactory.newInstance()
val parser = factory.newDocumentBuilder()
val document = parser.parse(input)
val fields = document.getElementsByTagName("field")
// ktfmt doesn't understand the `..<` range syntax; explicitly call .rangeUntil instead
for (i in 0.rangeUntil(fields.getLength())) {
val field = fields.item(i)
val fieldName = field.getAttribute("name")
val className =
requireNotNull(field.getParentNode()) { "Bad XML: top level <field> element" }
.getAttribute("name")
output.add(Symbol.create("$className.$fieldName"))
}
return output
}
fun main(args: Array<String>) = CheckCommand().main(args) fun main(args: Array<String>) = CheckCommand().main(args)