1 /* <lambda>null2 * Copyright (C) 2023 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.cli.common.SignatureFileLoader 20 import com.android.tools.metalava.model.ClassResolver 21 import com.android.tools.metalava.model.Codebase 22 import com.android.tools.metalava.model.text.SignatureFile 23 24 private data class CacheKey( 25 val signatureFiles: List<SignatureFile>, 26 val classResolver: ClassResolver? 27 ) 28 29 /** Loads signature files, caching them for reuse where appropriate. */ 30 class SignatureFileCache(private val signatureFileLoader: SignatureFileLoader) : 31 SignatureFileLoader { 32 private val map = mutableMapOf<CacheKey, Codebase>() 33 34 override fun load( 35 signatureFiles: List<SignatureFile>, 36 classResolver: ClassResolver?, 37 ): Codebase { 38 val key = CacheKey(signatureFiles, classResolver) 39 return map.computeIfAbsent(key) { k -> 40 signatureFileLoader.load(k.signatureFiles, k.classResolver).apply { 41 // Freeze the classes before caching to avoid any changes being made to cached and 42 // potentially shared objects. 43 freezeClasses() 44 } 45 } 46 } 47 } 48