首页 > 编程知识 正文

java获取参数注解JAVA 自定义注解及参数获取

时间:2023-05-04 17:10:31 阅读:158452 作者:4551

package com.java.annotation;

import java.lang.annotation.*;

//*

*创建按lw on 14-5-30。

*自定义注释

*/

@Documented

@target(elementtype.method ) )。

@ retention (retention policy.runtime ) )。

公共@ interface method info {

公共字符串值() default (还没有说明);

}

/*

定义注释时的4个限定数据

@target(elementtype.type ) :接口、类、枚举和注释

@target(elementtype.field ) :字段,枚举的常量

@target(elementtype.method ) :方法

@target(elementtype.parameter ) :方法参数

@ target (element type.constructor ) :构造函数

@ target (element type.local _ variable ) :局部变量

@ target (element type.annotation _ type ) :注释

@target(elementtype.package ) :包

@ retention (retention policy.source ) :在源文件中有效(即保留源文件)

@ retention (retention policy.class ) :在class文件中有效(即class保留)

@ retention (retention policy.runtime ) :在运行时有效(即运行时挂起)

@Inherited :说明子类可以从父类继承此注释

@Document :表示此注释包含在javadoc中

*/

package com.java.annotation;

import Java.lang.annotation.annotation;

import java.lang.reflect.Method;

//*

*创建按lw on 14-5-30。

*/

公共类测试{

//*

*获取注释参数

*

* @param annotationClass注释类

* @param annotationField注释类字段名称

* @param aClass使用注释的class名称

* @param methodName使用注释的方法名称

* @throws Exception

*/

publicstaticvoidgetannotationpar (classannotationclass,String annotationField,Class aClass,String methodName ) Throwsexceppper

methodaclassmethod=aclass.get method (method name;

annotation annotation=aclass method.get annotation (annotation class );

method method=annotation.getclass ().getdeclaredmethod ) annotationfield;

system.out.println (method.invoke (annotation ) );

}

publicstaticvoidmain (string [ ] args ) throws Exception { )。

test.getannotationpar (method info.class,' Value ',Test2Annotation.class,' testMyAnnotation ' );

}

}

class Test2Annotation {

@MethodInfo(value='自定义注释@methodinfo ' ) ) ) ) )。

公共void test my annotation (

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。