Elye
Elye

Reputation: 60171

How to implement dynamic linting check using annotation?

In AndroidStudio, we could just use @IntegerRes to denote an Int parameter must be a resource.

If we use an Int instead of Resource value, it will complaint dynamically (while we're coding, before we compile)

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.

Click into @IntegerRes would see the below code.

/**
 * 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 {
}

I want to look into how this annotation is processed dynamically, so I could make my annotation. Where could I find the source of this code? (I thought all Android code are open source)

Upvotes: 0

Views: 566

Answers (1)

Benjamin Maurer
Benjamin Maurer

Reputation: 3753

They are open source: https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks

And there are many articles on how to implement custom lint checks. Like this one, which even links a repository with further examples.

Since it seems like you can't be bothered to actually take at the resources I have provided, there, this one checks Annotations on Parameters, e.g., whether an @ColorInt annotated parameter actually gets passed a valid color:

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

Upvotes: 0

Related Questions