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“
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
2. 定义编译时注解
```java
import java.lang.annotation.*;
/**
* 修饰普通成员变量的Annotation
*/
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Property {
String column();
String type();
}
import java.lang.annotation.*;
/**
* 修饰表属性
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Persistent {
String table();
}
import java.lang.annotation.*;
/**
* 修饰标识属性
*/
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface IdProperty {
String column();
String type();
String generator();
}
- 定义注解处理器, 注意这里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; }
// 循环处理每个对象
public void process() {
// 遍历每个class文件
for (TypeDeclaration t : env.getSpecifiedTypeDeclarations()) {
// 定义一个文件输出流用于生成额外的文件
FileOutputStream fos = null;
// 获取正在处理的类名
String clazzName = t.getSimpleName();
// 获取类定义前的Persistent Annotation
Persistent per = t.getAnnotation(Persistent.class);
//当per Annotation不为空时才继续处理
if (per != null) {
try {
// 创建文件输出流
fos = new FileOutputStream(clazzName + ".hbm.xml");
PrintStream ps = new PrintStream(fos);
// 执行输出
ps.println("<?xml version=\"1.0\"?>");
ps.println("<!DOCTYPE hibernate-mapping");
ps.println(" PUBLIC \"-// Hibernate/Hibernate Mapping DTD 3.0//EN\"");
ps.println(" \"http:// hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">");
ps.println("<hibernate-mapping>");
ps.print(" <class name=\"" + t);
// 输出per的table()的值
ps.println("\" table=\"" + per.table() + "\">");
for (FieldDeclaration f : t.getFields()) {
// 获取指定FieldDeclaration前面的IdProperty Annotation
IdProperty id = f.getAnnotation(IdProperty.class);
// 如果id Annotation不为空
if (id != null) {
// 执行输出
ps.println(" <id name=\"" + f.getSimpleName() + "\" column=\"" + id.column() + "\" type=\"" + id.type() + "\">");
ps.println(" <generator class=\"" + id.generator() + "\"/>");
ps.println(" </id>");
}
// 获取指定FieldDeclaration前面的Property Annotation
Property p = f.getAnnotation(Property.class);
// 如果p Annotation不为空
if (p != null) {
// 执行输出
ps.println(" <property name=\"" + f.getSimpleName() + "\" column=\"" + p.column() + "\"type=\"" + p.type() + "\"/>");
}
}
ps.println(" </class>");
ps.println("</hibernate-mapping>");
} catch (Exception e) {
e.printStackTrace();
} finally { // 关闭输出流
try {
if (fos != null) {
fos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
}
4. 注解处理器工厂,支持哪些注解
```java
import com.sun.mirror.apt.AnnotationProcessor;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.apt.AnnotationProcessorFactory;
import com.sun.mirror.declaration.AnnotationTypeDeclaration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
public class HibernateAnnotationFactory implements AnnotationProcessorFactory {
// 所有支持的注释类型
public Collection<String> supportedAnnotationTypes() {
return Arrays.asList("Property", "IdProperty", "Persistent");
}
// 返回所有支持的选项
public Collection<String> supportedOptions() {
return Arrays.asList(new String[0]);
}
// 返回Annotation处理器
public AnnotationProcessor getProcessorFor( Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) {
return new HibernateAnnotationProcessor(env);
}
}
设置cmd窗口临时path 为JDK1.6
set path=C:\Program Files\Java\jdk1.6.0_45\bin
编译注解处理器和注解
D:\devin\code\apt-processor-jdk1.6\src\main\java>javac -encoding utf-8
-classpath "C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar" *.java
apt 命令编译Person.java
D:\devin\code\apt-processor-jdk1.6\src\main\java>
apt -factory HibernateAnnotationFactory Person.java
Gitee: https://gitee.com/java-base-devin/apt-processor-jdk1.6.git