• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.doclava1.ApiFile
20 import com.android.tools.metalava.doclava1.ApiParseException
21 import com.android.tools.metalava.model.Codebase
22 import java.io.File
23 
24 object SignatureFileLoader {
25     private val map = mutableMapOf<File, Codebase>()
loadnull26     fun load(
27         file: File,
28         kotlinStyleNulls: Boolean? = null
29     ): Codebase {
30         return map[file] ?: run {
31             val loaded = loadFromSignatureFiles(file, kotlinStyleNulls)
32             map[file] = loaded
33             loaded
34         }
35     }
36 
loadFromSignatureFilesnull37     private fun loadFromSignatureFiles(
38         file: File,
39         kotlinStyleNulls: Boolean? = null
40     ): Codebase {
41         try {
42             val codebase = ApiFile.parseApi(File(file.path), kotlinStyleNulls)
43             codebase.description = "Codebase loaded from ${file.path}"
44             return codebase
45         } catch (ex: ApiParseException) {
46             val message = "Unable to parse signature file $file: ${ex.message}"
47             throw DriverException(message)
48         }
49     }
50 }