首页 > 编程知识 正文

java方法注释,@conditional注解

时间:2023-05-04 05:06:29 阅读:175378 作者:4405

Java注释@interface@Retention@Target等的用法@ override @ deprecated @ suppress warnings @ retention @ target

java在@interface Annotation{ }中定义了注释@Annotation,注释是一个类

@Override,@Deprecated,@SuppressWarnings是常见的三个评论。

注释相当于一种标记,对程序进行注释就等于对程序进行某种标记,以后,

JAVAC编译器、开发工具和其他程序可以反射性地知道类和各种元素是否有标记,可以看到标记了什么,然后做相应的事情

@Override注释@Override用于方法中,想重写方法时给方法加上@Override,方法名称错误时编译器报告错误。

@Deprecated注释@Deprecated表示某个类的属性或方法过时,不想被别人使用时,用@Deprecated对属性或方法进行修饰。

@SuppressWarnings注释@SuppressWarnings用于抑制程序发出的警告,例如在没有通用用法或方法过时时。

@Retention注释@Retention可用于修饰注释,是一种称为元注释的注释

Retention注释具有RetentionPolicy类型的属性value,Enum RetentionPolicy是枚举类型。

可以将此枚举理解为确定如何保留Retention注释,并将Rentention和RententionPolicy结合使用。 RetentionPolicy有三个值: CLASS RUNTIME SOURCE

@ retention (retention policy.class )限定的注释存储在表示注释的信息为class文件(字节码文件)的计算机上,并在程序编译时保留

@ retention (retention policy.source )限定的注释表示编译器将放弃注释信息,它不会保留在class文件中,而只保留在源文件中。

@ retention (retention policy.runtime )修饰的注释,表示注释的信息保存在class文件(字节码文件)中,程序编译时,由虚拟机执行

所以他们可以反射性地读取。 RetentionPolicy.RUNTIME可用于从JVM中读取Annotation注释信息和分析程序。

package com.self; import Java.lang.annotation.retention; import Java.lang.annotation.retention policy; @ retention (retentionpolicy.runtime ) public @interface MyTarget { }注释@MyTarget,用retention policy.runtime限定;

package com.self; import java.lang.reflect.Method; publicclassmytargettest { @ mytargetpublicvoiddosomething () system.out.println('Helloworld ' ); }publicstaticvoidmain(string[] args ) throwsexception ) method method=my target test.class.get method ) do something ' if (method.isannotationpresent ) 如果存在,则为true ) system.out.println打印以上程序。 @com.self.MyTarget (,如果RetentionPolicy的值不是运行时间,则不打印

@ retention (retention policy.source ) public @ interface override @ retention (retention policy.source ) )。 public @ interfacesuppresswarnings @ retention (retention policy.runtime ) public @interface Deprecated从上面可以看出,注释@ DDD

可以在注释中定义属性。 请看示例。

@ retention (retention policy.runtime ) public @ interfacemyannotation { stringh }

ello() default "gege"; String world(); int[] array() default { 2, 4, 5, 6 }; EnumTest.TrafficLamp lamp() ; TestAnnotation lannotation() default @TestAnnotation(value = "ddd"); Class style() default String.class; }

上面程序中,定义一个注解@MyAnnotation,定义了6个属性,他们的名字为:

hello,world,array,lamp,lannotation,style.

属性hello类型为String,默认值为gege
属性world类型为String,没有默认值
属性array类型为数组,默认值为2,4,5,6
属性lamp类型为一个枚举,没有默认值
属性lannotation类型为注解,默认值为@TestAnnotation,注解里的属性是注解
属性style类型为Class,默认值为String类型的Class类型
看下面例子:定义了一个MyTest类,用注解@MyAnnotation修饰,注解@MyAnnotation定义的属性都赋了值

@MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class) public class MyTest { @MyAnnotation(lannotation=@TestAnnotation(value="wgdls"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW) @Deprecated @SuppressWarnings("") public void output() { System.out.println("output something!"); } }

接着通过反射读取注解的信息:

public class MyReflection { public static void main(String[] args) throws Exception { MyTest myTest = new MyTest(); Class<MyTest> c = MyTest.class; Method method = c.getMethod("output", new Class[] {}); //如果MyTest类名上有注解@MyAnnotation修饰,则为true if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) { System.out.println("have annotation"); } if (method.isAnnotationPresent(MyAnnotation.class)) { method.invoke(myTest, null); //调用output方法 //获取方法上注解@MyAnnotation的信息 MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); String hello = myAnnotation.hello(); String world = myAnnotation.world(); System.out.println(hello + ", " + world);//打印属性hello和world的值 System.out.println(myAnnotation.array().length);//打印属性array数组的长度 System.out.println(myAnnotation.lannotation().value()); //打印属性lannotation的值 System.out.println(myAnnotation.style()); } //得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的 Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); } } }

上面程序打印:

have annotation output something! gege, shanghai 3 wgdls class java.lang.String com.heima.annotation.MyAnnotation java.lang.Deprecated

如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。

可见,@Retention(RetentionPolicy.RUNTIME )注解中,RetentionPolicy.RUNTIME是注解属性值,它的属性名字就是是value,

属性的返回类型是RetentionPolicy,如下:

public @interface MyTarget { String value(); } 可以这样用: @MyTarget("aaa") public void doSomething() { System.out.println("hello world"); } @Target

注解@Target也是用来修饰注解的元注解,它有一个属性ElementType也是枚举类型,

值为:
ANNOTATION_TYPE
CONSTRUCTOR
FIELD LOCAL_VARIABLE
METHOD PACKAGE
PARAMETER TYPE

如@Target(ElementType.METHOD) 修饰的注解表示该注解只能用来修饰在方法上

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyTarget { String value() default "hahaha"; }

如把@MyTarget修饰在类上,则程序报错,如下:

@MyTarget public class MyTargetTest

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