blob: 948e64f22f2021a5acff85a4deaa281364b6d462 [file] [log] [blame]
Yiming Pana95134c2023-11-15 00:26:25 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.platform.coverage
18
19import com.android.tools.metalava.model.text.ApiFile
20import java.io.File
21import java.io.FileWriter
22
23/** Usage: extract-flagged-apis <api text file> <output .pb file> */
24fun main(args: Array<String>) {
25 var cb = ApiFile.parseApi(listOf(File(args[0])))
26 val flagToApi = mutableMapOf<String, MutableList<String>>()
27 cb.getPackages()
28 .allTopLevelClasses()
29 .filter { it.methods().size > 0 }
30 .forEach {
31 for (method in it.methods()) {
32 val flagValue =
33 method.modifiers
34 .findAnnotation("android.annotation.FlaggedApi")
35 ?.findAttribute("value")
36 ?.value
37 ?.value()
38 if (flagValue != null && flagValue is String) {
39 val methodQualifiedName = "${it.qualifiedName()}.${method.name()}"
40 if (flagToApi.containsKey(flagValue)) {
41 flagToApi.get(flagValue)?.add(methodQualifiedName)
42 } else {
43 flagToApi.put(flagValue, mutableListOf(methodQualifiedName))
44 }
45 }
46 }
47 }
48 var builder = FlagApiMap.newBuilder()
49 for (flag in flagToApi.keys) {
50 var flaggedApis = FlaggedApis.newBuilder()
51 for (method in flagToApi.get(flag).orEmpty()) {
52 flaggedApis.addFlaggedApi(FlaggedApi.newBuilder().setQualifiedName(method))
53 }
54 builder.putFlagToApi(flag, flaggedApis.build())
55 }
56 val flagApiMap = builder.build()
57 FileWriter(args[1]).use { it.write(flagApiMap.toString()) }
58}