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  * Base class for an [OutputFilter] that uses another filter as a fallback.
20  */
21 abstract class DelegatingFilter(
22         // fallback shouldn't be used by subclasses directly, so make it private.
23         // They should instead be calling into `super` or `outermostFilter`.
24         private val fallback: OutputFilter
25 ) : OutputFilter() {
26     init {
27         fallback.outermostFilter = this
28     }
29 
30     /**
31      * Returns the outermost filter in a filter chain.
32      *
33      * When methods in a subclass needs to refer to a policy on an item (class, fields, methods)
34      * that are not the "subject" item -- e.g.
35      * in [ClassWidePolicyPropagatingFilter.getPolicyForField], when it checks the
36      * class policy -- [outermostFilter] must be used, rather than the super's method.
37      * The former will always return the correct policy, but the later won't consult outer
38      * filters than the current filter.
39      */
40     override var outermostFilter: OutputFilter = this
41         get() = field
42         set(value) {
43             field = value
44             // Propagate to the inner filters.
45             fallback.outermostFilter = value
46         }
47 
getPolicyForClassnull48     override fun getPolicyForClass(className: String): FilterPolicyWithReason {
49         return fallback.getPolicyForClass(className)
50     }
51 
getPolicyForFieldnull52     override fun getPolicyForField(
53             className: String,
54             fieldName: String
55     ): FilterPolicyWithReason {
56         return fallback.getPolicyForField(className, fieldName)
57     }
58 
getPolicyForMethodnull59     override fun getPolicyForMethod(
60             className: String,
61             methodName: String,
62             descriptor: String
63     ): FilterPolicyWithReason {
64         return fallback.getPolicyForMethod(className, methodName, descriptor)
65     }
66 
getRenameTonull67     override fun getRenameTo(
68             className: String,
69             methodName: String,
70             descriptor: String
71     ): String? {
72         return fallback.getRenameTo(className, methodName, descriptor)
73     }
74 
getNativeSubstitutionClassnull75     override fun getNativeSubstitutionClass(className: String): String? {
76         return fallback.getNativeSubstitutionClass(className)
77     }
78 
getClassLoadHooksnull79     override fun getClassLoadHooks(className: String): List<String> {
80         return fallback.getClassLoadHooks(className)
81     }
82 
getMethodCallHooksnull83     override fun getMethodCallHooks(
84         className: String,
85         methodName: String,
86         descriptor: String
87     ): List<String> {
88         return fallback.getMethodCallHooks(className, methodName, descriptor)
89     }
90 }
91