1 /* 2 * 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 package com.android.hoststubgen.filters 17 18 /** 19 * Captures a [FilterPolicy] with a human-readable reason. 20 */ 21 data class FilterPolicyWithReason ( 22 val policy: FilterPolicy, 23 val reason: String = "", 24 ) { 25 /** 26 * Return a new [FilterPolicy] with an updated reason, while keeping the original reason 27 * as an "inner-reason". 28 */ wrapReasonnull29 fun wrapReason(reason: String): FilterPolicyWithReason { 30 return FilterPolicyWithReason(policy, "$reason [inner-reason: ${this.reason}]") 31 } 32 33 /** 34 * If the visibility is lower than "Keep" (meaning if it's "remove"), 35 * then return a new [FilterPolicy] with "Keep". 36 * Otherwise, return itself 37 */ promoteToKeepnull38 fun promoteToKeep(promotionReason: String): FilterPolicyWithReason { 39 if (policy.needsInImpl) { 40 return this 41 } 42 val newPolicy = if (policy.isClassWidePolicy) FilterPolicy.KeepClass else FilterPolicy.Keep 43 44 return FilterPolicyWithReason(newPolicy, 45 "$promotionReason [original remove reason: ${this.reason}]") 46 } 47 48 /** 49 * If the visibility is above "Keep" (meaning if it's "stub"), 50 * then return a new [FilterPolicy] with "Keep". 51 * Otherwise, return itself 52 */ demoteToKeepnull53 fun demoteToKeep(promotionReason: String): FilterPolicyWithReason { 54 if (!policy.needsInStub) { 55 return this 56 } 57 val newPolicy = if (policy.isClassWidePolicy) FilterPolicy.KeepClass else FilterPolicy.Keep 58 59 return FilterPolicyWithReason(newPolicy, 60 "$promotionReason [original stub reason: ${this.reason}]") 61 } 62 toStringnull63 override fun toString(): String { 64 return "[$policy - reason: $reason]" 65 } 66 67 /** Returns whether this policy should be ignored for stats. */ 68 val isIgnoredForStats: Boolean 69 get() { 70 return reason.contains("anonymous-inner-class") 71 || reason.contains("is-annotation") 72 || reason.contains("is-enum") 73 || reason.contains("is-synthetic-method") 74 || reason.contains("special-class") 75 || reason.contains("substitute-to") 76 } 77 } 78