java自定义注解使用案例_java 自定义注解(翻译)

在android 的开源项目butterknife,使用注解的方式,解放了android中view的注入,下面通过一个列子学习一下注解。

在这个例子中,向你展示怎样创建两个自定义注解Annotation---@Test和@TestInfo, 进行单元测试

1:@Test 注解

这个@interface告诉java这个一个自定义的注解,然后你可以使用这个注解在类的方法上使用,例如:@Test(enable=false).

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;

}

笔记:

@Retention,@Target 都是元注解,(用来标注注解的注解叫做元注解)

@Retention(RetentionPolicy.RUNTIME) 表示运行时也保留该注解

@Target(ElementType.METHOD) 表示只能在方法的上面使用它,如果是ElementType.TYPE就表示在类的上面使用。

2.@TesterInfo 注解

这个@TesterInfo自定义注解应用到类上,这个展示了其中方法返回的不同类型,有枚举,数组,字符串等

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 level

public @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";

}

@Target(ElementType.TYPE) 表示只能就表示在类的上面使用。

3: 单元测试的例子

使用自定义的@Test,和@TesterInfo,在这个测试例子中。

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 {

@Test

void 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.

}

}

}

上面的例子中,@Testinfo用在类上,@test用在方法上

4. 反射获取注解

下面的这个例子,通过反射的api,去读取自定义注解的信息

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 obj = TestExample.class;

// Process @TesterInfo

if (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 @Test

for (Method method : obj.getDeclaredMethods()) {

// if method is annotated with @Test

if (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 'testC' - passed

3 - Test 'testB' - ignored

Result : Total : 3, Passed: 1, Failed 1, Ignore 1

先是获取类上的注解@TesterInfo的信息,然后获取方法上的注解@Test的信息

其中在android stuido中运行java文件,可以在android项目的单元测试中编写,然后运行就好。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值