测试类
package top.ltyzqhh.testAnnotataion;
public class testAnnotataion {
@LtyzqhhAnnotation
public void test(){
System.out.println("这是test的方法");
}
}
注解
package top.ltyzqhh.testAnnotataion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LtyzqhhAnnotation {
}
对注解进行操作的类
package top.ltyzqhh.testAnnotataion;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args)throws Exception{
//扫描所指定的类
Class cla=Class.forName("top.ltyzqhh.testAnnotataion.testAnnotataion");
//得到方法
Method[] methods = cla.getMethods();
//方法不为空的话
if (methods!=null){
//对方法进行判断是否存在LtyzqhhAnnotation类
for (Method method : methods) {
boolean isLtyzqhh = method.isAnnotationPresent(LtyzqhhAnnotation.class);
if (isLtyzqhh){ //如果是的话进行反射执行其方法
method.invoke(cla.getConstructor(null).newInstance(null),null);
}
}
}
}
}