注解小例一则

很简单的一个例子,下面的代码是一个Test的标签,我希望它用来标记静态无参方法
在运行时,在方法上被检测到。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {

}


一个简单的测试类,如何控制午餐静态 就是下面控制的
public class RunTest {
	public static void main(String[] args) throws ClassNotFoundException {
		Class testClass =Class.forName(args[0]);
		for(Method m : testClass.getDeclaredMethods()){
			if(m.isAnnotationPresent(Test.class)){
				try {
					m.invoke(null);
				} catch (InvocationTargetException e) {
					System.out.println(m + " failed : " + e.getCause());
				}catch(Exception e){
					System.out.println("Invalid @Test " + m);
				}
			}
		}
		
}

猜你喜欢

转载自zenghuiss.iteye.com/blog/2286576