APT处理annotation

来源:互联网 发布:mac版本阿里旺旺 编辑:程序博客网 时间:2024/06/05 00:27

APT是一种java提供的注释处理工具,他能对annotation做额外的处理。

我们就用hibernate的annotation来做例子,eg:(代码如下,我就不多说了)

[java] view plaincopy
  1. @Documented  
  2. @Target(ElementType.FIELD)  
  3. @Retention(RetentionPolicy.SOURCE)  
  4. public @interface IdProperty  
  5. {  
  6.     String column();  
  7.     String type();  
  8.     String generator();  
  9. }  

[java] view plaincopy
  1. @Documented  
  2. @Target(ElementType.TYPE)  
  3. @Retention(RetentionPolicy.SOURCE)  
  4. public @interface Persistent  
  5. {  
  6.     String table();  
  7. }  

[java] view plaincopy
  1. @Documented  
  2. @Target(ElementType.FIELD)  
  3. @Retention(RetentionPolicy.SOURCE)  
  4. public @interface Property  
  5. {  
  6.     String column();  
  7.     String type();  
  8. }  

以上是三个Annotation类型

[java] view plaincopy
  1. @Persistent(table="persons_table")  
  2. public class Person  
  3. {  
  4.     @IdProperty(column="person_id",type="integer",generator="identity")  
  5.     private int id;  
  6.     @Property(column="person_name",type="string")  
  7.     private String name;  
  8.     @Property(column="person_age",type="integer")  
  9.     private int age;  
  10.   
  11.     public Person()  
  12.     {  
  13.     }  
  14.   
  15.     public Person(int id , String name , int age)  
  16.     {  
  17.         this.id = id;  
  18.         this.name = name;  
  19.         this.age = age;  
  20.     }  
  21.   
  22.     public void setId(int id)  
  23.     {  
  24.         this.id = id;  
  25.     }  
  26.     public int getId()  
  27.     {  
  28.          return this.id;  
  29.     }  
  30.     public void setName(String name)  
  31.     {  
  32.         this.name = name;  
  33.     }  
  34.     public String getName()  
  35.     {  
  36.          return this.name;  
  37.     }  
  38.   
  39.     public void setAge(int age)  
  40.     {  
  41.         this.age = age;  
  42.     }  
  43.     public int getAge()  
  44.     {  
  45.          return this.age;  
  46.     }  
  47.   
  48. }  

以上是要处理的类

[java] view plaincopy
  1. public class HibernateAnnotationProcessor implements AnnotationProcessor   
  2. {  
  3.     //Annotation处理器环境,是该处理器与APT交互的重要途径  
  4.     private AnnotationProcessorEnvironment env;  
  5.     //构造HibernateAnnotationProcessor对象时,获得处理器环境  
  6.     public HibernateAnnotationProcessor(AnnotationProcessorEnvironment env)   
  7.     {  
  8.         this.env = env;  
  9.     }  
  10.     //循环处理每个对象  
  11.     public void process()  
  12.     {  
  13.         //遍历每个class文件  
  14.         for (TypeDeclaration t : env.getSpecifiedTypeDeclarations())  
  15.         {  
  16.             //定义一个文件输出流,用于生成额外的文件  
  17.             FileOutputStream fos = null;  
  18.             //获取正在处理的类名  
  19.             String clazzName = t.getSimpleName();  
  20.             //获取类定义前的Persistent Annotation  
  21.             Persistent per = t.getAnnotation(Persistent.class);  
  22.             //当per Annotation不为空时才继续处理  
  23.             if(per != null)  
  24.             {  
  25.                 try  
  26.                 {  
  27.                     //创建文件输出流  
  28.                     fos = new FileOutputStream(clazzName + ".hbm.xml");  
  29.                     PrintStream ps = new PrintStream(fos);  
  30.                     //执行输出  
  31.                     ps.println("<?xml version=/"1.0/"?>");  
  32.                     ps.println("<!DOCTYPE hibernate-mapping");  
  33.                     ps.println("    PUBLIC /"-//Hibernate/Hibernate Mapping DTD 3.0//EN/"");  
  34.                     ps.println("    /"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd/">");  
  35.                     ps.println("<hibernate-mapping>");  
  36.                     ps.print("  <class name=/"" + t);  
  37.                     //输出per的table()的值  
  38.                     ps.println("/" table=/"" + per.table() + "/">");  
  39.                     for (FieldDeclaration f : t.getFields())  
  40.                     {  
  41.                         //获取指定FieldDeclaration前面的IdProperty Annotation  
  42.                         IdProperty id = f.getAnnotation(IdProperty.class);  
  43.                         //如果id Annotation不为空  
  44.                         if (id != null)  
  45.                         {  
  46.                             //执行输出  
  47.                             ps.println("        <id name=/""  
  48.                                 + f.getSimpleName()   
  49.                                 + "/" column=/"" + id.column()  
  50.                                 + "/" type=/"" + id.type()  
  51.                                 + "/">");  
  52.                             ps.println("            <generator class=/""   
  53.                                 + id.generator() + "/"/>");  
  54.                             ps.println("        </id>");  
  55.                         }  
  56.                         //获取指定FieldDeclaration前面的Property Annotation  
  57.                         Property p = f.getAnnotation(Property.class);  
  58.                         //如果p Annotation不为空  
  59.                         if (p != null)  
  60.                         {  
  61.                             //执行输出  
  62.                             ps.println("        <property name=/""   
  63.                                 + f.getSimpleName()   
  64.                                 + "/" column=/"" + p.column()   
  65.                                 + "/" type=/"" + p.type()  
  66.                                 + "/"/>");         
  67.                         }  
  68.                     }  
  69.                     ps.println("    </class>");  
  70.                     ps.println("</hibernate-mapping>");  
  71.                 }  
  72.                 catch (Exception e)  
  73.                 {  
  74.                     e.printStackTrace();  
  75.                 }  
  76.                 finally  
  77.                 {  
  78.                     //关闭输出流  
  79.                     try  
  80.                     {  
  81.                         if (fos != null)  
  82.                         {  
  83.                             fos.close();  
  84.                         }  
  85.                     }  
  86.                     catch (IOException ex)  
  87.                     {  
  88.                         ex.printStackTrace();  
  89.                     }  
  90.                 }  
  91.             }  
  92.         }  
  93.     }  
  94. }  

[java] view plaincopy
  1. public class HibernateAnnotationFactory implements AnnotationProcessorFactory   
  2. {  
  3.     //所有支持的注释类型  
  4.     public Collection<String> supportedAnnotationTypes()   
  5.     {  
  6.         return Arrays.asList("Property" , "IdProperty" , "Persistent");  
  7.     }  
  8.     //返回所有支持的选项  
  9.     public Collection<String> supportedOptions()   
  10.     {  
  11.         return Arrays.asList(new String[0]);  
  12.     }  
  13.     //返回Annotation处理器  
  14.     public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,  
  15.         AnnotationProcessorEnvironment env)   
  16.     {  
  17.         return new HibernateAnnotationProcessor(env);  
  18.     }     
  19. }  

处理机和工厂

最后用apt命令:apt -factory HibernateAnnotationFactory Person.java

这样就生成了hibernate的.hbm.xml文件

[xhtml] view plaincopy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping  
  3.     PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping>  
  6.     <class name="Person" table="persons_table">  
  7.         <id name="id" column="person_id" type="integer">  
  8.             <generator class="identity"/>  
  9.         </id>  
  10.         <property name="name" column="person_name" type="string"/>  
  11.         <property name="age" column="person_age" type="integer"/>  
  12.     </class>  
  13. </hibernate-mapping>  

0 0
原创粉丝点击