在运行时,在方法上被检测到。
@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); } } } }