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 18 19 import com.android.tools.lint.detector.api.getUMethod 20 import org.jetbrains.uast.UAnnotation 21 import org.jetbrains.uast.UCallExpression 22 import org.jetbrains.uast.UElement 23 import org.jetbrains.uast.UMethod 24 import org.jetbrains.uast.UParameter 25 import org.jetbrains.uast.UQualifiedReferenceExpression 26 isPermissionMethodCallnull27fun isPermissionMethodCall(callExpression: UCallExpression): Boolean { 28 val method = callExpression.resolve()?.getUMethod() ?: return false 29 return hasPermissionMethodAnnotation(method) 30 } 31 hasPermissionMethodAnnotationnull32fun hasPermissionMethodAnnotation(method: UMethod): Boolean = 33 getPermissionMethodAnnotation(method) != null 34 35 fun getPermissionMethodAnnotation(method: UMethod?): UAnnotation? = method?.uAnnotations 36 ?.firstOrNull { it.qualifiedName == ANNOTATION_PERMISSION_METHOD } 37 <lambda>null38fun hasPermissionNameAnnotation(parameter: UParameter) = parameter.annotations.any { 39 it.hasQualifiedName(ANNOTATION_PERMISSION_NAME) 40 } 41 42 /** 43 * Attempts to return a CallExpression from a QualifiedReferenceExpression (or returns it directly if passed directly) 44 * @param callOrReferenceCall expected to be UCallExpression or UQualifiedReferenceExpression 45 * @return UCallExpression, if available 46 */ findCallExpressionnull47fun findCallExpression(callOrReferenceCall: UElement?): UCallExpression? = 48 when (callOrReferenceCall) { 49 is UCallExpression -> callOrReferenceCall 50 is UQualifiedReferenceExpression -> callOrReferenceCall.selector as? UCallExpression 51 else -> null 52 } 53