首页 > 编程知识 正文

java项目使用自定义注解,java注解的使用

时间:2023-05-04 23:07:17 阅读:33331 作者:3111

目录元注释测试如何使用自定义注释注释注释处理注释

元评论

Java定义四种标准的元注释类型,并负责用于说明其他注释类型的其他注释

位于java.lang.annotation

@Target:用于描述注释的范围。 在哪里可以使用)

@Retention:指示注释必须存储在哪个级别,以说明注释的生命周期

source class 3358 www.Sina.com/@ document :指示javadoc中是否包含注释

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

RUNTIME

最常用的元注释是@Target。 使用@Target定义Annotation可应用于源代码中的何处。

或接口: ElementType.TYPE;

字段:元素类型. field;

方法ElementType.METHOD;

构建方法: ElementType.CONSTRUCTOR;

方法参数: ElementType.PARAMETER。

例如,要定义方法中可用的注释@MyAnnotation,必须添加@target(elementtype.method )

@Target

另一个重要的元注释@Retention定义了注释的生命周期。

仅限编译期间: RetentionPolicy.SOURCE;

仅限class文件: RetentionPolicy.CLASS;

执行时间: RetentionPolicy.RUNTIME。

如果@Retention不存在,则Annotation缺省为CLASS。 通常,我们自定义的Annotation是RUNTIME,因此请确保添加元注释@ retention (retention policy.runtime )

/**定义注释* Target:表示我们的注释可以在那些位置使用* Retention:表示注释何时生效,源代码,class运行时* documentented * Inherited:表示子类可以继承父类的注释*/@ target (value={ element type.method } ) @ retention (value=retention )

自定义注释import Java.lang.annotation.element type; import Java.lang.annotation.retention; import Java.lang.annotation.retention policy; import Java.lang.annotation.target; @ retention (retention policy.runtime ) target ) elementtype.field ) public @interface Range { int min=1; int max=7; String msg='长度小于' Range.min ',或' Range.max; String message ()默认msg; intmin (默认min; intmax (默认最大; }

注释public class person { @ rangepublicstringname; @range(min=22,max=50,message='年龄在22以上或50以上')公共integer age; @range(min=3、max=20、message='长度不能大于3或大于20 ) )公共字符串city; public person (} public person (string name,Integer age,String city ) { this.name=name; this.age=age; this.city=city; //这里省略get/set .}

如何处理注释import java.lang.reflect.Field; publicclassrangecheck { publicstaticvoidcheck (person person ) throws IllegalAccessException,NullPointerException, 使用IllegalArgumentException { //反射获取所有字段,并在for (现场字段: person.getclass (.get fields ) )//字段上注释rand=null (获取//字段的值。 这里可能会抛出IllegalAccessException权限异常objectvalue=field.get(Person )。 system.out.println (field.getname () ' value-- ' value ); //确定参数是否为空if (value==null thrownewnullpointerexception (field.getname ) ) (“不能为空”); 字符串类型检查如果是elseif(valueinstanceofstring )字符串类型,则为strings=(string ) value; //此值是否满足@Range的最小/最大值(s.length (range.min )|| s.length (range.max ) ) thrownewillegalargumenter ) if(Irange.min(|Irange.max ) ) thrownewillegalargumentexception ('参数异常: ' field.getName ) ) range.messsion

测试并使用classdemoapplicationtests { publicstaticvoidmain (字符串[ ] args ) person=newperson('Hello ',18,'深圳'); try{rangecheck.check(Person ); //catch (illegalaccessexceptione ) {//e .打印堆栈跟踪}; //catch (illegalargumentexceptione ) {//e .打印堆栈跟踪}; //String message=e.getMessage (; //System.out.println (非法参数异常: -- ' message ); //catch(nullpointerexceptione ) {//e .打印堆栈跟踪}; //String message=e.getMessage (; //system.out.println (空指针异常: -- ' message ); //catch(exceptione ) {e.printStackTrace ); if (einstanceofillegalargumentexception ) {System.out.println (“错误的参数异常”); }String message=e.getMessage (; 系统. out.println (所有异常: -- ' message ); }/**************如果某个字段不符合要求,就会抛出异常* * * * * * * * * * * * * * * * * * * * *。 ava.lang.illegalargumentexception :参数异常: age年龄在22以上或50 atcom.pro.POJO.range check.check (range check.Java 0 --参数异常: age年龄在22以上或502021-04-191533602336027.599 info 5604---[ extshutdownhook ] o.s.s.concurent.TTT

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