1 /* <lambda>null2 * Copyright (C) 2024 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.MetalavaSubCommand 20 import com.android.tools.metalava.cli.common.executionEnvironment 21 import com.android.tools.metalava.cli.common.existingFile 22 import com.android.tools.metalava.cli.common.newFile 23 import com.android.tools.metalava.cli.common.progressTracker 24 import com.android.tools.metalava.cli.common.stderr 25 import com.android.tools.metalava.jar.StandaloneJarCodebaseLoader 26 import com.android.tools.metalava.model.CodebaseFragment 27 import com.android.tools.metalava.model.visitors.ApiPredicate 28 import com.android.tools.metalava.model.visitors.ApiType 29 import com.android.tools.metalava.reporter.BasicReporter 30 import com.github.ajalt.clikt.parameters.arguments.argument 31 32 /** Generates a JDiff XML file from a jar. */ 33 class JarToJDiffCommand : 34 MetalavaSubCommand( 35 help = 36 """ 37 Convert a jar file into a file in the JDiff XML format. 38 39 This is intended for use by the coverage team to extract information needed to 40 determine test coverage of the API from the stubs jars. Any other use is 41 unsupported. 42 """ 43 .trimIndent() 44 ) { 45 46 private val jarFile by 47 argument( 48 name = "<jar-file>", 49 help = 50 """ 51 Jar file to convert to the JDiff XML format. 52 """ 53 .trimIndent() 54 ) 55 .existingFile() 56 57 private val xmlFile by 58 argument( 59 name = "<xml-file>", 60 help = 61 """ 62 Output JDiff XML format file. 63 """ 64 .trimIndent() 65 ) 66 .newFile() 67 68 override fun run() { 69 // Make sure that none of the code called by this command accesses the global `options` 70 // property. 71 OptionsDelegate.disallowAccess() 72 73 StandaloneJarCodebaseLoader.create( 74 executionEnvironment.disableStderrDumping(), 75 progressTracker, 76 BasicReporter(stderr) 77 ) 78 .use { jarCodebaseLoader -> 79 val codebase = jarCodebaseLoader.loadFromJarFile(jarFile) 80 81 val apiType = ApiType.PUBLIC_API 82 val apiPredicateConfig = ApiPredicate.Config() 83 val apiFilters = apiType.getApiFilters(apiPredicateConfig) 84 85 val codebaseFragment = 86 CodebaseFragment.create(codebase) { delegate -> 87 createFilteringVisitorForJDiffWriter( 88 delegate, 89 apiFilters = apiFilters, 90 preFiltered = false, 91 showUnannotated = false, 92 ) 93 } 94 95 createReportFile(progressTracker, codebaseFragment, xmlFile, "JDiff File") { 96 printWriter -> 97 JDiffXmlWriter( 98 writer = printWriter, 99 ) 100 } 101 } 102 } 103 } 104