如何使用注释实施动态掉毛检查?

时间:2018-08-28 09:06:17

标签: java android gradle kotlin

在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 {
}

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

1 个答案:

答案 0 :(得分:1)

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

关于如何实现自定义棉绒检查的文章很多。 Like this one,甚至还有links a repository with further examples

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

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