package com.test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestExample {
@Test
public int Arithmetic(int a,int b){
int and=a+b;
return and;
}
public static void main(String[] args){
Method[] methods = TestExample.class.getDeclaredMethods();
for(int i=0;i<methods.length;i++){
Method method=methods[i];
boolean hasAnnotation = method.isAnnotationPresent(Test.class);
if (hasAnnotation) {
Test annotation = method.getAnnotation(Test.class);
System.out.print(annotation+"|"+method.getName());
}
}
}
}
等价于:
package com.test;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestExample {
@Test
public int Arithmetic(int a,int b){
int and=a+b;
return and;
}
public static void main(String[] args){
Method[] methods = TestExample.class.getDeclaredMethods();
for (Method method : methods) {
boolean hasAnnotation = method.isAnnotationPresent(Test.class);
if (hasAnnotation) {
Test annotation = method.getAnnotation(Test.class);
System.out.print(annotation+"|"+method.getName());
}
}
}
}