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.cli.common
18 
19 import com.android.tools.metalava.model.AnnotationManager
20 import com.android.tools.metalava.model.ClassResolver
21 import com.android.tools.metalava.model.Codebase
22 import com.android.tools.metalava.model.text.ApiFile
23 import com.android.tools.metalava.model.text.ApiParseException
24 import com.android.tools.metalava.model.text.FileFormat
25 import com.android.tools.metalava.model.text.SignatureFile
26 
27 /**
28  * Helper object to load signature files and rethrow any [ApiParseException] as a
29  * [MetalavaCliException].
30  */
31 class SignatureFileLoader(
32     private val annotationManager: AnnotationManager,
33     private val formatForLegacyFiles: FileFormat? = null,
34 ) {
loadnull35     fun load(
36         file: SignatureFile,
37         classResolver: ClassResolver? = null,
38     ): Codebase {
39         return loadFiles(listOf(file), classResolver)
40     }
41 
loadFilesnull42     fun loadFiles(
43         files: List<SignatureFile>,
44         classResolver: ClassResolver? = null,
45     ): Codebase {
46         require(files.isNotEmpty()) { "files must not be empty" }
47 
48         try {
49             return ApiFile.parseApi(
50                 signatureFiles = files,
51                 annotationManager = annotationManager,
52                 classResolver = classResolver,
53                 formatForLegacyFiles = formatForLegacyFiles,
54             )
55         } catch (ex: ApiParseException) {
56             throw MetalavaCliException("Unable to parse signature file: ${ex.message}")
57         }
58     }
59 }
60