Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 583fe724 authored by Thiébaud Weksteen's avatar Thiébaud Weksteen
Browse files

Support Kotlin implementation for EnforcePermissionDetector

sourcePsi is null for Kotlin implementation. Use javaPsi instead.

When resolving the annotation, if "value" (the default parameter name)
is not provided, Kotlin uses "null". If that's the case, do the
resolution manually.

Bug: 430102172
Flag: EXEMPT build infrastructure
Test: atest --host AndroidGlobalLintCheckerTest
Change-Id: Iec9c50e5ff4eb53deeb83f9e12c8b78c3c4a5cef
parent 0ab05452
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -88,7 +88,9 @@ class EnforcePermissionDetector : Detector(), SourceCodeScanner {
            return false
        }
        for (i in overridingAttributes.indices) {
            if (overridingAttributes[i].name != overriddenAttributes[i].name) {
            val overridingAttrName = overridingAttributes[i].name ?: "value"
            val overriddenAttrName = overriddenAttributes[i].name ?: "value"
            if (overridingAttrName != overriddenAttrName) {
                return false
            }
            val value1 = overridingAttributes[i].value ?: return false
@@ -171,7 +173,7 @@ class EnforcePermissionDetector : Detector(), SourceCodeScanner {
            if (getContainingAidlInterface(context, uMethod) == null) {
                return
            }
            val overridingMethod = element.sourcePsi as PsiMethod
            val overridingMethod = element.javaPsi as PsiMethod
            val overriddenMethod = usageInfo.referenced as PsiMethod
            compareMethods(context, element, overridingMethod, overriddenMethod)
        }
+24 −1
Original line number Diff line number Diff line
@@ -54,6 +54,24 @@ class EnforcePermissionDetectorTest : LintDetectorTest() {
        .expectClean()
    }

    fun testDoesNotDetectIssuesCorrectAnnotationOnMethodKotlin() {
        lint().files(kotlin(
            """
            package test.pkg
            import android.annotation.EnforcePermission
            public class TestClass2 : IFooMethod.Stub {
                @EnforcePermission(android.Manifest.permission.READ_PHONE_STATE)
                override fun testMethod() {
                    testMethod_enforcePermission()
                }
            }
            """).indented(),
                *stubs
        )
        .run()
        .expectClean()
    }

    fun testDoesNotDetectIssuesCorrectAnnotationAllOnMethod() {
        lint().files(java(
            """
@@ -487,6 +505,7 @@ class EnforcePermissionDetectorTest : LintDetectorTest() {
        """
        public interface IFooMethod extends android.os.IInterface {
         public static abstract class Stub extends android.os.Binder implements IFooMethod {
             protected void testMethod_enforcePermission() {}
          }
          @android.annotation.EnforcePermission(android.Manifest.permission.READ_PHONE_STATE)
          public void testMethod();
@@ -532,7 +551,11 @@ class EnforcePermissionDetectorTest : LintDetectorTest() {
    private val enforcePermissionAnnotationStub: TestFile = java(
        """
        package android.annotation;
        public @interface EnforcePermission {}
        public @interface EnforcePermission {
            String value() default "";
            String[] allOf() default {};
            String[] anyOf() default {};
        }
        """
    ).indented()