首页 > 编程知识 正文

java序列化,java的注解原理是什么

时间:2023-05-06 17:29:34 阅读:134548 作者:2877

注释的基本概念注释(元数据)提供了向代码中添加信息的形式化方法。 在某个时刻可以非常方便地使用这些数据。 一般来说,这是在这个方法中增加的说明和功能。 例如,@Overvide注释用于说明用这种方式重写父类。

JDK提供的注释Java当前包含三种类型的注释: @Override、@Deprecated和@SuppressWarnnings

@Override :识别属于重写父类的方法的方法@target(elementtype.method ) retention ) retentionpolicy.source ) public @ interertiod @ documented @ retention (retention policy.runtime ) target ) value={constructor,FIELD, 建议不要使用local _ vari ime }。type } (public @ interface deprecated () suppresswarnnings:在类、方法、成员变量和变量的初始

@target(type,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE} ) @retention ) retentionpolicy.source ) ) }元注释

Java有四种类型的元注释:限定注释的注释。 查看上面的源代码,可以找到三种类型: @Target、@Retention、@Document和@Inherited。 主要作用如下:

定制注释@interface用于定义注释的方法自动继承Java.lang.annotation.annotation接口,就像类自动继承Object一样。 注释中的每个方法都表示一个配置参数,返回类型为参数类型,default可以标识参数的默认值。 定义方法如下。

import Java.lang.annotation.element type; import Java.lang.annotation.target; @target(elementtype.method ) public @ interface test (string value ) ) default ' ); }注释中的参数类型注释中的参数仅支持以下类型:

的所有基本类型: byte、short、char、int、long、float、doubleString型Class型enum型Annotation型及以上类型的数组

定义其他类型的参数编译器将报告错误。

使用注释时的注意事项如果注释只有一个参数,建议将其命名为value,以便在使用时可以直接指定属性值。 例如,@test('hello ) )只是使用public和默认权限修饰符来限定参数的默认值。 注释参数必须具有确定的值。 定义时是否提供默认值; 使用注释时指定参数值,或选择。 注释处理器

如果没有方法读取评论,则该评论没有任何意义。 要使用注释,注释处理器是必不可少的,Java颜色反射机制完成注释的处理。

由于注释类库Java使用反射机制来检索类、方法和属性的注释,因此java.lang.reflect以主要方式支持AnnotationElement注释。

booleanisannotationpresent (类? extendsannotationannotationclass :确定元素是否由annotationclass注释限定

textendsannotationtgetannotation (classtannotationclass ) :如果未返回null,则获取元素的annotationclass类型的注释

Annotation[] getAnnotations (:返回元素的所有注释

textendsannotationt [ ] getannotationsbytype (classtannotationclass ) :返回此元素指定类型的所有注释

annotation [ ] getdeclaredannotations (:返回直接限定元素的所有注释

textendsannotationt [ ] getdeclaredannotationsbytype (classtannotationclass ) :返回直接限定元素的所有注释

注释的例子如下所示。

注释定义:

import Java.lang.annotation.element type; import Java.lang.annotation.retention; import Java.lang.annotation.retention policy; import Java.lang.annotation.target; @target(elementtype.field ) ) retention ) retentionpolicy.runtime ) public@interfacetest ) intmax ) ) default 0; int min ) ) default 0; 字符串描述() default ' ); }使用注释:

publicclassannoationtest { @ test (min=6,max=10,description=“用户名长度在6到10个字符之间”) )权限字符串名称; @test(min=6,max=10,description='密码长度在6到10个字符之间') ) private String pasdword; 公共字符串获取名称() { return name; }publicvoidsetname(stringname ) { this.name=name; }公共字符串获取密码() { return pasdword; } publicvoidsetpasdword (string pasdword ) { this.pasdword=pasdword; }测试CPU注释和主方法:

import java.lang.reflect.Field; publicclasstestutil { publicstaticvoidvalid (objectobj ) throwsillegalaccessexception { class? clazz=obj.getClass (; field [ ] fields=clazz.getdeclaredfields (; for(fieldfield:fields ) test test=field.get annotation (test.class ); //属性的@Test注释if(test!=null () field.setaccessible ) ) true; 设置//属性可设置class Java.lang.string (.equals ) field.getgenerictype(.tostring ) ) /字符串类型的长度string valg=null((value.length ) (test.max )|| value.length ) (test.min ) ) (system.out.println ).destem ) } } } publicstaticvoidmain (string [ ] args ) annoationtestannotation=newannoationtest ); annotation.setname(ABCEFG ); annotation.set pasdword (1234567890 ); try{valid(annotation ); }catch(illegalaccessexceptione ) { e.printStackTrace ); }}根据用户设置的属性长度,检查属性值是否满足要求

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