• 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.tools.metalava.model.Codebase
20 import com.android.tools.metalava.model.text.ApiFile
21 import com.android.tools.metalava.model.text.ApiParseException
22 import java.io.File
23 
24 object SignatureFileLoader {
25     private val map = mutableMapOf<File, Codebase>()
26 
27     fun load(file: File, kotlinStyleNulls: Boolean = false): Codebase {
28         return map[file] ?: run {
29             val loaded = loadFiles(listOf(file), kotlinStyleNulls)
30             map[file] = loaded
31             loaded
32         }
33     }
34 
35     fun loadFiles(files: List<File>, kotlinStyleNulls: Boolean = false): Codebase {
36         require(files.isNotEmpty()) { "files must not be empty" }
37 
38         try {
39             val codebase = ApiFile.parseApi(files, kotlinStyleNulls)
40 
41             // Unlike loadFromSources, analyzer methods are not required for text based codebase
42             // because all methods in the API text file belong to an API surface.
43             val analyzer = ApiAnalyzer(codebase)
44             analyzer.addConstructors { _ -> true }
45             return codebase
46         } catch (ex: ApiParseException) {
47             throw DriverException("Unable to parse signature file: ${ex.message}")
48         }
49     }
50 }
51