0

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)

Elye
  • 53,639
  • 54
  • 212
  • 474

1 Answers1

1

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

Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49
  • Thanks. My intent is more on making an annotation processor that could perform some dynamic operation (e.g. lint). Not just a blanket lint check for everything. – Elye Aug 28 '18 at 09:18
  • e.g. the `@IntegerRes` will only apply it's lint to those that uses it, and not lint all parameters. – Elye Aug 28 '18 at 09:19
  • I did your job and typed "Strg+F: Annotation". There, I added the file that does what you want. – Benjamin Maurer Aug 28 '18 at 09:37
  • Yes, that could be found. The code just show the normal lint checking algorithm, which is doable. But my question is `How to implement dynamic linting check using annotation?`. Not just a static check, but *dynamic* checking (while one code). The source provided and show doesn't seems to show how that's done. – Elye Aug 28 '18 at 14:10
  • That still uses the same inspections. There is no magic flying over your code trick. AndroidStudio just runs the linter in the background while you type. You can enable/disable inspections and set their warning levels in File->Settings->Editor->Inspections .. There you find Android->Lint->Correctness and there you'll find the linter-check I pointed out: "Incorrect support annotation usage" – Benjamin Maurer Aug 28 '18 at 16:10
  • Thanks. I guess in that case, we probably can't add our own custom linting to AndroidStudio, for it's auto checking while code? – Elye Aug 28 '18 at 23:11
  • No you can. I'm not 100% sure how AndoridStudio detects your lints, or how you have to register them, but I think you need to declare that in your gradle file and AndroidStudio will find them. New lint checks are deactivated by default, you'll have to enable them in the menu. I'd recommend to start with some "hello world"-like, useless example, just to get familiar with the mechanism. – Benjamin Maurer Aug 29 '18 at 09:49
  • My investigation shows that the auto checking is called `inspection`, while `lint` is not something triggered automatically. For IntelliJ, to have custom inspection, it is in https://www.jetbrains.com/help/idea/creating-custom-inspections.html, it is not as powerful. To make it something more powerful, need to create some IntelliJ plugin as per https://www.jetbrains.org/intellij/sdk/docs/tutorials/code_inspections.html. So I don't think we could make it just by using gradle alone. I hope I am wrong though. – Elye Aug 29 '18 at 12:09
  • Found another statement that confirm the Android Studio provided lint is triggered through the plugin. https://stackoverflow.com/a/40338663/3286489. So it is not just a simple gradle code, or custom code. – Elye Aug 29 '18 at 12:45
  • I did do my study, and exploration. As oppose to your thought of me not doing my homework. Hopes you understand my question, is not just a blind lazy question. Clearly there are people out there like me that are not clear what is the different between inspection and lint in Android Studio. Hope our conversation here helps enlighten all who visit this questions. – Elye Aug 29 '18 at 12:48