Skip to content

Annotations

Annotations are a lot like Java's. Use the @MyAnnotation syntax to annotate a class, field or method parameter.

@MyAnnotation
class MyClass {
  @MyAnnotation
  int myField

  fun int myMethod(@MyAnnotation Integer someInt) {
    return someInt + myField
  }
}

Annotation attributes

You can specify attributes like in Java. If your annotation only has one attribute, with the name value() You can just type your attribute value between parentheses.

@MyAnnotation(1)

Or you can specify the attribute name like in the below example

@MyAnnotation(value = 1)

If you have multiple attributes, separate them with a comma

@MyAnnotation(value1 = 1, value2 = 3)

Enum attributes

When specifying enum attributes you just have to specify the enum's name, without its class

@MyAnnotation(timeUnit = MILLISECONDS)

Class attributes

For attributes of type Class, you can specify any class as you would in Java

@MyAnnotation(converter = MyConverter.class)

But like in Groovy, you can also specify Lambdas

@MyAnnotation(converter = { it.toString() })

A lambda generates a class at compilation. This generated class will be used as the value for this attribute.