---
title: "Java注解（自定义方法注解使用及测试）"
author: "txidol"
date: "2013-05-11T15:00:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2013-05-19T09:10:55Z"
id: 1871648
url: "https://txidol.iteye.com/blog/1871648"
---

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

假设我们设计一个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 {

	@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 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 "";
	}
}
```