注解的定义和使用
package com. bjpowernode. javase. annotation ;
public @interface MyAnnotation {
}
package com. bjpowernode. javase. annotation ;
@MyAnnotation
public class AnnotationTest01 {
@MyAnnotation
private int no;
@MyAnnotation
public AnnotationTest01 ( ) {
}
@MyAnnotation
public static void m1 ( @MyAnnotation String name,
@MyAnnotation int k) {
@MyAnnotation
int a = 100 ;
}
@MyAnnotation
public void m2 ( ) {
}
}
@MyAnnotation
interface MyInterface {
}
@MyAnnotation
enum Seasion {
SPRING , SUMMER , AUTUMN , WINTER ;
}
package com. bjpowernode. javase. annotation ;
@MyAnnotation
public @interface OthreAnnotation {
}
JDK内置注解
Override注解
public class AnnotationTest02 {
@Override
public String toString ( ) {
return "toString" ;
}
}
元注解
Deprecated注解
package com. bjpowernode. javase. annotation ;
public class AnnotationTest03 {
public static void main ( String [ ] args) {
AnnotationTest03 at = new AnnotationTest03 ( ) ;
at. doSome ( ) ;
}
@Deprecated
public void doSome ( ) {
System . out. println ( "do Some..." ) ;
}
@Deprecated
public static void doOther ( ) {
System . out. println ( "do Other..." ) ;
}
}
class T {
public static void main ( String [ ] args) {
AnnotationTest03 at = new AnnotationTest03 ( ) ;
at. doSome ( ) ;
AnnotationTest03 . doOther ( ) ;
}
}
注解中定义属性
package com. bjpowernode. javase. annotation2 ;
public @interface MyAnnotation {
String name ( ) ;
String color ( ) ;
int age ( ) default 25 ;
}
package com. bjpowernode. javase. annotation2 ;
public class MyAnnotaionTest {
@MyAnnotation ( name= "zhangsan" , color= "red" )
public void doSome ( ) {
}
}
属性是value时,可省略
package com. bjpowernode. javase. annotation3 ;
public @interface MyAnnotation {
String value ( ) ;
}
package com. bjpowernode. javase. annotation3 ;
public class MyAnnotaionTest {
@MyAnnotation ( value= "hehe" )
public void doSome ( ) {
}
@MyAnnotation ( "haha" )
public void doOther ( ) {
}
}
package com. bjpowernode. javase. annotation3 ;
public @interface OtherAnnotation {
String name ( ) ;
}
package com. bjpowernode. javase. annotation3 ;
public class OtherAnnotationTest {
@OtherAnnotation ( name = "test" )
public void doSome ( ) {
}
}
属性是一个数组
package com. bjpowernode. javase. annotation4 ;
public @interface MyAnnotaion {
int value1 ( ) ;
String value2 ( ) ;
int [ ] value3 ( ) ;
String [ ] value4 ( ) ;
Season value5 ( ) ;
Season [ ] value6 ( ) ;
Class parameterType ( ) ;
Class [ ] parameterTypes ( ) ;
}
public enum Season {
SPING , SUMMER , AUTUMN , WINTER
}
package com. bjpowernode. javase. annotation4 ;
public @interface OtherAnnotation {
int age ( ) ;
String [ ] email ( ) ;
Season [ ] seasonArray ( ) ;
}
package com. bjpowernode. javase. annotation4 ;
import java. lang. annotation. RetentionPolicy ;
public class OtherAnnotationTest {
@OtherAnnotation ( age= 25 , email= {
"[email protected] " , "lisi@souhu" } , seasonArray = Season . AUTUMN )
public void doSome ( ) {
}
@OtherAnnotation ( age= 25 , email= "[email protected] " , seasonArray = {
Season . SPING , Season . AUTUMN } )
public void doOther ( ) {
}
}
反射注解
package com. bjpowernode. javase. annotation5 ;
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 MyAnnotation {
String value ( ) default "北京大兴区" ;
}
package com. bjpowernode. javase. annotation5 ;
import com. bjpowernode. javase. annotation5. MyAnnotation ;
@MyAnnotation ( "上海浦东区" )
public class MyAnnotationTest {
@MyAnnotation
public void doSome ( ) {
}
}
package com. bjpowernode. javase. annotation5 ;
public class ReflectAnnotationTest {
public static void main ( String [ ] args) throws Exception {
Class c = Class . forName ( "com.bjpowernode.javase.annotation5.MyAnnotationTest" ) ;
System . out. println ( c. isAnnotationPresent ( MyAnnotation . class ) ) ;
if ( c. isAnnotationPresent ( MyAnnotation . class ) ) {
MyAnnotation myAnnotation = ( MyAnnotation ) c. getAnnotation ( MyAnnotation . class ) ;
System . out. println ( "类上面的注解对象: " + myAnnotation) ;
String value = myAnnotation. value ( ) ;
System . out. println ( value) ;
}
Class stringClass = Class . forName ( "java.lang.String" ) ;
System . out. println ( stringClass. isAnnotationPresent ( MyAnnotation . class ) ) ;
}
}
通过反射获取注解对象属性的值
package com. bjpowernode. javase. annotation6 ;
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 MyAnnotation {
String username ( ) ;
String password ( ) ;
}
package com. bjpowernode. javase. annotation6 ;
import java. lang. reflect. Method ;
public class MyAnnotationTest {
@MyAnnotation ( username = "admin" , password = "123" )
public void doSome ( ) {
}
public static void main ( String [ ] args) throws Exception {
Class c = Class . forName ( "com.bjpowernode.javase.annotation6.MyAnnotationTest" ) ;
Method doSomeMethod = c. getDeclaredMethod ( "doSome" ) ;
if ( doSomeMethod. isAnnotationPresent ( MyAnnotation . class ) ) {
MyAnnotation myAnnotation = doSomeMethod. getAnnotation ( MyAnnotation . class ) ;
System . out. println ( myAnnotation. username ( ) ) ;
System . out. println ( myAnnotation. password ( ) ) ;
}
}
}
注解的应用实例
package com. bjpowernode. javase. annotation7 ;
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 {
}
package com. bjpowernode. javase. annotation7 ;
@Id
public class User {
String name;
String password;
}
package com. bjpowernode. javase. annotation7 ;
import java. lang. reflect. Field ;
public class Test {
public static void main ( String [ ] args) throws Exception {
Class userClass = Class . forName ( "com.bjpowernode.javase.annotation7.User" ) ;
if ( userClass. isAnnotationPresent ( Id . class ) ) {
Field [ ] fields = userClass. getDeclaredFields ( ) ;
boolean isOk = false ;
for ( Field field : fields) {
if ( "id" . equals ( field. getName ( ) ) && "int" . equals ( field. getType ( ) . getSimpleName ( ) ) ) {
isOk = true ;
}
}
if ( ! isOk) {
throw new HasNotIdPropertyException ( "被@Id注解标注的类中必须要有一个int类型的id属性!" ) ;
}
}
}
}
package com. bjpowernode. javase. annotation7 ;
public class HasNotIdPropertyException extends RuntimeException {
public HasNotIdPropertyException ( ) {
}
public HasNotIdPropertyException ( String s) {
super ( s) ;
}
}