Annotations
Java annotations are a form of metadata that can be attached at different places in the source code and are supported by Base Language as well.
For an overview of Java built-in annotations, read this article ⧉.
The built-in annotations are:
- @Override ⧉: indicates that a method is intended to override a method declared in a superclass.
- @SuppressWarnings ⧉: tells the compiler to suppress specific warnings that it would otherwise generate.
- @Deprecated ⧉: marks a program element (class, method, field, etc.) as no longer recommended for use, typically because it’s dangerous or there is a better alternative.
- @SafeVarargs ⧉: denotes that a method with a variable number of arguments (varargs) is safely handling the arguments to prevent potential heap pollution.
- @FunctionalInterface ⧉: specifies that the annotated interface is intended to be a functional interface, which has exactly one abstract method.
- @Native ⧉: Used to signal that a field is referencing a constant value from a native library, typically used in conjunction with the
native
keyword.
A common annotation that is also often used is the @Serial ⧉ annotation.
The meta-annotations to describe annotations are:
- @Target ⧉: specifies the kinds of program element to which an annotation type is applicable (such as a method, field, or class).
- @Retention ⧉: indicates how long annotations with the annotated type are to be retained. It can specify retention at source level, class level, or runtime level.
- @Inherited ⧉: marks an annotation to be inherited to subclasses of the annotated class (by default, annotations are not inherited to subclasses).
- @Documented ⧉: signifies that whenever the specified annotation is used those elements should be documented using the Javadoc tool.
- @Repeatable ⧉: allows an annotation to be applied more than once to the same declaration or type use.
Some useful Intellij SDK annotations are:
- @State ⧉: specify the storage location when persisting the state of components
- @RequiresBackgroundThread ⧉ : Methods and constructors must be called from some thread that is not the EDT.
- @RequiresEdt ⧉ : Methods and constructors must be called from the EDT only.
- @RequiresReadLock ⧉ : Methods and constructors must be called only with read lock held.
- @RequiresReadLockAbsence ⧉ : Methods and constructors must be called without read lock held.
- @RequiresWriteLock ⧉ : Methods and constructor must be called only with write lock held.
For an overview of JetBrains-specific annotations, read: JetBrains Annotations ⧉:
- @Nullable and @NotNull: indicate a variable, parameter, or return value that can or cannot be null.
- @Nls: indicates that an annotated code element is a string that needs to be localized.
- @NonNls: indicates that an annotated code element is a string which is not visible to users, which doesn't require localization, and which doesn't contain strings requiring localization. When you annotate an element with @NonNls, localization tools will skip this element and strings inside it.
- @PropertyKey: 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.
- @TestOnly: indicates that a method or a constructor must be called from testing code only.
- @Contract: lets you specify a set of rules (a contract) that a method must follow. If the contract is violated, IntelliJ IDEA reports a problem.
- @Language: injects code written in another language in a Java String.
More annotations can be found in the package org.jetbrains.annotations and org.intellij.lang.annotations ⧉.