• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.SdkConstants.DOT_TXT
20 import com.android.tools.metalava.doclava1.ApiPredicate
21 import com.android.tools.metalava.doclava1.ElidingPredicate
22 import com.android.tools.metalava.doclava1.FilterPredicate
23 import com.android.tools.metalava.model.Codebase
24 import com.android.tools.metalava.model.Item
25 import java.io.File
26 import java.util.function.Predicate
27 
28 /** Types of APIs emitted (or parsed etc) */
29 enum class ApiType(val flagName: String, val displayName: String = flagName) {
30     /** The public API */
31     PUBLIC_API("api", "public") {
32         override fun getOptionFile(): File? {
33             return options.apiFile
34         }
35 
36         override fun getEmitFilter(): Predicate<Item> {
37             val apiFilter = FilterPredicate(ApiPredicate())
38             val apiReference = ApiPredicate(ignoreShown = true)
39             return apiFilter.and(ElidingPredicate(apiReference))
40         }
41 
42         override fun getReferenceFilter(): Predicate<Item> {
43             return ApiPredicate(ignoreShown = true)
44         }
45     },
46 
47     /** The API that has been removed */
48     REMOVED("removed", "removed") {
49         override fun getOptionFile(): File? {
50             return options.removedApiFile
51         }
52 
53         override fun getEmitFilter(): Predicate<Item> {
54             val removedFilter = FilterPredicate(ApiPredicate(matchRemoved = true))
55             val removedReference = ApiPredicate(ignoreShown = true, ignoreRemoved = true)
56             return removedFilter.and(ElidingPredicate(removedReference))
57         }
58 
59         override fun getReferenceFilter(): Predicate<Item> {
60             return ApiPredicate(ignoreShown = true, ignoreRemoved = true)
61         }
62     },
63 
64     /** The private API */
65     PRIVATE("private", "private") {
66         override fun getOptionFile(): File? {
67             return options.privateApiFile
68         }
69 
70         override fun getEmitFilter(): Predicate<Item> {
71             val apiFilter = FilterPredicate(ApiPredicate())
72             val memberIsNotCloned: Predicate<Item> = Predicate { !it.isCloned() }
73             return memberIsNotCloned.and(apiFilter.negate())
74         }
75 
76         override fun getReferenceFilter(): Predicate<Item> {
77             return Predicate { true }
78         }
79     },
80 
81     /** Everything */
82     ALL("all", "all") {
83         override fun getOptionFile(): File? {
84             return null
85         }
86 
87         override fun getEmitFilter(): Predicate<Item> {
88             return Predicate { it.emit }
89         }
90 
91         override fun getReferenceFilter(): Predicate<Item> {
92             return Predicate { true }
93         }
94     };
95 
96     /** Returns the user-configured file where the API has been written to, if any */
97     abstract fun getOptionFile(): File?
98 
99     abstract fun getEmitFilter(): Predicate<Item>
100 
101     abstract fun getReferenceFilter(): Predicate<Item>
102 
103     override fun toString(): String = displayName
104 
105     /**
106      * Gets the signature file for the given API type. Will create it if not already
107      * created.
108      */
109     fun getSignatureFile(codebase: Codebase, defaultName: String): File {
110         val apiType = this
111         return apiType.getOptionFile() ?: run {
112             val tempFile = createTempFile(defaultName, DOT_TXT)
113             tempFile.deleteOnExit()
114             val apiEmit = apiType.getEmitFilter()
115             val apiReference = apiType.getReferenceFilter()
116 
117             createReportFile(codebase, tempFile, null) { printWriter ->
118                 SignatureWriter(printWriter, apiEmit, apiReference, codebase.preFiltered)
119             }
120 
121             tempFile
122         }
123     }
124 }
125