Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02525a88 authored by Mårten Kongstad's avatar Mårten Kongstad
Browse files

check-flagged-apis: api-versions.xml: correctly parse nested class ctor

The constructor of a nested class is represented as follows in
api-versions.xml:

  <class name="android/Clazz$Foo$Bar" since="1">
    <method name="&lt;init>()V"/>
  </class>

The nested dollar signs are not replaced by forward slashes before the
parsing logic uses `split("/")` to find the name of the inner-most
class, incorrectly resulting in `Class$Foo$Bar` instead of `Bar`. Fix
this by immediately replacing dollar signs with forward slashes after
extracting the package and class.

Also clean up the following call of `Symbol.create`.

Bug: 334870672
Test: atest --host check-flagged-apis-test
Change-Id: I8c0619faae90ded7eb14dcc20ecb94a086a1c764
parent 5a990562
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -139,6 +139,27 @@ class CheckFlaggedApisTest {
    assertEquals(expected, actual)
  }

  @Test
  fun testParseApiVersionsNestedClasses() {
    val apiVersions =
        """
          <?xml version="1.0" encoding="utf-8"?>
          <api version="3">
            <class name="android/Clazz${'$'}Foo${'$'}Bar" since="1">
              <method name="&lt;init>()V"/>
            </class>
          </api>
        """
            .trim()
    val expected: Set<Symbol> =
        setOf(
            Symbol("android/Clazz/Foo/Bar"),
            Symbol("android/Clazz/Foo/Bar/Bar()"),
        )
    val actual = parseApiVersions(apiVersions.byteInputStream())
    assertEquals(expected, actual)
  }

  @Test
  fun testFindErrorsNoErrors() {
    val expected = setOf<ApiError>()
+4 −3
Original line number Diff line number Diff line
@@ -281,10 +281,11 @@ internal fun parseApiVersions(input: InputStream): Set<Symbol> {
        requireNotNull(method.getParentNode()?.getAttribute("name")) {
              "Bad XML: top level <method> element, or <class> element missing name attribute"
            }
            .replace("$", "/")
    if (methodName == "<init>") {
      methodName = packageAndClassName.split("/").last()
    }
    output.add(Symbol.create("${packageAndClassName.replace("/", ".")}.$methodName($methodArgs)"))
    output.add(Symbol.create("$packageAndClassName/$methodName($methodArgs)"))
  }

  return output