1 /* 2 * 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.model.Codebase 20 import com.android.tools.metalava.model.source.EnvironmentManager 21 import com.android.tools.metalava.reporter.FileLocation 22 import com.android.tools.metalava.reporter.Issues 23 import com.android.tools.metalava.reporter.Issues.Category 24 import com.android.tools.metalava.reporter.Reportable 25 import com.android.tools.metalava.reporter.Reporter 26 import com.android.tools.metalava.reporter.Severity 27 28 /** 29 * A special [Reporter] that is passed to [EnvironmentManager.createSourceParser] which will in turn 30 * be passed through and be accessible as [Codebase.reporter]. 31 * 32 * This will redirect the reports based on the [Category] to an appropriate [Reporter]. This is 33 * needed because a [Reporter] will produce a custom error message if any errors were reported 34 * through it and reporting an inappropriate issues could cause confusion. e.g. reporting an 35 * unresolved import through an API Lint [Reporter] would suggest that it be resolved by using 36 * `@SuppressWarnings` but that would not work. 37 * 38 * Issues in the [Category.COMPATIBILITY] should only be reported from the specific compatibility 39 * check not within the [Codebase] so attempting to report one of them will result in an error. 40 */ 41 class CategoryRedirectingReporter( 42 /** Destination for [Issues.Issue] in [Category.UNKNOWN]. */ 43 private val defaultReporter: Reporter, 44 /** Destination for [Issues.Issue] in [Category.API_LINT] and [Category.DOCUMENTATION]. */ 45 private val apiLintReporter: Reporter = defaultReporter, 46 /** Destination for [Issues.Issue] in [Category.COMPATIBILITY]. */ 47 private val compatibilityReporter: Reporter = defaultReporter, 48 ) : Reporter { 49 reportnull50 override fun report( 51 id: Issues.Issue, 52 reportable: Reportable?, 53 message: String, 54 location: FileLocation, 55 maximumSeverity: Severity 56 ) = 57 when (id.category) { 58 Category.API_LINT, 59 Category.DOCUMENTATION -> 60 apiLintReporter.report(id, reportable, message, location, maximumSeverity) 61 Category.COMPATIBILITY -> 62 compatibilityReporter.report(id, reportable, message, location, maximumSeverity) 63 else -> defaultReporter.report(id, reportable, message, location, maximumSeverity) 64 } 65 isSuppressednull66 override fun isSuppressed(id: Issues.Issue, reportable: Reportable?, message: String?) = 67 // It does not matter which reporter this is delegated to as it only depends on the 68 // reportable and the issue configuration which is identical for both. 69 defaultReporter.isSuppressed(id, reportable, message) 70 } 71