如何使用注释实现动态 linting 检查?



在AndroidStudio中,我们可以用@IntegerRes来表示Int参数必须是资源。

如果我们使用Int而不是Resource值,它将动态抱怨(在我们编码时,在我们编译之前(

Expected resource of type integer less... (⌘F1) 
Ensures that resource id's passed to APIs are of the right type; for example, calling Resources.getColor(R.string.name) is wrong.

单击进入@IntegerRes将看到以下代码。

/**
* Denotes that an integer parameter, field or method return value is expected
* to be an integer resource reference (e.g. {@code android.R.integer.config_shortAnimTime}).
*/
@Documented
@Retention(CLASS)
@Target({METHOD, PARAMETER, FIELD, LOCAL_VARIABLE})
public @interface IntegerRes {
}

我想研究如何动态处理此注释,以便我可以进行注释。我在哪里可以找到此代码的来源?(我以为所有的安卓代码都是开源的(

它们是开源的:https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks

并且有很多关于如何实现自定义 lint 检查的文章。就像这个,它甚至将存储库与更多示例链接起来。

由于您似乎懒得实际使用我提供的资源,因此,在这里,这个检查参数上的注释,例如,@ColorInt注释参数是否实际上传递了有效的颜色:

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

最新更新