javase-annotation-210710-02
注解中可以用的属性类型 反射获取类上的注解 反射获取类中方法上的注解 注解实例
注解中可以用的属性类型
MyAnnotation0301.java
package bgy_annotation_02 ;
public @interface MyAnnotation0301 {
int value01 ( ) ;
int [ ] value0101 ( ) ;
String value02 ( ) ;
String [ ] value0202 ( ) ;
Season value03 ( ) ;
Season [ ] value0303 ( ) ;
Class value04 ( ) ;
Class [ ] value0404 ( ) ;
}
MyAnnotation0302.java
package bgy_annotation_02 ;
public @interface MyAnnotation0302 {
int age ( ) ;
String [ ] email ( ) ;
}
MyAnnotation0303.java
package bgy_annotation_02 ;
public @interface MyAnnotation0303 {
int age ( ) ;
String [ ] email ( ) ;
Season [ ] seasonArray ( ) ;
}
Season.java
package bgy_annotation_02 ;
public enum Season {
SPRING, SUMMER, AUTUMN, WINTER;
}
AnnotationTest03.java
package bgy_annotation_02 ;
public class AnnotationTest03 {
@MyAnnotation0302 ( age = 22 , email = { "1111111111@qq.com" , "baiguangyi@souhu.com" } )
public void doSome ( ) {
}
@MyAnnotation0303 ( age = 22 , email = "123456789@qq.com" , seasonArray = { Season . SPRING, Season . AUTUMN} )
public void doOther ( ) {
}
}
反射获取类上的注解
MyAnnotation0401.java
package bgy_annotation_03 ;
import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Target ( { ElementType . TYPE, ElementType . METHOD} )
@Retention ( RetentionPolicy . RUNTIME)
public @interface MyAnnotation0401 {
String value ( ) default "河南" ;
}
MyAnnotationTest04.java
package bgy_annotation_03 ;
@MyAnnotation0401
public class MyAnnotationTest04 {
@MyAnnotation0401
public void doSome ( ) {
}
}
AnnotationTest04.java
package bgy_annotation_03 ;
public class AnnotationTest04 {
public static void main ( String [ ] args) throws Exception {
Class myAnnotationTest04Class = Class . forName ( "bgy_annotation_03.MyAnnotationTest04" ) ;
if ( myAnnotationTest04Class. isAnnotationPresent ( MyAnnotation0401 . class ) ) {
MyAnnotation0401 myAnnotation0401 = ( MyAnnotation0401 ) myAnnotationTest04Class. getAnnotation ( MyAnnotation0401 . class ) ;
String value = myAnnotation0401. value ( ) ;
System . out. println ( "注解对象value属性的值为:" + value) ;
}
}
}
反射获取类中方法上的注解
MyAnnotation0501.java
package bgy_annotation_04 ;
import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Retention ( RetentionPolicy . RUNTIME)
@Target ( { ElementType . METHOD} )
public @interface MyAnnotation0501 {
String username ( ) ;
String password ( ) ;
}
AnnotationTest05.java
package bgy_annotation_04 ;
import java. lang. annotation. Annotation ;
import java. lang. reflect. Method ;
public class AnnotationTest05 {
@MyAnnotation0501 ( username = "bgy" , password = "123" )
public void doSome ( ) {
}
public static void main ( String [ ] args) throws Exception {
Class annotationTest05Class = Class . forName ( "bgy_annotation_04.AnnotationTest05" ) ;
Method doSomeMethod = annotationTest05Class. getMethod ( "doSome" ) ;
if ( doSomeMethod. isAnnotationPresent ( MyAnnotation0501 . class ) ) {
MyAnnotation0501 annotation = doSomeMethod. getAnnotation ( MyAnnotation0501 . class ) ;
System . out. println ( annotation. username ( ) ) ;
System . out. println ( annotation. password ( ) ) ;
}
}
}
注解实例
要求:
有一个叫 @Id 的注解
这个注解只能在类上面出现,
当这个类有@Id注解的时候,要求这个类中必须有一个int类型的id属性,
如果没有这个属性就报异常,
如果有这个属性则正常运行。
Id.java
package bgy_annotation_05 ;
import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Target ( ElementType . TYPE)
@Retention ( RetentionPolicy . RUNTIME)
public @interface ID {
}
User.java
package bgy_annotation_05 ;
@ID
public class User {
int id;
String name;
String password;
}
HasNotIdPropertyException.java
package bgy_annotation_05 ;
public class HasNotIdPropertyException extends RuntimeException {
public HasNotIdPropertyException ( ) {
}
public HasNotIdPropertyException ( String message) {
super ( message) ;
}
}
AnnotationTest06.java
package bgy_annotation_05 ;
import java. lang. reflect. Field ;
public class AnnotationTest06 {
public static void main ( String [ ] args) throws Exception {
Class userClass = Class . forName ( "bgy_annotation_05.User" ) ;
if ( userClass. isAnnotationPresent ( ID. class ) ) {
boolean flag = false ;
Field [ ] fields = userClass. getDeclaredFields ( ) ;
for ( Field field : fields) {
if ( "id" . equals ( field. getName ( ) ) && "int" . equals ( field. getType ( ) . getSimpleName ( ) ) ) {
flag = true ;
break ;
}
}
if ( ! flag) {
throw new HasNotIdPropertyException ( "被@Id注解标记的类中必须要有一个int类型的id属性" ) ;
}
}
}
}