![]()
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
String className();
String methodName();
}
@MyAnno(className = "com.howhy.demo.Person", methodName ="say" )
public class Demo1 {
public static void main(String[] args) throws Exception {
Class<Demo1> cls=Demo1.class;
MyAnno annotation = cls.getAnnotation(MyAnno.class);
String s = annotation.className();
String s1 = annotation.methodName();
Class<?> aClass = Class.forName(s);
Object constructor = aClass.newInstance();
Method method = aClass.getMethod(s1);
method.invoke(constructor);
}
}