Java注解(自定义方法注解使用及测试)

来源:互联网 发布:java ajax文件上传 编辑:程序博客网 时间:2024/06/06 04:05

目标,创建一个方法注解,我们能够初始化时执行该方法,并可以给该方法传入注解的参数值

假设我们设计一个 sayHello(String name) 方法,给该方法做个注解,初始时使用注解传入"小明"

package annotation;public class HelloWorldStub {@HelloWorldAnnotation(name = "小明")public String sayHello(String name) {if (name == null ) {name = "";} return name + " say hello world!";}}

定义注解

package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)//注解会在class中存在,运行时可通过反射获取@Target(ElementType.METHOD)//目标是方法@Documented//文档生成时,该注解将被包含在javadoc中,可去掉public @interface HelloWorldAnnotation {public String name() default "";}

我们的测试类 

package annotation;import java.lang.reflect.InvocationTargetException;import org.junit.Test;import static org.junit.Assert.assertEquals;public class TestHelloWorldAnnotation {@Testpublic void testHello() throws IllegalArgumentException,IllegalAccessException, InvocationTargetException,SecurityException, NoSuchMethodException, InstantiationException {//定义操作类ParseAnnotationStub parse = new ParseAnnotationStub();//假设我们知道类HelloWorldStub使用了注解,执行HelloWorldStub中带注解的方法//判断是否使用了注解的name()方法,设置name = "小明",并返回"小明 say hello world!"String returnValue = parse.parseMethod(HelloWorldStub.class);assertEquals("小明 say hello world!", returnValue) ;}}

执行的注解方法的操作类

package annotation;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class ParseAnnotationStub {//包装了下Java基本的方法反射(范围是带了我们特定注解的方法)//传入我们要执行的类型,所以我们时常发现某些框架要我们定义好类查找的范围,或前后缀什么的//可以设置返回值为空void 或者Object通用,这里我们为了测试采用String返回值public String parseMethod(Class<?> clazz) throws IllegalArgumentException,IllegalAccessException, InvocationTargetException,SecurityException, NoSuchMethodException, InstantiationException {//获得该对象Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});//变量该对象的方法for (Method method : clazz.getDeclaredMethods()) {//获取方法的注解,这里特定获取方法上@HelloWorld注解HelloWorldAnnotation say = method.getAnnotation(HelloWorldAnnotation.class);//如果@HelloWorld注解不空,即方法有@HelloWorld注解if (say != null) {//这里我们先前定义了   UseHelloWorld.sayHello(String name)方法//这里可以从注解中获取值,或者直接运行,或者缓存该对象方法String name = say.name();return (String)method.invoke(obj, name);}}return "";}}








原创粉丝点击