1. JDK要设置为1.6并添加依赖 C:/Program Files/Java/jdk1.6.0_45/lib/tools.jar ```xml <?xml version=”1.0” encoding=”UTF-8”?> <project xmlns=”http://maven.apache.org/POM/4.0.0

      1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      4.0.0

      cn.java.money apt-processor-jdk1.6

      1.0-SNAPSHOT 1.6 1.6 1.6 UTF-8 UTF-8 C:/Program Files/Java/jdk1.6.0_45/lib/tools.jar com.sun tools 1.6 system ${tools.path} true

    1. 2. 定义编译时注解
    2. ```java
    3. import java.lang.annotation.*;
    4. /**
    5. * 修饰普通成员变量的Annotation
    6. */
    7. @Documented
    8. @Target(ElementType.FIELD)
    9. @Retention(RetentionPolicy.SOURCE)
    10. public @interface Property {
    11. String column();
    12. String type();
    13. }
    1. import java.lang.annotation.*;
    2. /**
    3. * 修饰表属性
    4. */
    5. @Documented
    6. @Target(ElementType.TYPE)
    7. @Retention(RetentionPolicy.SOURCE)
    8. public @interface Persistent {
    9. String table();
    10. }
    1. import java.lang.annotation.*;
    2. /**
    3. * 修饰标识属性
    4. */
    5. @Documented
    6. @Target(ElementType.FIELD)
    7. @Retention(RetentionPolicy.SOURCE)
    8. public @interface IdProperty {
    9. String column();
    10. String type();
    11. String generator();
    12. }
    1. 定义注解处理器, 注意这里com.sun.mirror.apt在JDK6中存在,JDK7以后移除了。 ```java import com.sun.mirror.apt.AnnotationProcessor; import com.sun.mirror.apt.AnnotationProcessorEnvironment; import com.sun.mirror.declaration.FieldDeclaration; import com.sun.mirror.declaration.TypeDeclaration;

    import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream;

    public class HibernateAnnotationProcessor implements AnnotationProcessor { // Annotation处理器环境是该处理器与APT交互的重要途径 private AnnotationProcessorEnvironment env; // 构造HibernateAnnotationProcessor对象时获得处理器环境 public HibernateAnnotationProcessor(AnnotationProcessorEnvironment env) { this.env = env; }

    1. // 循环处理每个对象
    2. public void process() {
    3. // 遍历每个class文件
    4. for (TypeDeclaration t : env.getSpecifiedTypeDeclarations()) {
    5. // 定义一个文件输出流用于生成额外的文件
    6. FileOutputStream fos = null;
    7. // 获取正在处理的类名
    8. String clazzName = t.getSimpleName();
    9. // 获取类定义前的Persistent Annotation
    10. Persistent per = t.getAnnotation(Persistent.class);
    11. //当per Annotation不为空时才继续处理
    12. if (per != null) {
    13. try {
    14. // 创建文件输出流
    15. fos = new FileOutputStream(clazzName + ".hbm.xml");
    16. PrintStream ps = new PrintStream(fos);
    17. // 执行输出
    18. ps.println("<?xml version=\"1.0\"?>");
    19. ps.println("<!DOCTYPE hibernate-mapping");
    20. ps.println(" PUBLIC \"-// Hibernate/Hibernate Mapping DTD 3.0//EN\"");
    21. ps.println(" \"http:// hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">");
    22. ps.println("<hibernate-mapping>");
    23. ps.print(" <class name=\"" + t);
    24. // 输出per的table()的值
    25. ps.println("\" table=\"" + per.table() + "\">");
    26. for (FieldDeclaration f : t.getFields()) {
    27. // 获取指定FieldDeclaration前面的IdProperty Annotation
    28. IdProperty id = f.getAnnotation(IdProperty.class);
    29. // 如果id Annotation不为空
    30. if (id != null) {
    31. // 执行输出
    32. ps.println(" <id name=\"" + f.getSimpleName() + "\" column=\"" + id.column() + "\" type=\"" + id.type() + "\">");
    33. ps.println(" <generator class=\"" + id.generator() + "\"/>");
    34. ps.println(" </id>");
    35. }
    36. // 获取指定FieldDeclaration前面的Property Annotation
    37. Property p = f.getAnnotation(Property.class);
    38. // 如果p Annotation不为空
    39. if (p != null) {
    40. // 执行输出
    41. ps.println(" <property name=\"" + f.getSimpleName() + "\" column=\"" + p.column() + "\"type=\"" + p.type() + "\"/>");
    42. }
    43. }
    44. ps.println(" </class>");
    45. ps.println("</hibernate-mapping>");
    46. } catch (Exception e) {
    47. e.printStackTrace();
    48. } finally { // 关闭输出流
    49. try {
    50. if (fos != null) {
    51. fos.close();
    52. }
    53. } catch (IOException ex) {
    54. ex.printStackTrace();
    55. }
    56. }
    57. }
    58. }
    59. }

    }

    1. 4. 注解处理器工厂,支持哪些注解
    2. ```java
    3. import com.sun.mirror.apt.AnnotationProcessor;
    4. import com.sun.mirror.apt.AnnotationProcessorEnvironment;
    5. import com.sun.mirror.apt.AnnotationProcessorFactory;
    6. import com.sun.mirror.declaration.AnnotationTypeDeclaration;
    7. import java.util.Arrays;
    8. import java.util.Collection;
    9. import java.util.Set;
    10. public class HibernateAnnotationFactory implements AnnotationProcessorFactory {
    11. // 所有支持的注释类型
    12. public Collection<String> supportedAnnotationTypes() {
    13. return Arrays.asList("Property", "IdProperty", "Persistent");
    14. }
    15. // 返回所有支持的选项
    16. public Collection<String> supportedOptions() {
    17. return Arrays.asList(new String[0]);
    18. }
    19. // 返回Annotation处理器
    20. public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) {
    21. return new HibernateAnnotationProcessor(env);
    22. }
    23. }
    1. 设置cmd窗口临时path 为JDK1.6

      1. set path=C:\Program Files\Java\jdk1.6.0_45\bin
    2. 编译注解处理器和注解

      1. D:\devin\code\apt-processor-jdk1.6\src\main\java>javac -encoding utf-8
      2. -classpath "C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar" *.java
    3. apt 命令编译Person.java

      1. D:\devin\code\apt-processor-jdk1.6\src\main\java>
      2. apt -factory HibernateAnnotationFactory Person.java

      image.png
      Gitee: https://gitee.com/java-base-devin/apt-processor-jdk1.6.git