本文共 2916 字,大约阅读时间需要 9 分钟。
packagesample;importjava.lang.annotation.Annotation;importjava.lang.reflect.Field;importjava.lang.reflect.Method;public classClient {public static voidmain(String[] args){
System.out.println("start annotation sample");
SampleClass1 sc1= newSampleClass1();
SampleClass2 sc2= newSampleClass2();//取得指定的“注解”
Annotation annotList[] = Info.class.getAnnotations();
System.out.println("annotation size is [" + annotList.length + "]");for(int i=0; i
Annotation anno=annotList[i];
System.out.println(" annotation class is [" + anno.annotationType().getName() + "]");
}/****取得带有注解的CLASS****/
//SampleClass1
Annotation annotList1[] = SampleClass1.class.getAnnotations();
System.out.println("SampleClass1's annotation size is [" + annotList1.length + "]");for(int i=0; i
Annotation anno1=annotList1[i];
System.out.println(" annotation class is [" + anno1.annotationType().getName() + "]");
}//SampleClass2
Annotation annotList2[] = SampleClass2.class.getAnnotations();
System.out.println("SampleClass2's annotation size is [" + annotList2.length + "]");for(int i=0; i
Annotation anno2=annotList2[i];
System.out.println(" annotation class is [" + anno2.annotationType().getName() + "]");
}
System.out.println();
System.out.println();/****取得带有注解的METHOD****/
//SampleClass1
Method methodList1[] = SampleClass1.class.getMethods();
System.out.println("SampleClass1's method count is [" + methodList1.length + "]");for(Method method : methodList1){
System.out.println(" method name is [" + method.getName() + "]");for(Annotation annot : method.getAnnotations()){
System.out.println(" method annotation is [" + annot.annotationType().getName() + "]");
}
}//SampleClass2
Method methodList2[] = SampleClass1.class.getMethods();
System.out.println("SampleClass2's method count is [" + methodList2.length + "]");for(Method method : methodList2){
System.out.println(" method name is [" + method.getName() + "]");for(Annotation annot : method.getAnnotations()){
System.out.println(" method annotation is [" + annot.annotationType().getName() + "]");
}
}
System.out.println();
System.out.println();/****取得带有注解的FIELD(成员变量)****/
//SampleClass1
Field fieldList1[] = SampleClass1.class.getFields();
System.out.println("SampleClass1 has [" + fieldList1.length + "] fields");for(Field field : fieldList1){
System.out.println(" field name is [" + field.getName() +"]");for(Annotation annot : field.getAnnotations()){
System.out.println(" field annotation is [" + annot.annotationType().getName() + "]");
}
}//SampleClass1
Field fieldList2[] = SampleClass2.class.getDeclaredFields();//getFields();
System.out.println("SampleClass2 has [" + fieldList2.length + "] fields");for(Field field : fieldList2){
System.out.println(" field name is [" + field.getName() +"]");for(Annotation annot : field.getAnnotations()){
System.out.println(" field annotation is [" + annot.annotationType().getName() + "]");
}
}
System.out.println();
System.out.println();
}
}
转载地址:https://blog.csdn.net/weixin_32661831/article/details/114719865 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!