1 /*
2  * Copyright (C) 2022 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.google.android.lint.aidl
18 
19 import com.android.tools.lint.detector.api.JavaContext
20 import com.android.tools.lint.detector.api.LintFix
21 import com.android.tools.lint.detector.api.Location
22 import com.intellij.psi.PsiClass
23 import com.intellij.psi.PsiReferenceList
24 import org.jetbrains.uast.UMethod
25 
26 /**
27  * Given a UMethod, determine if this method is the entrypoint to an interface
28  * generated by AIDL, returning the interface name if so, otherwise returning
29  * null
30  */
getContainingAidlInterfacenull31 fun getContainingAidlInterface(context: JavaContext, node: UMethod): String? {
32     val containingStub = containingStub(context, node) ?: return null
33     val superMethod = node.findSuperMethods(containingStub)
34     if (superMethod.isEmpty()) return null
35     return containingStub.containingClass?.name
36 }
37 
38 /* Returns the containing Stub class if any. This is not sufficient to infer
39  * that the method itself extends an AIDL generated method. See
40  * getContainingAidlInterface for that purpose.
41  */
containingStubnull42 fun containingStub(context: JavaContext, node: UMethod?): PsiClass? {
43     var superClass = node?.containingClass?.superClass
44     while (superClass != null) {
45         if (isStub(context, superClass)) return superClass
46         superClass = superClass.superClass
47     }
48     return null
49 }
50 
isStubnull51 private fun isStub(context: JavaContext, psiClass: PsiClass?): Boolean {
52     if (psiClass == null) return false
53     if (psiClass.name != "Stub") return false
54     if (!context.evaluator.isStatic(psiClass)) return false
55     if (!context.evaluator.isAbstract(psiClass)) return false
56 
57     if (!hasSingleAncestor(psiClass.extendsList, BINDER_CLASS)) return false
58 
59     val parent = psiClass.parent as? PsiClass ?: return false
60     if (!hasSingleAncestor(parent.extendsList, IINTERFACE_INTERFACE)) return false
61 
62     val parentName = parent.qualifiedName ?: return false
63     if (!hasSingleAncestor(psiClass.implementsList, parentName)) return false
64 
65     return true
66 }
67 
hasSingleAncestornull68 private fun hasSingleAncestor(references: PsiReferenceList?, qualifiedName: String) =
69         references != null &&
70                 references.referenceElements.size == 1 &&
71                 references.referenceElements[0].qualifiedName == qualifiedName
72 
73 fun getHelperMethodCallSourceString(node: UMethod) = "${node.name}$AIDL_PERMISSION_HELPER_SUFFIX()"
74 
75 fun getHelperMethodFix(
76     node: UMethod,
77     manualCheckLocation: Location,
78     prepend: Boolean = true
79 ): LintFix {
80     val helperMethodSource = getHelperMethodCallSourceString(node)
81     val indent = " ".repeat(manualCheckLocation.start?.column ?: 0)
82     val newText = "$helperMethodSource;${if (prepend) "\n\n$indent" else ""}"
83 
84     val fix = LintFix.create()
85             .replace()
86             .range(manualCheckLocation)
87             .with(newText)
88             .reformat(true)
89             .autoFix()
90 
91     if (prepend) fix.beginning()
92 
93     return fix.build()
94 }
95