|
Annotation Declaration
|
annotation class Fancy
|
annotation class Fancy
|
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION,
AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
annotation class Fancy
|
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION,
AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
annotation class Fancy
|
Usage
|
@Fancy class Foo {
@Fancy fun baz(@Fancy foo: Int): Int {
return (@Fancy 1)
}
}
|
@Fancy class Foo {
@Fancy func baz(@Fancy foo: Int) -> Int {
return (@Fancy 1)
}
}
|
class Foo @Inject constructor(dependency: MyDependency) { ... }
|
class Foo @Inject constructor(dependency: MyDependency) { ... }
|
class Foo {
var x: MyDependency? = null
@Inject set
}
|
class Foo {
var x: MyDependency? = null
@Inject set
}
|
Constructors
|
annotation class Special(val why: String)
@Special("example") class Foo {}
|
annotation class Special(let why: String)
@Special("example") class Foo {}
|
annotation class ReplaceWith(val expression: String)
annotation class Deprecated(
val message: String,
val replaceWith: ReplaceWith = ReplaceWith(""))
@Deprecated("This function is deprecated, use === instead", ReplaceWith("this === other"))
|
annotation class ReplaceWith(let expression: String)
annotation class Deprecated(
let message: String,
let replaceWith: ReplaceWith = ReplaceWith(""))
@Deprecated("This function is deprecated, use === instead", ReplaceWith("this === other"))
|
import kotlin.reflect.KClass
annotation class Ann(val arg1: KClass<*>, val arg2: KClass<out Any>)
@Ann(String::class, Int::class) class MyClass
|
import kotlin.reflect.KClass
annotation class Ann(let arg1: KClass<*>, let arg2: KClass<out Any>)
@Ann(String::class, Int::class) class MyClass
|
Lambdas
|
annotation class Suspendable
val f = @Suspendable { Fiber.sleep(10) }
|
annotation class Suspendable
let f = @Suspendable { Fiber.sleep(10) }
|
Annotation Use-site Targets
|
class Example(@field:Ann val foo, // annotate Java field
@get:Ann val bar, // annotate Java getter
@param:Ann val quux) // annotate Java constructor parameter
|
class Example(@field:Ann let foo, // annotate Java field
@get:Ann let bar, // annotate Java getter
@param:Ann let quux) // annotate Java constructor parameter
|
@file:JvmName("Foo")
package org.jetbrains.demo
|
@file:JvmName("Foo")
package org.jetbrains.demo
|
class Example {
@set:[Inject VisibleForTesting]
var collaborator: Collaborator
}
|
class Example {
@set:[Inject VisibleForTesting]
var collaborator: Collaborator
}
|
fun @receiver:Fancy String.myExtension() { ... }
|
func @receiver:Fancy String.myExtension() { ... }
|
Java Annotations
|
import org.junit.Test
import org.junit.Assert.*
import org.junit.Rule
import org.junit.rules.*
class Tests {
// apply @Rule annotation to property getter
@get:Rule val tempFolder = TemporaryFolder()
@Test fun simple() {
val f = tempFolder.newFile()
assertEquals(42, getTheAnswer())
}
}
|
import org.junit.Test
import org.junit.Assert.*
import org.junit.Rule
import org.junit.rules.*
class Tests {
// apply @Rule annotation to property getter
@get:Rule let tempFolder = TemporaryFolder()
@Test func simple() {
let f = tempFolder.newFile()
assertEquals(42, getTheAnswer())
}
}
|
// Java
public @interface Ann {
int intValue();
String stringValue();
}
|
// Java
public @interface Ann {
int intValue();
String stringValue();
}
|
// Kotlin
@Ann(intValue = 1, stringValue = "abc") class C
|
// Kotlin
@Ann(intValue = 1, stringValue = "abc") class C
|
// Java
public @interface AnnWithValue {
String value();
}
|
// Java
public @interface AnnWithValue {
String value();
}
|
// Kotlin
@AnnWithValue("abc") class C
|
// Kotlin
@AnnWithValue("abc") class C
|
Arrays as annotation parameters
|
// Java
public @interface AnnWithArrayValue {
String[] value();
}
|
// Java
public @interface AnnWithArrayValue {
String[] value();
}
|
// Kotlin
@AnnWithArrayValue("abc", "foo", "bar") class C
|
// Kotlin
@AnnWithArrayValue("abc", "foo", "bar") class C
|
// Java
public @interface AnnWithArrayMethod {
String[] names();
}
|
// Java
public @interface AnnWithArrayMethod {
String[] names();
}
|
// Kotlin 1.2+:
@AnnWithArrayMethod(names = ["abc", "foo", "bar"])
class C
// Older Kotlin versions:
@AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar"))
class D
|
// Kotlin 1.2+:
@AnnWithArrayMethod(names = ["abc", "foo", "bar"])
class C
// Older Kotlin versions:
@AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar"))
class D
|
Accessing properties of an annotation instance
|
// Java
public @interface Ann {
int value();
}
|
// Java
public @interface Ann {
int value();
}
|
// Kotlin
fun foo(ann: Ann) {
val i = ann.value
}
|
// Kotlin
func foo(ann: Ann) {
let i = ann.value
}
|