注解小示例,主要讲解7种方法的用法:
返回值 | 方法 | 说明 |
boolean | isAnnotationPresent(MyAnno.class) | 钉点上是否有该注解 |
MyAnno | getAnnotation(MyAnno.class) | 获取某注解 |
Annotation[ ] | getAnnotations() | 返回所有注解 |
MyAnno[ ] | getAnnotationsByType(MyAnno.class) | 获取jdk8的重复注解 兼容getAnnotation |
抛开@Inherited继承性,直接钉点上就出现的注解(getDeclare*的若干方法) | ||
MyAnno | getDeclaredAnnotation(MyAnno.class) | |
Annotation[ ] | getDeclaredAnnotations() | |
MyAnno[ ] | getDeclaredAnnotationsByType(MyAnno.class) |
import java.lang.reflect.*;
import java.lang.annotation.*;
//源码阶段的注解
@Retention(RetentionPolicy.SOURCE)
@interface Becareful{
int message();
}
//编译阶段的注解
@Retention(RetentionPolicy.CLASS)
@interface What{
String description();
}
//运行阶段的注解:空注解:也即标记注解
@Retention(RetentionPolicy.RUNTIME)
@interface Id{}
//运行阶段的注解:单属性注解
@Retention(RetentionPolicy.RUNTIME)
@interface Singleton{
String value() default "singleton";
}
//运行阶段的注解:多属性注解,有的属性携带默认值
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno{
String str() default "12345";
int val();
}
//运行阶段的注解:
//钉在类上的注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface ClassAnno{
String classanno() default "classanno";
}
//运行阶段的注解:
//钉在父类上的注解:
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface ParentAnno{
String parent() default "parent";
}
//容器
@Retention(RetentionPolicy.RUNTIME)
@interface Persons {
Person[] value();
}
//容器内的属性数组,数组中的每个元素也是注解
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Persons.class)
@interface Person{
char value() ;
}
//父类上的容器
@Retention(RetentionPolicy.RUNTIME)
@interface Fathers {
Father[] value();
}
//父类上的容器内的属性数组,数组中的每个元素也是注解
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Fathers.class)
@interface Father{
String father() default "";
}
@ParentAnno
@Father(father="father1")
@Father(father="father2")
class Parent //父类上的注解被子类继承
{
}
@ClassAnno
@Person('A')
@Person('B')
public class ChildAnnotationTest extends Parent
{
@Id
private int id;
@Singleton
private static ChildAnnotationTest instance = new ChildAnnotationTest();
@Person('男')
@Person('女')
private char sex;
public void testIsAnnotationPresent(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
System.out.println( clazz.getDeclaredField("id").isAnnotationPresent(Id.class) );
System.out.println("clazz.getDeclaredField( id ).isAnnotationPresent(Id.class)\r\n");
}catch(Exception e){}
}
public void testGetAnnotations(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
Annotation[] annos = clazz.getAnnotations() ;
for(Annotation anno : annos)
System.out.println( anno );
System.out.println("clazz.getAnnotations()\r\n");
}catch(Exception e){}
}
public void testGetDeclaredAnnotations(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
Annotation[] annos = clazz.getDeclaredAnnotations() ;
for(Annotation anno : annos)
System.out.println( anno );
System.out.println("clazz.getDeclaredAnnotations()\r\n");
}catch(Exception e){}
}
public void testGetAnnotation(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
System.out.println( clazz.getDeclaredField("instance").getAnnotation(Singleton.class) );
System.out.println("clazz.getDeclaredField( instance ).getAnnotation(Singleton.class)\r\n");
}catch(Exception e){}
}
public void testGetAnnotationsByType(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
Field f = clazz.getDeclaredField("sex");
Person[] annos = f.getAnnotationsByType(Person.class);
for(Person anno : annos)
System.out.println( anno.value() );
System.out.println("clazz.getDeclaredField( sex ).getAnnotationsByType(Persons.class)\r\n");
}catch(Exception e){}
}
public void testGetDeclaredAnnotation(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
System.out.println( clazz.getDeclaredAnnotation(ParentAnno.class) );
System.out.println("clazz.getDeclaredAnnotation(ParentAnno.class)\r\n");
}catch(Exception e){}
}
public void testGetDeclaredAnnotationsByType(){
try{
Class<?> clazz = ChildAnnotationTest.class ;
Father[] annos = clazz.getDeclaredAnnotationsByType(Father.class) ;
for(Father anno : annos)
System.out.println( anno );
System.out.println("clazz.getDeclaredAnnotationsByType(Father.class)........empty!!!\r\n");
Person[] as = clazz.getDeclaredAnnotationsByType(Person.class) ;
for(Person anno : as)
System.out.println( anno );
System.out.println("clazz.getDeclaredAnnotationsByType(Person.class)\r\n");
}catch(Exception e){}
}
public static void main(String[] args){
ChildAnnotationTest child = new ChildAnnotationTest();
try{
Class<?> clazz = ChildAnnotationTest.class ;
Method[] mtds = clazz.getDeclaredMethods();
for(Method m : mtds){
if(m.getName().equals("main")) continue;
m.invoke(child);
}
}catch(Exception e){
e.printStackTrace() ;
}
}
}