编写自定义棉绒警告以检查自定义注释 [英] Writing custom lint warning to check for custom annotation

查看:98
本文介绍了编写自定义棉绒警告以检查自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下注释:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD})
public @interface Warning {

}

该注释旨在注释如果不慎调用会引起问题的方法。我在项目中添加了注释处理器,但这仅在javac命令的日志输出中提供警告。我希望此警告与其他带有绒毛的警告一起出现在Android Studio中,并在带有此批注的方法被调用的任何地方出现。这就是为什么我要编写自定义的皮棉规则的原因。我有棉绒规则的基本框架:

Which is intended to annotate methods which can cause problems if called carelessly. I added an annotation processor to my project, but this only provides the warning in the log output of the javac command. I want this warning to appear in Android Studio along with the other lint warnings anywhere a method with this annotation is called. This is why I am trying to write a custom lint rule. I have the basic skeleton of the lint rule:

import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;

public class CaimitoDetector extends Detector implements Detector.JavaScanner {

  public static final Issue ISSUE = Issue.create(
      "WarningAnnotation",
      "This method has been annotated with @Warning",
      "This method has special conditions surrounding it's use, be careful when using it and refer to its documentation.",
      Category.USABILITY, 7, Severity.WARNING,
      new Implementation(CaimitoDetector.class, Scope.JAVA_FILE_SCOPE));

  @Override
  public void visitMethod(JavaContext context, AstVisitor visitor, MethodInvocation node) {

  }

}







import com.android.tools.lint.client.api.IssueRegistry;
import com.android.tools.lint.detector.api.Issue;

import java.util.Collections;
import java.util.List;

public class CaimitoIssueRegistry extends IssueRegistry {

  @Override
  public List<Issue> getIssues() {
    return Collections.singletonList(CaimitoDetector.ISSUE);
  }

}

但我不知道如何从这里继续。如何检查方法中是否存在注释,并发出警告以使其在Android Studio中可见?

But I do not know how to proceed from here. How can I check if an annoation exists on a method, and raise a warning such that it will be visible in Android Studio?

推荐答案


但是我不知道如何从这里继续

But I do not know how to proceed from here

我建议为您的检测器。这是一个示例项目,演示如何编写 Detector 测试[1]。这样,您可以尝试调整自己的 Detector

I suggest to write a test for your Detector first. Here is an example project which demonstrates how to write Detector tests [1]. That way you can try and adjust your Detector as you like.


如何我检查方法上是否存在注释。

How can I check if an annoation exists on a method

我建议您查看一下Android的默认检测器[2]。您很可能会在这里找到一个好的起点。例如。 AnnotationDetector

I suggest to have a look at Android's default detectors [2]. There you'll most probably find a good point to start. E.g. the AnnotationDetector.


,并发出警告,使其在Android中可见Studio吗?

and raise a warning such that it will be visible in Android Studio?

如果您将自定义规则正确地集成到项目中,那么Lint将为您发出警告。请在此处[3]中查看有关如何在项目中集成自定义规则的不同选项。注意:自定义规则的AFAIK警告只会在运行相应的Gradle任务时报告。 Android Studio的自动突出显示不适用于自定义规则。

If you integrate your custom rules correctly into your project, then Lint will raise the warning for you. Please have a look here [3] for different options on how to integrate custom rules in your project. Note: AFAIK warnings of custom rules will only reported when running the corresponding Gradle task. The "auto-highlight" of Android Studio does not work with custom rules.


  1. https://github.com/a11n/CustomLintRules

  2. https://android.googlesource。 com / platform / tools / base / + / master / lint / libs / lint-checks / src / main / java / com / android / tools / lint / checks

  3. https://github.com/a11n/android-lint/tree/master/6_application

  1. https://github.com/a11n/CustomLintRules
  2. https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks
  3. https://github.com/a11n/android-lint/tree/master/6_application

这篇关于编写自定义棉绒警告以检查自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆