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.ClassResolver 20 import com.android.tools.metalava.model.Codebase 21 import com.android.tools.metalava.model.text.ApiFile 22 import com.android.tools.metalava.model.text.ApiParseException 23 import com.android.tools.metalava.model.text.FileFormat 24 import com.android.tools.metalava.model.text.SignatureFile 25 26 /** Supports loading [SignatureFile]s into a [Codebase] using an optional [ClassResolver]. */ 27 interface SignatureFileLoader { 28 /** Load [signatureFiles] into a [Codebase] using the optional [classResolver]. */ loadnull29 fun load(signatureFiles: List<SignatureFile>, classResolver: ClassResolver? = null): Codebase 30 } 31 32 /** 33 * Helper object to load signature files and rethrow any [ApiParseException] as a 34 * [MetalavaCliException]. 35 */ 36 class DefaultSignatureFileLoader( 37 private val codebaseConfig: Codebase.Config, 38 private val formatForLegacyFiles: FileFormat? = null, 39 ) : SignatureFileLoader { 40 41 override fun load( 42 signatureFiles: List<SignatureFile>, 43 classResolver: ClassResolver?, 44 ): Codebase { 45 require(signatureFiles.isNotEmpty()) { "files must not be empty" } 46 47 try { 48 return ApiFile.parseApi( 49 signatureFiles = signatureFiles, 50 codebaseConfig = codebaseConfig, 51 classResolver = classResolver, 52 formatForLegacyFiles = formatForLegacyFiles, 53 ) 54 } catch (ex: ApiParseException) { 55 cliError("Unable to parse signature file: ${ex.message}") 56 } 57 } 58 } 59