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