编译时处理Annotation 的一份代码

Posted woshi123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编译时处理Annotation 的一份代码相关的知识,希望对你有一定的参考价值。

package com.woshi.test;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Persistent {
    String table();
}
package com.woshi.test;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Id {
    String column();
    String type();
    String generator();
}
package com.woshi.test;

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Property {
    String column();
    String type();
}
package com.woshi.test;

@Persistent(table = "person_inf")
public 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;

    public Person(){}


    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package com.woshi.test;

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;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Set;

@SupportedSourceVersion(SourceVersion.RELEASE_11)
@SupportedAnnotationTypes({"Persistent","Id","Property"})
public class HibernateAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        PrintStream printStream = null;
        try {
            for (Element element:roundEnvironment.getElementsAnnotatedWith(Persistent.class)){
                Name clazzName = element.getSimpleName();
                Persistent persistent = element.getAnnotation(Persistent.class);
                printStream = new PrintStream(new FileOutputStream(clazzName+".hbm.xml"));
                printStream.println("<?xml version="1.0"?>");
                printStream.println("<!DOCTYPE hibernate-mapping PUBLIC");
                printStream.println("  "-Hibernate/Hibernate Mapping DTD 3.0//EN"");
                printStream.println(" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">");
                printStream.println(" <hibernate-mapping>");
                printStream.print("<class Name="" + element);
                printStream.println("" table=""+persistent.table()+"">");
                for (Element f:element.getEnclosedElements()){
                    if (f.getKind() == ElementKind.FIELD){
                        Id id = f.getAnnotation(Id.class);
                        if (id != null){
                            printStream.println("<id name=""+f.getSimpleName()+"" column=""+id.column()+"" type=""+id.type()+"">");
                            printStream.println("   <generator class=""+id.generator()+""/>");
                            printStream.println("            </id>");
                        }
                        Property property = f.getAnnotation(Property.class);
                        if (property != null){
                            printStream.println("   <property name=""+f.getSimpleName()+"" column=""+property.column()+"" type=""+property.type()+""/>");
                        }
                    }
                }
                printStream.println("   </class>");
                printStream.println("</hibernate-mapping>");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            if (printStream != null){
                try{
                    printStream.close();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }
        return true;
    }

}

以上是关于编译时处理Annotation 的一份代码的主要内容,如果未能解决你的问题,请参考以下文章

Java Annotation使用详解

Java注解

关于annotation的认识

java annotation

Annotation

仿照 ButterKnife 的 Android 注解实例