自定义注解获取类,属性变量,方法,方法参数上注解的值(Kotlin)

先看下效果

我们先来看一下kotlin中的注解Annotation.kt:


package kotlin.annotation

import kotlin.annotation.AnnotationTarget.*

public enum class AnnotationTarget {
    /** 类、接口或对象 */
    CLASS,
    /** 仅注释类 */
    ANNOTATION_CLASS,
    /** 泛型类型参数(尚不支持)*/
    TYPE_PARAMETER,
    /** Property */
    PROPERTY,
    /** 属性变量 */
    FIELD,
    /** 局部变量*/
    LOCAL_VARIABLE,
    /** 函数或构造函数的参数 */
    VALUE_PARAMETER,
    /** 仅构造函数 */
    CONSTRUCTOR,
    /** 函数(不包括构造函数) */
    FUNCTION,
    /** 仅属性getter */
    PROPERTY_GETTER,
    /** 仅属性setter */
    PROPERTY_SETTER,
    /** Type usage */
    TYPE,
    /** Any expression */
    EXPRESSION,
    /** 文件 */
    FILE,
    /** Type alias */
    @SinceKotlin("1.1")
    TYPEALIAS
}


public enum class AnnotationRetention {
    /** 在源文件中有效(即源文件保留) */
    SOURCE,
    /** 注释存储在二进制输出中,但对反射不可见 */
    BINARY,
    /** 在运行时有效(即运行时保留)*/
    RUNTIME
}

@Target(AnnotationTarget.ANNOTATION_CLASS)
@MustBeDocumented
public annotation class Target(vararg val allowedTargets: AnnotationTarget)

@Target(AnnotationTarget.ANNOTATION_CLASS)
public annotation class Retention(val value: AnnotationRetention = AnnotationRetention.RUNTIME)

@Target(AnnotationTarget.ANNOTATION_CLASS)
public annotation class Repeatable

@Target(AnnotationTarget.ANNOTATION_CLASS)
public annotation class MustBeDocumented

使用方法:
创建file:KTAnnotation.kt (所有都写到一个kt文件中,方便测试和使用),

完整代码如下所示,简单易用

package com.i.mvvmdemo

//-------------------------------定义注解---------------------------------------
@Target(
    //类注解
    AnnotationTarget.CLASS,
    //属性变量注解
    AnnotationTarget.FIELD,
    //函数方法注解
    AnnotationTarget.FUNCTION,
    //方法参数注解
    AnnotationTarget.VALUE_PARAMETER
)
@Retention(AnnotationRetention.RUNTIME)
annotation class RequestParam(val name: String, val value: String)

//-------------------------------使用注解---------------------------------------

@RequestParam(name = "nnn", value = "vvv")
class UserRequest {
    @RequestParam(name = "小明", value = "男生")
    private var age = 18

    @RequestParam(name = "张飞", value = "大口吃饭")
    private fun eat(
        @RequestParam(name = "吃什么?", value = "白米饭") food: String,
        @RequestParam(name = "喝什么?", value = "水") drink: String
    ) {

    }

}

//-------------------------------获取注解的值---------------------------------------

//获取类上面的注解
fun getClassAnnotaion() {
    val clazz = UserRequest::class.java
    val annotation = clazz.getAnnotation(RequestParam::class.java)
    println("类注解的值:${annotation?.name},${annotation?.value}")
}

//获取属性变量上面的注解
fun getFieldAnnotaion() {
    val clazz = UserRequest::class.java
    val annotations = clazz.declaredFields
    for (i in annotations) {
        val annotation = i.getAnnotation(RequestParam::class.java)
        println("属性变量注解的值:${annotation?.name},${annotation?.value}")
    }
}

//获取函数上面的注解
fun getFunctionAnnotaion() {
    val clazz = UserRequest::class.java
    val annotations = clazz.declaredMethods
    for (i in annotations) {
        val annotation = i.getAnnotation(RequestParam::class.java)
        println("函数注解的值:${annotation?.name},${annotation?.value}")
    }
}

//获取方法参数上注解的值
fun getParameterAnnotaion() {
    val clazz = UserRequest::class.java
    val method = clazz.getDeclaredMethod("eat", String::class.java, String::class.java)
    val parameterAnnotations = method.parameterAnnotations
    for (iss in parameterAnnotations) {
        for (i in iss) {
            if (i is RequestParam) {
                println("参数注解的值:${i?.name},${i?.value}")
            }
        }
    }
}

//-------------------------------测试---------------------------------------

fun main() {
    getClassAnnotaion()
    getFieldAnnotaion()
    getFunctionAnnotaion()
    getParameterAnnotaion()
}
Java的注解使用方式参照:https://www.jianshu.com/p/13991ec28dd3

获取方法参数注解的值,不指定方法名:

//获取方法参数上注解的值,不指定具体方法
fun getParameterAnnotaion2() {
    val clazz = UserRequest::class.java
    for (method in clazz.declaredMethods) {
        val parameterAnnotations = method.parameterAnnotations
        for (iss in parameterAnnotations) {
            for (i in iss) {
                print("方法名:${method.name}")
                if (i is RequestParam) {
                    println("参数注解的值:${i?.name},${i?.value}")
                }
            }
        }
    }

}

你可能感兴趣的:(Kotlin)