1 /* 2 * Copyright (C) 2018 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 @file:Suppress("UnstableApiUsage") 18 19 package androidx.build.lint 20 21 import com.android.tools.lint.client.api.UElementHandler 22 import com.android.tools.lint.detector.api.Category 23 import com.android.tools.lint.detector.api.Detector 24 import com.android.tools.lint.detector.api.Implementation 25 import com.android.tools.lint.detector.api.Incident 26 import com.android.tools.lint.detector.api.Issue 27 import com.android.tools.lint.detector.api.JavaContext 28 import com.android.tools.lint.detector.api.Scope 29 import com.android.tools.lint.detector.api.Severity 30 import org.jetbrains.kotlin.psi.KtAnnotationEntry 31 import org.jetbrains.uast.UAnnotation 32 import org.jetbrains.uast.UReferenceExpression 33 34 class BanVisibleForTestingParams : Detector(), Detector.UastScanner { 35 getApplicableUastTypesnull36 override fun getApplicableUastTypes() = listOf(UAnnotation::class.java) 37 38 override fun createUastHandler(context: JavaContext): UElementHandler { 39 return AnnotationChecker(context) 40 } 41 42 private inner class AnnotationChecker(val context: JavaContext) : UElementHandler() { visitAnnotationnull43 override fun visitAnnotation(node: UAnnotation) { 44 if (node.qualifiedName != "androidx.annotation.VisibleForTesting") return 45 46 // Using "declared" to resolve an unspecified value to null, rather than the default, 47 // resolve the FQN for the `otherwise` attribute value and abort if it's unspecified. 48 val otherwise = 49 (node.findDeclaredAttributeValue("otherwise") as? UReferenceExpression) 50 ?.resolve() 51 ?.getFqName() ?: return // Unspecified, abort. 52 53 val fixBuilder = 54 when (otherwise) { 55 "androidx.annotation.VisibleForTesting.Companion.PRIVATE", 56 "androidx.annotation.VisibleForTesting.Companion.NONE" -> { 57 // Extract Kotlin use-site target, if available. 58 val useSiteTarget = 59 (node.sourcePsi as? KtAnnotationEntry) 60 ?.useSiteTarget 61 ?.getAnnotationUseSiteTarget() 62 ?.renderName 63 ?.let { "$it:" } ?: "" 64 65 fix() 66 .name("Remove non-default `otherwise` value") 67 .replace() 68 .with("@${useSiteTarget}androidx.annotation.VisibleForTesting") 69 } 70 "androidx.annotation.VisibleForTesting.Companion.PACKAGE_PRIVATE", 71 "androidx.annotation.VisibleForTesting.Companion.PROTECTED" -> { 72 fix().name("Remove @VisibleForTesting annotation").replace().with("") 73 } 74 else -> { 75 // This could happen if a new visibility is added in the future, in which 76 // case 77 // we'll warn about the non-default usage but we won't attempt a fix. 78 null 79 } 80 } 81 82 val incident = 83 Incident(context) 84 .issue(ISSUE) 85 .location(context.getNameLocation(node)) 86 .message("Found non-default `otherwise` value for @VisibleForTesting") 87 .scope(node) 88 89 fixBuilder?.let { incident.fix(it.shortenNames().build()) } 90 91 context.report(incident) 92 } 93 } 94 95 companion object { 96 val ISSUE = 97 Issue.create( 98 "UsesNonDefaultVisibleForTesting", 99 "Uses non-default @VisibleForTesting visibility", 100 "Use of non-default @VisibleForTesting visibility is not allowed, use the " + 101 "default value instead.", 102 Category.CORRECTNESS, 103 5, 104 Severity.ERROR, 105 Implementation(BanVisibleForTestingParams::class.java, Scope.JAVA_FILE_SCOPE) 106 ) 107 } 108 } 109