私人定制注释
来源:互联网 发布:iphone淘宝新版换旧版 编辑:程序博客网 时间:2024/06/10 00:25
package com.mkyong.test.core; import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD) //can use in method only.public @interface Test { //should ignore this test?public boolean enabled() default true; }
package com.mkyong.test.core; import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE) //on class levelpublic @interface TesterInfo { public enum Priority { LOW, MEDIUM, HIGH} Priority priority() default Priority.MEDIUM; String[] tags() default ""; String createdBy() default "Mkyong"; String lastModified() default "03/01/2014"; }
package com.mkyong.test;import com.mkyong.test.core.Test;import com.mkyong.test.core.TesterInfo;import com.mkyong.test.core.TesterInfo.Priority;@TesterInfo(priority = Priority.HIGH, createdBy = "mkyong.com", tags = {"sales", "test" })public class TestExample {@Testvoid testA() {if (true)throw new RuntimeException("This test always failed");}@Test(enabled = false)void testB() {if (false)throw new RuntimeException("This test always passed");}@Test(enabled = true)void testC() {if (10 > 1) {// do nothing, this test always passed.}}}
package com.mkyong.test;import java.lang.annotation.Annotation;import java.lang.reflect.Method;import com.mkyong.test.core.Test;import com.mkyong.test.core.TesterInfo;public class RunTest {public static void main(String[] args) throws Exception {System.out.println("Testing...");int passed = 0, failed = 0, count = 0, ignore = 0;Class<TestExample> obj = TestExample.class;// Process @TesterInfoif (obj.isAnnotationPresent(TesterInfo.class)) {Annotation annotation = obj.getAnnotation(TesterInfo.class);TesterInfo testerInfo = (TesterInfo) annotation;System.out.printf("%nPriority :%s", testerInfo.priority());System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());System.out.printf("%nTags :");int tagLength = testerInfo.tags().length;for (String tag : testerInfo.tags()) {if (tagLength > 1) {System.out.print(tag + ", ");} else {System.out.print(tag);}tagLength--;}System.out.printf("%nLastModified :%s%n%n",testerInfo.lastModified());}// Process @Testfor (Method method : obj.getDeclaredMethods()) {// if method is annotated with @Testif (method.isAnnotationPresent(Test.class)) {Annotation annotation = method.getAnnotation(Test.class);Test test = (Test) annotation;// if enabled = true (default)if (test.enabled()) {try {method.invoke(obj.newInstance());System.out.printf("%s - Test '%s' - passed %n",++count, method.getName());passed++;} catch (Throwable ex) {System.out.printf("%s - Test '%s' - failed: %s %n",++count, method.getName(), ex.getCause());failed++;}} else {System.out.printf("%s - Test '%s' - ignored%n", ++count,method.getName());ignore++;}}}System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n",count, passed, failed, ignore);}}Testing...
Priority :HIGH
CreatedBy :mkyong.com
Tags :sales, test
LastModified :03/01/2014
1 - Test 'testA' - failed: java.lang.RuntimeException: This test always failed
2 - Test 'testB' - ignored
3 - Test 'testC' - passed
Result : Total : 3, Passed: 1, Failed 1, Ignore 1
http://www.mkyong.com/java/java-custom-annotations-example/
0 0
- 私人定制注释
- 私人定制 观感
- 2014上半年私人定制
- 私人定制--家庭理财软件
- 私人定制推送铃声
- unity3d控制台 私人定制
- 私人定制:linux系统
- LaTex:私人定制
- 私人定制的联系人app
- 爱在空中,私人定制
- mj评-《私人定制》-6分
- 装饰模式--私人定制冬装夏装
- OSX: 私人定制Dock默认程序图标
- 打造国内私人定制护栏第一家
- 利用BusyBox ~私人定制 My LINUX~
- Unity Editor Extensions – Inspectors私人定制
- 几分钟私人定制APP全攻略!!
- Linuxmint17.2我的私人定制
- linux为用户添加sudo权限
- Vim命令图解和XVim使用
- Running rsync as a daemon
- 更新 Debian 的 SVN 版本
- Latex 表格
- 私人定制注释
- win7内核调试
- html 一个表格的例子
- terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
- 深层置信网的快速学习方法(Deep Learning)
- android viewpager 无限循环实现gallery 效果
- LeetCode Search a 2D Matrix
- events/0--工作者线程 .
- [Leetcode] Longest Common Prefix (Java)