blob: 43caaecebdaf04f12b8ad532244d4b7866cc1db5 [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])))
Yiming Pan090155e2023-12-08 08:29:41 +000026 var builder = FlagApiMap.newBuilder()
27 for (pkg in cb.getPackages().packages) {
28 var packageName = pkg.qualifiedName()
29 pkg.allClasses()
30 .filter { it.methods().size > 0 }
31 .forEach {
32 for (method in it.methods()) {
33 val flagValue =
34 method.modifiers
35 .findAnnotation("android.annotation.FlaggedApi")
36 ?.findAttribute("value")
37 ?.value
38 ?.value()
39 if (flagValue != null && flagValue is String) {
40 var api =
41 JavaMethod.newBuilder()
42 .setPackageName(packageName)
43 .setClassName(it.fullName())
44 .setMethodName(method.name())
45 for (param in method.parameters()) {
46 api.addParameterTypes(param.type().toTypeString())
47 }
48 if (builder.containsFlagToApi(flagValue)) {
49 var updatedApis =
50 builder
51 .getFlagToApiOrThrow(flagValue)
52 .toBuilder()
53 .addJavaMethods(api)
54 .build()
55 builder.putFlagToApi(flagValue, updatedApis)
56 } else {
57 var apis = FlaggedApis.newBuilder().addJavaMethods(api).build()
58 builder.putFlagToApi(flagValue, apis)
59 }
Yiming Pana95134c2023-11-15 00:26:25 +000060 }
61 }
62 }
Yiming Pana95134c2023-11-15 00:26:25 +000063 }
64 val flagApiMap = builder.build()
65 FileWriter(args[1]).use { it.write(flagApiMap.toString()) }
66}