import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.CONSTRUCTOR})//
@Retention(RetentionPolicy.RUNTIME)//
public @interface MyAnn {
String name();//
int age();
//Integer age2();
}
public class TestAnn {
@MyAnn(name="zhang",age=20)//
public void test(String name,int age){
System.out.print(name+""+age);
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception{
Class c=TestAnn.class;
Object obj =c.newInstance();
Method [] m=c.getMethods();
for(Method mm:m){
if(mm.isAnnotationPresent(MyAnn.class)){//
MyAnn ma=mm.getAnnotation(MyAnn.class);//
mm.invoke(obj, ma.name(),ma.age());
}
}
}
}