首页 > 其他 > 详细

注解测试类

时间:2015-12-31 10:32:20      阅读:237      评论:0      收藏:0      [点我收藏+]

package demo.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)
@Target(ElementType.METHOD)
@Documented
public @interface Author {
String name();
String group();
}

package demo.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)
@Target(ElementType.TYPE)
@Documented
public @interface Description {
String value();
}

package demo.annotation;

import java.lang.reflect.Method;

@Description(value="这是一个有用的工具类")
public class Utility {

@Author(name="haoran_202",group="com.magc")
public String work(){
return "work over";
}
public static void main(String[] args) {
try {
Class rt_class=Class.forName("demo.annotation.Utility");
Method[] methods = rt_class.getMethods();
boolean flag=rt_class.isAnnotationPresent(Description.class);
if(flag){
Description description=(Description) rt_class.getAnnotation(Description.class);
System.out.println("description value:"+description.value());
for(Method method:methods){
if(method.isAnnotationPresent(Author.class)){
Author author=(Author)method.getAnnotation(Author.class);
System.out.println("author name:"+author.name()+",group:"+author.group());
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

注解测试类

原文:http://www.cnblogs.com/likeju/p/5090750.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!