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