|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 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.example.lint.checks |
| 17 | + |
| 18 | +import com.android.tools.lint.client.api.UElementHandler |
| 19 | +import com.android.tools.lint.detector.api.* |
| 20 | +import com.android.tools.lint.detector.api.Detector.UastScanner |
| 21 | +import org.jetbrains.uast.UElement |
| 22 | +import org.jetbrains.uast.ULiteralExpression |
| 23 | +import org.jetbrains.uast.evaluateString |
| 24 | + |
| 25 | +/** |
| 26 | + * Sample detector showing how to analyze Kotlin/Java code. |
| 27 | + * This example flags all string literals in the code that contain |
| 28 | + * the word "lint". |
| 29 | + */ |
| 30 | +@Suppress("UnstableApiUsage") |
| 31 | +class SampleCodeDetector : Detector(), UastScanner { |
| 32 | + override fun getApplicableUastTypes(): List<Class<out UElement?>>? { |
| 33 | + return listOf(ULiteralExpression::class.java) |
| 34 | + } |
| 35 | + |
| 36 | + override fun createUastHandler(context: JavaContext): UElementHandler? { |
| 37 | + // Note: Visiting UAST nodes is a pretty general purpose mechanism; |
| 38 | + // Lint has specialized support to do common things like "visit every class |
| 39 | + // that extends a given super class or implements a given interface", and |
| 40 | + // "visit every call site that calls a method by a given name" etc. |
| 41 | + // Take a careful look at UastScanner and the various existing lint check |
| 42 | + // implementations before doing things the "hard way". |
| 43 | + // Also be aware of context.getJavaEvaluator() which provides a lot of |
| 44 | + // utility functionality. |
| 45 | + return object : UElementHandler() { |
| 46 | + override fun visitLiteralExpression(node: ULiteralExpression) { |
| 47 | + val string = node.evaluateString() ?: return |
| 48 | + if (string.contains("lint") && string.matches(Regex(".*\\blint\\b.*"))) { |
| 49 | + context.report(ISSUE, node, context.getLocation(node), |
| 50 | + "This code mentions `lint`: **Congratulations**") |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + companion object { |
| 57 | + /** Issue describing the problem and pointing to the detector implementation */ |
| 58 | + @JvmField |
| 59 | + val ISSUE: Issue = Issue.create( |
| 60 | + // ID: used in @SuppressLint warnings etc |
| 61 | + id = "ShortUniqueId", |
| 62 | + // Title -- shown in the IDE's preference dialog, as category headers in the |
| 63 | + // Analysis results window, etc |
| 64 | + briefDescription = "Lint Mentions", |
| 65 | + // Full explanation of the issue; you can use some markdown markup such as |
| 66 | + // `monospace`, *italic*, and **bold**. |
| 67 | + explanation = """ |
| 68 | + This check highlights string literals in code which mentions the word `lint`. \ |
| 69 | + Blah blah blah. |
| 70 | +
|
| 71 | + Another paragraph here. |
| 72 | + """, // no need to .trimIndent(), lint does that automatically |
| 73 | + category = Category.CORRECTNESS, |
| 74 | + priority = 6, |
| 75 | + severity = Severity.WARNING, |
| 76 | + implementation = Implementation( |
| 77 | + SampleCodeDetector::class.java, |
| 78 | + Scope.JAVA_FILE_SCOPE)) |
| 79 | + } |
| 80 | +} |
0 commit comments