目标,创建一个方法注解,我们能够初始化时执行该方法,并可以给该方法传入注解的参数值
假设我们设计一个 sayHello(String name) 方法,给该方法做个注解,初始时使用注解传入"小明"
[java]
package annotation;
public class HelloWorldStub {
@HelloWorldAnnotation(name = "小明")
public String sayHello(String name) {
if (name == null ) {
name = "";
}
return name + " say hello world!";
}
}
package annotation;
public class HelloWorldStub {
@HelloWorldAnnotation(name = "小明")
public String sayHello(String name) {
if (name == null ) {
name = "";
}
return name + " say hello world!";
}
}
定义注解
[java]
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.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 "";
}
我们的测试类
[java]
package annotation;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestHelloWorldAnnotation {
@Test
public 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 org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestHelloWorldAnnotation {
@Test
public 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) ;
}
}
执行的注解方法的操作类
[java]
package annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ParseAnnotationStub {
//包装了下Java基本的方法反射(范围是带了我们特定注解的方法)
//传入我们要执行的类型,所以我们时常发现某些框架要我们定义好类查找的范围,或前后缀什么的
//可以设置返回值为空v