Java annotations are pieces of metadata that provide information about the code they are used with, and can instruct the IDE how to process this code.
In Java, there is a set of built-in annotations. On top of that, IntelliJ IDEA provides a bundled package with proprietary annotations that you can add to the classpath and use in your code.
Add org.jetbrains.annotations to a project
- Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Libraries.
Click and under the
For example, the path on Windows: C:\Program Files\JetBrains\IntelliJ IDEA 181.4203.6\redist.
On macOS, annotations can be found in IntelliJ IDEA.app/Contents/redist.
annotation.jarif you use Java 7 or lower, or select
annotation-java8.jarif you use Java 8 or higher.
- In the next dialog, select the module in which you want to use annotations, and apply the changes.
Add org.jetbrains.annotations to Gradle or Maven projects
- To add the library with annotations to a Gradle project, add the
compile 'org.jetbrains:annotations:16.0.1'dependency to the
- For Maven projects, add the
Add org.jetbrains.annotations in the editor
You can also enable annotations using an intention action.
- In the editor, type an annotation, for example,
@NotNulland press Alt+Enter:
- From the list, select Add 'annotations' to classpath.
- In the next dialog, specify whether you want to use annotations from the IntelliJ IDEA distribution package or create a local copy of the library. If you share your project through VCS, it is recommended that you create a local copy of the library, as you will not be able to share annotations from the distribution package.
@Nlsannotation indicates that an annotated code element is a string that needs to be localized.
@NonNlsannotation indicates that an annotated code element is a string which is not visible to users, it doesn't require localization, and it doesn't contain strings requiring localization. When you annotate an element with
@NonNls, localization tools will skip this element and strings inside it.
@PropertyKeyannotation indicates that a method parameter accepts arguments that must be valid property keys in a specific resource bundle. When a string literal that is not a property key in a bundle is passed as a parameter, IntelliJ IDEA highlights it as an error. The annotation is also used to provide completion in string literals passed as parameters.
@TestOnlyannotation indicates that a method or a constructor must be called from testing code only.
- The @Contract annotation lets you specify a set of rules (a contract) that a method must follow. If the contract is violated, IntelliJ IDEA reports a problem.
- The @Nullable annotation indicates a variable, parameter, or return value that can be null.
- The @NotNull annotation indicates a variable, parameter, or return value that cannot be null.