package annotation.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 定义注解 Test <br>
* 为方便测试:注解目标为类 方法,属性及构造方法<br>
* 注解中含有三个元素 id ,name和 gid; <br>
* id 元素 有默认值 0 <br>
*/
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
//有四个元注解,一般这两个 就可以搞定了
public @interface TestA {
String name() default ""; //不写的话默认为空,不报错
int id() default ;
Class<Long> gid();
}
 1 package annotation.test;
2
3 import java.util.HashMap;
4 import java.util.Map;
7 /**
8 * 这个类专门用来测试注解使用
9 */
10
11 @TestA(name="type",gid=Long.class)
12 // 使用了类注解
13 public class UserAnnotation {
16 @TestA(name="param",id=1,gid=Long.class) // 使用了类成员注解
17 private Integer age;
18
19 @TestA(name="construct",id=2,gid=Long.class)// 使用了构造方法注解
20 public UserAnnotation() {
22 }
23
24 @TestA(name="public method", id=3, gid=Long.class)// 使用了 public 方法注解
25 public void a() {
26 Map<String, String> m = new HashMap<String, String>(0);
27 }
28
29 @TestA(name="protected method", id=4, gid=Long.class)//protected 方法注解
30 protected void b() {
31 Map<String, String> m = new HashMap<String, String>(0);
32 }
33
34 @TestA(name="private method " , id = 5, gid=Long.class) // private 方法注解
35 private void c(){
36 Map<String, String> m = new HashMap<String, String>(0);
37 }
38
39 public void b(Integer a){
41 }
42 }
package annotation.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method; public class ParseAnnotation { /**
* 简单打印出UserAnnotation 类中所使用到的类注解
* 该方法只打印了 Type 类型的注解
* @throws ClassNotFoundException
*/
public static void parseTypeAnnotation() throws ClassNotFoundException{
Class clazz = Class.forName("annotation.test.UserAnnotation");
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
TestA testA = (TestA) annotation;
System.out.println("type name = "+clazz.getName() + " | id = " + testA.id() + " | name = " + testA.name() + " |                     gid = " + testA.gid());
}
} /**
* 简单打印出UserAnnotation 类中所使用到的方法注解
* 该方法只打印了 Method 类型的注解
* @throws ClassNotFoundException
*/
public static void parseMethodAnnotation() throws ClassNotFoundException{
Method[] methods = UserAnnotation.class.getDeclaredMethods();
for (Method method : methods) {
/*
* 判断方法中是否有指定注解类型的注解
*/
boolean hasAnnotation = method.isAnnotationPresent(TestA.class);
if(hasAnnotation){
TestA annotation = method.getAnnotation(TestA.class);
System.out.println("method name = " + method.getName() + " | id = " +
annotation.id() + " | description = " + annotation.name() + " | gid = " + annotation.gid());
}
}
} /**
* 简单打印出UserAnnotation 类中所使用到的构造方法注解
* 该方法只打印了 构造方法 类型的注解
* @throws ClassNotFoundException
*/
public static void parseConstructAnnotation() throws ClassNotFoundException{
Constructor[] constructors = UserAnnotation.class.getConstructors();
for (Constructor constructor : constructors) {
/*
* 判断构造方法中是否有指定注解类型的注解
*/
boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);
if(hasAnnotation){
/*
* 根据注解类型返回方法的指定类型注解
*/
TestA annotation = (TestA) constructor.getAnnotation(TestA.class);
System.out.println("constructor = " + constructor.getName()
+ " | id = " + annotation.id() + " | description = "
+ annotation.name() + " | gid= "+annotation.gid());
}
}
} /**
* 简单打印出UserAnnotation 类中所使用到的字段注解
* 该方法只打印了 Method 类型的注解
* @throws ClassNotFoundException
*/
public static void parseFieldAnnotation() throws ClassNotFoundException{
Field[] fields = UserAnnotation.class.getDeclaredFields();
for (Field field : fields) {
boolean hasAnnotation = field.isAnnotationPresent(TestA.class);
if(hasAnnotation){
TestA annotation = field.getAnnotation(TestA.class);
System.out.println("Field = " + field.getName()
+ " | id = " + annotation.id() + " | description = "
+ annotation.name() + " | gid= "+annotation.gid());
}
}
} public static void main(String[] args) throws ClassNotFoundException {
System.out.println("------------------------------解析Type注解----------------------------------------------------------");
parseTypeAnnotation();
System.out.println("------------------------------解析Method注解-------------------------------------------------------");
parseMethodAnnotation();
System.out.println("------------------------------解析构造方法(Construct)注解------------------------------------------");
parseConstructAnnotation();
System.out.println("------------------------------解析字段(Field)注解-----------------------------------------------------");
parseFieldAnnotation();
}
}

Annotation(jdk5.0注解)复习(转自http://3w_cnblogs_com/pepcod/)的更多相关文章

  1. Spring4.0之四:Meta Annotation(元注解)

    Spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持.注解为依赖注入,AOP(如事务)提供了更强大和简便的方式.这也导致你要是用一个相同的注解到许多不同的类中去.这篇文章介 ...

  2. AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.

    把Androidstudio2.2的项目放到3.0里面去了,然后开始报错了. 体验最新版AndroidStudio3.0 Canary 8的时候,发现之前项目的butter knife报错,用到注解的 ...

  3. JDK 5.0 注解知识快速进阶

    1.了解注解 对于Java开发人员来说,在编写代码时,除了源程序外,还会使用Javadoc标签对类.方法或成员变量进行注释,一遍使用Javadoc工具生成和源代码配套的Javadoc文件,如@para ...

  4. Day07 jdk5.0新特性&Junit&反射

    day07总结 今日内容 MyEclipse安装与使用 JUnit使用 泛型 1.5新特性 自动装箱拆箱 增强for 静态导入 可变参数方法 枚举 反射 MyEclipse安装与使用(yes) 安装M ...

  5. [2]注解(Annotation)-- 深入理解Java:注解(Annotation)自定义注解入门

    转载 http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html 深入理解Java:注解(Annotation)自定义注解入门 要深入学习 ...

  6. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

  7. JDK5.0新特性 (Day_07)

      JDK5.0新特性   目录 静态导入 自动装箱/拆箱 for-each循环 可变参数 枚举 JDK 5.0 新特性简介 JDK 5.0 的一个重要主题就是通过新增一些特性来简化开发,这些特性包括 ...

  8. JDK5.0新特性1

    目录 静态导入 自动装箱/拆箱 for-each循环 可变参数 枚举 JDK 5.0 新特性简介 JDK 5.0 的一个重要主题就是通过新增一些特性来简化开发,这些特性包括: 静态导入 自动装箱/拆箱 ...

  9. Java基础和JDK5.0新特性

    Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...

随机推荐

  1. Java IO(二)

    字节流 字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream B ...

  2. iOS开发--appstore应用上架

    除了企业级的应用,一般一个应用开发完成后,都会上架App Store.其实上架流程并不繁琐,麻烦的是要耗时等待审核,如果被拒,修改后又需要等待.被拒的原因很多(真的很多…),比如程序有崩溃,适配没做好 ...

  3. VS编译的QT程序发布时产生的AppCrash问题

    至少我碰到了三个情况,都是AppCrash错误(以下都指VS2008的Release的设置) 第1个错误,报错模块是程序自己 我使用VS2008 Team with SP1和QT4.86编译程序,一直 ...

  4. 指定IE浏览器渲染方式

    <meta http-equiv="X-UA-Compatible" content="IE=7" />以上代码告诉IE浏览器,无论是否用DTD声明 ...

  5. hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据

    参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...

  6. HDU 4638 Group 树状数组 + 思路

    实际上就是问这个区间编号连续的段的个数,假如一个编号连续的段有(a+b)个人,我把他们分在同一组能得到的分值为(a+b)^2,而把他们分成人数为a和b的两组的话,得到的分值就是a^2+b^2,显然(a ...

  7. psycopg2.pool – Connections pooling / psycopg2.pool – 连接池 / postgresql 连接池

    创建新的PostgreSQL连接可以是一个昂贵的操作.这个模块提供了一些纯Python类直接在客户端应用程序实现简单的连接池.      class psycopg2.pool.AbstractCon ...

  8. Kobject结构体分析

    kobject是组成设备device.驱动driver.总线bus.class的基本结构.如果把前者看成基类,则后者均为它的派生产物.device.driver.bus.class构成了设备模型,而k ...

  9. [CFgym101061C]Ramzi(贪心,双条件最短路)

    题目链接:http://codeforces.com/gym/101061/problem/C 题意:一张图,图上的边有两种,一种是车道,一种是人行道.一个人要从A点到B点,可以坐车也可以走人行道.这 ...

  10. 我的ipad应用备份

    听歌: 网易云音乐,QQ音乐 摄影: CameraConnect,Replay,PhotoBooth,PhotoCollage,画中画相机,Creative Shape, 简拼,玩图,PicsArt, ...