📄 annotationmain.java
字号:
package test.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class AnnotationMain {
TestAn test = new TestAn();
public static void main(String[] args) {
// 15. Class cl = obj.getClass();
// 16. for (Method m : cl.getDeclaredMethods())
// 17. {
// 18. ActionListenerFor a = m.getAnnotation(ActionListenerFor.class);
// 19. if (a != null)
// 20. {
// 21. Field f = cl.getDeclaredField(a.source());
// 22. f.setAccessible(true);
// 23. addListener(f.get(obj), obj, m);
// 24. }
// 25. }
AnnotationMain ma = new AnnotationMain();
Class c = ma.test.getClass();
System.out.println("----- Begin .....new ------------");
for (Method m : c.getDeclaredMethods()) {
System.out.println(m.getName());
Truth a = m.getAnnotation(Truth.class);
if (a != null) {
System.out.println(a.getString());
System.out.println(a.name());
}else{
System.out.println("Annotation is null!");
}
}
System.out.println("----- End .....new ------------");
Annotation[] ans = c.getAnnotations();
Method[] methods = c.getMethods();
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
for (int i = 0; i < methods.length; i++) {
Annotation[] temps = methods[i].getAnnotations();
String methodName = methods[i].getName();
if (methodName.startsWith("anMethod")) {
System.out.println(methods[i]);
System.out.println("------- Annotation Of Mehtod----------");
Annotation[] as = methods[i].getAnnotations();
// System.out.println(as[0]);
}
for (int j = 0; j < temps.length; j++) {
System.out.println(temps[j]);
}
}
// the Annotation's Class
Class truth = Truth.class;
Annotation[] truths = truth.getAnnotations();
for (int j = 0; j < truths.length; j++) {
System.out.println("TypeOf this Annotation: "
+ truths[j].annotationType());
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -