Kotlin 注解
1. kotlin 定义注解
注解属性在使用时指定,其后不会再变,只能声明为只读属性
annotation class Annotation1(val name: String, val desc: String)
annotation class Annotation2(val field1: Int, val field2: String)
2. kotlin 元注解
- @Retention : 修饰注解可以保留多长时间 SOURCE, BINARY, RUNTIME
- @Target : 指定注解可以修饰的程序目标
- @Repeatable : 可重复修饰的注解
// @Retention(value = AnnotationRetention.SOURCE) // 注解只保留在源代码中,注解会被编译器丢弃
// @Retention(value = AnnotationRetention.BINARY) // 注解将被记录在 class 文件中,JVM无法获取
@Retention(value = AnnotationRetention.RUNTIME) // 注解将被记录在 class 文件中,JVM可以获取,程序可以通过反射获取。默认值
annotation class Annotation3()
// 注解只能修饰类
@Target(AnnotationTarget.CLASS)
annotation class Annotation4()
// 注解只能修饰注解
@Target(AnnotationTarget.ANNOTATION_CLASS)
annotation class Annotation5()
// 注解只能修饰函数或方法,不含构造方法
@Target(AnnotationTarget.FUNCTION)
annotation class Annotation6()
// 注解只能修饰构造方法
@Target(AnnotationTarget.CONSTRUCTOR)
annotation class Annotation7()
// 注解只能修饰属性
@Target(AnnotationTarget.PROPERTY)
annotation class Annotation8()
// 注解只能修饰字段,包括属性的支持字段
@Target(AnnotationTarget.FIELD)
annotation class Annotation9()
// 注解只能修饰局部变量
@Target(AnnotationTarget.LOCAL_VARIABLE)
annotation class Annotation10()
// 注解只能修饰函数或构造函数的值参数
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Annotation11()
// 注解只能修饰表达式
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Annotation12()
// 注解只能修饰类型别名
@Target(AnnotationTarget.TYPEALIAS)
annotation class Annotation13()
// 指定多个可修饰的程序目标
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FUNCTION])
annotation class Annotation14()
// 2.3 可重复修饰的注解
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
@Repeatable
annotation class RepeatAnnotation(val field1: Int, val field2: String)
class DemoAnnotation {
@RepeatAnnotation(1, "2")
@RepeatAnnotation(3, "4")
fun method() {
}
}
3. kotlin 使用注解
class TestAnnotation {
@Annotation1(name = "method", desc = "this is a normal method")
@Annotation2(field1 = 1001, field2 = "test annotation")
fun method() {
println("invoke method")
}
}
fun main() {
// 获取 TestAnnotation 的方法定义的注解信息
val annotationList = TestAnnotation::method.annotations
annotationList.forEach { annotation ->
if (annotation is Annotation1) {
println("name: ${annotation.name}, desc: ${annotation.desc}")
} else if (annotation is Annotation2) {
println("field1: ${annotation.field1}, field2: ${annotation.field2}")
}
}
}
附 Github 源码:
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
二维码