package Test2;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface demo2{
String classname();
String name();
}
package Test2;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@demo2(classname = "Test1.demo1.student",name = "qiuhe1")
public class demotest {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<demotest> dt = demotest.class;
demo2 annotation = dt.getAnnotation(demo2.class);
System.out.println(annotation);
String classname = annotation.classname();
String name = annotation.name();
System.out.println(name);
System.out.println(classname);
Class<?> aClass = Class.forName(classname);
Object o = aClass.newInstance();
Method method = aClass.getMethod(name);
method.invoke(o);
}
}