1 /*
2  * Copyright (C) 2018 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 
17 package com.android.tools.metalava
18 
19 import com.android.tools.metalava.model.ClassItem
20 import com.android.tools.metalava.model.FieldItem
21 import com.android.tools.metalava.model.Item
22 import com.android.tools.metalava.model.MethodItem
23 import com.android.tools.metalava.model.visitors.ApiVisitor
24 import java.io.PrintWriter
25 import java.util.function.Predicate
26 
27 class DexApiWriter(
28     private val writer: PrintWriter,
29     filterEmit: Predicate<Item>,
30     filterReference: Predicate<Item>
31 ) : ApiVisitor(
32     visitConstructorsAsMethods = true,
33     nestInnerClasses = false,
34     inlineInheritedFields = true,
35     filterEmit = filterEmit,
36     filterReference = filterReference
37 ) {
visitClassnull38     override fun visitClass(cls: ClassItem) {
39         if (filterEmit.test(cls)) {
40             writer.print(cls.toType().internalName())
41             writer.print("\n")
42         }
43     }
44 
visitMethodnull45     override fun visitMethod(method: MethodItem) {
46         if (method.inheritedMethod) {
47             return
48         }
49 
50         writer.print(method.containingClass().toType().internalName())
51         writer.print("->")
52         writer.print(method.internalName())
53         writer.print("(")
54         for (pi in method.parameters()) {
55             writer.print(pi.type().internalName())
56         }
57         writer.print(")")
58         if (method.isConstructor()) {
59             writer.print("V")
60         } else {
61             val returnType = method.returnType()
62             writer.print(returnType?.internalName() ?: "V")
63         }
64         writer.print("\n")
65     }
66 
visitFieldnull67     override fun visitField(field: FieldItem) {
68         val cls = field.containingClass()
69 
70         writer.print(cls.toType().internalName())
71         writer.print("->")
72         writer.print(field.name())
73         writer.print(":")
74         writer.print(field.type().internalName())
75         writer.print("\n")
76     }
77 }