7

Kotlin is a JVM language that has built in support for nullable and non-null types. Java, as we know, does not have this support.

However, Java does have @Nullable and @NotNull (or similar) annotations provided through Jetbrains, FindBugs, Android, and JSR305.

Like Kotlin, I would like to require (via IDE) all Java parameters and fields to be annotated with one of these annotations. If possible, I would like to use CheckStyle, as I already have it integrated into our build workflow. However, I could not find any rules programmed for the purpose of requiring annotations in CheckStyle's list of checks.

How would I go about doing something like this?

Mike Holler
  • 945
  • 2
  • 13
  • 28

1 Answers1

0

Checkstyle could do it.

Review this: http://checkstyle.sourceforge.net/config_annotation.html#AnnotationUseStyle

And this: http://checkstyle.sourceforge.net/config_annotation.html#AnnotationLocation

That will resolve the issue of "having" the annotations; but if you want an specific annotation, perhaps you could save the day with this: http://checkstyle.sourceforge.net/config_regexp.html#Regexp

Regular expressions always helps with pattern recognition. ^_^

twitter: @hernanemartinez

hernanemartinez
  • 617
  • 1
  • 5
  • 10
  • 1
    This sounds very fragile. Programming languages are not regular, so regular expressions don't sound like the best solution here (see [this legendary post](http://stackoverflow.com/a/1732454/1076585)). In addition, neither of the Checkstyle checks you mention can, as far as I can tell, *require* a parameter, field, etc. to have one or more annotations. – Mike Holler Nov 11 '15 at 17:46
  • There's nothing in Checkstyle that is ready to use, but feel free to raise feature request to [Sevntu Checkstyle](https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues) and you are welcome with pull request. – Michal Kordas Nov 11 '15 at 18:45
  • Mike, did you tried it? checkstyle could be used to found some keywords in your code. You aren't parsing a programming language here. You are just instructing checkstyle to request the .txt file he's reading to have a certain chain of characters, in a given file. (a .java file in this case). If you use a name convention, you can parse it, even request that name convention to replicate. – hernanemartinez Nov 12 '15 at 18:44
  • I did not. Regular expressions are not the right way to perform validation on a non-regular language. If you feel otherwise, feel free to share some code that shows how the method you describe works and I'll take a look. However, looking at the links you've posted I'm skeptical that you can use those together effectively in a way that is not fragile. – Mike Holler Nov 17 '15 at 16:47