• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 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 package com.android.hoststubgen
17 
18 import com.android.hoststubgen.asm.ClassNodes
19 import com.android.hoststubgen.asm.getOuterClassNameFromFullClassName
20 import com.android.hoststubgen.asm.getPackageNameFromFullClassName
21 import com.android.hoststubgen.filters.FilterPolicyWithReason
22 import com.android.hoststubgen.filters.StatsLabel
23 import org.objectweb.asm.Opcodes
24 import java.io.PrintWriter
25 
26 /**
27  * This class is no longer used. It was used for the old ravenwood dashboard. (b/402797626)
28  *
29  * TODO: Delete the class.
30  */
31 open class HostStubGenStats(val classes: ClassNodes) {
32     data class Stats(
33             var supported: Int = 0,
34             var total: Int = 0,
35             val children: MutableMap<String, Stats> = mutableMapOf<String, Stats>(),
36     )
37 
38     private val stats = mutableMapOf<String, Stats>()
39 
40     fun onVisitPolicyForMethod(
41         fullClassName: String,
42         methodName: String,
43         descriptor: String,
44         policy: FilterPolicyWithReason,
45         access: Int
46     ) {
47         // Ignore methods that aren't public
48         if ((access and Opcodes.ACC_PUBLIC) == 0) return
49         // Ignore methods that are abstract
50         if ((access and Opcodes.ACC_ABSTRACT) != 0) return
51 
52         // Ignore methods where policy isn't relevant
53         val statsLabel = policy.statsLabel
54         if (statsLabel == StatsLabel.Ignored) return
55 
56         val cn = classes.findClass(fullClassName) ?: return
57 
58         val packageName = getPackageNameFromFullClassName(fullClassName)
59         val className = getOuterClassNameFromFullClassName(fullClassName)
60 
61         // Ignore methods for certain generated code
62         if (className.endsWith("Proto")
63                 or className.endsWith("ProtoEnums")
64                 or className.endsWith("LogTags")
65                 or className.endsWith("StatsLog")) {
66             return
67         }
68 
69         val packageStats = stats.getOrPut(packageName) { Stats() }
70         val classStats = packageStats.children.getOrPut(className) { Stats() }
71 
72         if (statsLabel == StatsLabel.Supported) {
73             packageStats.supported += 1
74             classStats.supported += 1
75         }
76         packageStats.total += 1
77         classStats.total += 1
78     }
79 
80     fun dumpOverview(pw: PrintWriter) {
81         pw.printf("PackageName,ClassName,SupportedMethods,TotalMethods\n")
82         stats.toSortedMap().forEach { (packageName, packageStats) ->
83             if (packageStats.supported > 0) {
84                 packageStats.children.toSortedMap().forEach { (className, classStats) ->
85                     pw.printf("%s,%s,%d,%d\n", packageName, className,
86                             classStats.supported, classStats.total)
87                 }
88             }
89         }
90     }
91 }
92