代码实现了:你给一个javabean类加上Annotation注释,可以将其转化为*.hbm.xml的格式打印出来,如果想转化为文件的形式也不难,用流就行了。
主要用到了自定义Annotation。
Persistent.java
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;
@Target(ElementType.TYPE) //表示这个Annotation可以修饰类,接口或者枚举定义
@Retention(RetentionPolicy.SOURCE) //表示只是在源码中保留,不可在运行期间使用到
@Documented //指定被该元素修饰的Annotation类将被 javadoc 工具提取成文档,若是定义Annotation类时使用到了@Documented修饰,则
//所有使用该Annotation的程序的API文档中将含该Annotation说明
public @interface Persistent
{
String table();
}
Id.java
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;
@Target(ElementType.FIELD) //表示这个Annotation可以修饰成员变量
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Id
{
String generator();
String type();
String column();
}
Property.java
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;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Property
{
String column();
String type();
}
Person.java // 这就相当于javabean,不过没有写get \ set 方法,这不是重点
@Persistent(table="person_table")
class Person
{
@Id(column="person_id",type="integer" ,generator="identity")
private int id;
@Property(column="person_name" ,type="string")
private String name;
@Property(column="person_age" ,type="integer")
private int age;
}
HibernateAnnotationProcessor.java
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes({"Persistent","Id","Property"})
public class HibernateAnnotationProcessor extends AbstractProcessor
{
public boolean process(Set<? extends TypeElement>annotations,RoundEnvironment roundEnv)
{
//遍历所有被 @Persistent 修饰的类
for(Element e:roundEnv.getElementsAnnotatedWith(Persistent.class))
{
Name clazzName=e.getSimpleName();
Persistent per = e.getAnnotation(Persistent.class);
System.out.println("正在处理"+clazzName);
System.out.println("<class name="+e+" table="+per.table()+">");
//遍历所有类中Field区域
for(Element fi : e.getEnclosedElements())
{
//只处理Field上的Annotation
if(fi.getKind() == ElementKind.FIELD)
{
//获取当前Field上的@Id Annotation
Id id = fi.getAnnotation(Id.class);
if(id!=null)
{
System.out.println("<id name="+fi.getSimpleName()+" column="+id.column()+" type="+id.type()+">");
System.out.println("<generator class="+id.generator()+"/>");
System.out.println("</id>");
}
//获取Field定义上的 @Property Annotation
Property p=fi.getAnnotation(Property.class);
if(p!=null)
{
System.out.println("<property name="+fi.getSimpleName()+" column="+p.column()+" type="+p.type()+">");
}
}
}
System.out.println("</class>");
}
return true;
}
}
先编译 HibernateAnnotationProcessor.java
然后 javac -processor HibernateAnnotationProcessor Person.java