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.model.text.ApiFile 20 import com.android.tools.metalava.model.text.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>() 26 loadnull27 fun load( 28 file: File, 29 kotlinStyleNulls: Boolean? = null 30 ): Codebase { 31 return map[file] ?: run { 32 val loaded = loadFromSignatureFiles(file, kotlinStyleNulls) 33 map[file] = loaded 34 loaded 35 } 36 } 37 loadFromSignatureFilesnull38 private fun loadFromSignatureFiles( 39 file: File, 40 kotlinStyleNulls: Boolean? = null 41 ): Codebase { 42 try { 43 val codebase = ApiFile.parseApi(File(file.path), kotlinStyleNulls ?: false) 44 codebase.description = "Codebase loaded from ${file.path}" 45 return codebase 46 } catch (ex: ApiParseException) { 47 val message = "Unable to parse signature file $file: ${ex.message}" 48 throw DriverException(message) 49 } 50 } 51 loadFilesnull52 fun loadFiles(files: List<File>, kotlinStyleNulls: Boolean? = null): Codebase { 53 if (files.isEmpty()) { 54 throw IllegalArgumentException("files must not be empty") 55 } 56 try { 57 return ApiFile.parseApi(files, kotlinStyleNulls ?: false) 58 } catch (ex: ApiParseException) { 59 val message = "Unable to parse signature file: ${ex.message}" 60 throw DriverException(message) 61 } 62 } 63 } 64