samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 01 Jun 2010 12:08:47 +0200
changeset 355 d1e7424dc988
child 357 837370f791ba
permissions -rw-r--r--
Example of Annotation Processor that generates a db access class
jtulach@355
     1
/*
jtulach@355
     2
 * To change this template, choose Tools | Templates
jtulach@355
     3
 * and open the template in the editor.
jtulach@355
     4
 */
jtulach@355
     5
jtulach@355
     6
package org.apidesign.livedb.impl;
jtulach@355
     7
jtulach@355
     8
import java.io.IOException;
jtulach@355
     9
import java.io.Writer;
jtulach@355
    10
import java.util.Set;
jtulach@355
    11
import javax.annotation.processing.AbstractProcessor;
jtulach@355
    12
import javax.annotation.processing.Processor;
jtulach@355
    13
import javax.annotation.processing.RoundEnvironment;
jtulach@355
    14
import javax.annotation.processing.SupportedAnnotationTypes;
jtulach@355
    15
import javax.annotation.processing.SupportedSourceVersion;
jtulach@355
    16
import javax.lang.model.SourceVersion;
jtulach@355
    17
import javax.lang.model.element.Element;
jtulach@355
    18
import javax.lang.model.element.PackageElement;
jtulach@355
    19
import javax.lang.model.element.TypeElement;
jtulach@355
    20
import javax.tools.JavaFileObject;
jtulach@355
    21
import org.apidesign.livedb.LiveDB;
jtulach@355
    22
import org.openide.util.lookup.ServiceProvider;
jtulach@355
    23
jtulach@355
    24
/**
jtulach@355
    25
 *
jtulach@355
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@355
    27
 */
jtulach@355
    28
@SupportedAnnotationTypes("org.apidesign.livedb.LiveDB")
jtulach@355
    29
@SupportedSourceVersion(SourceVersion.RELEASE_6)
jtulach@355
    30
@ServiceProvider(service=Processor.class)
jtulach@355
    31
public class LiveDBProcessor extends AbstractProcessor {
jtulach@355
    32
    @Override
jtulach@355
    33
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jtulach@355
    34
        for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) {
jtulach@355
    35
            LiveDB db = e.getAnnotation(LiveDB.class);
jtulach@355
    36
            PackageElement pe = (PackageElement)e;
jtulach@355
    37
            String clsName = pe.getQualifiedName().toString() + "." + db.classname();
jtulach@355
    38
            try {
jtulach@355
    39
                JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
jtulach@355
    40
                Writer w = src.openWriter();
jtulach@355
    41
                w.append("package " + pe.getQualifiedName() + ";\n");
jtulach@355
    42
                w.append("class " + db.classname() + " {\n");
jtulach@355
    43
                w.append("  public String " + db.field() + ";\n");
jtulach@355
    44
                w.append("}");
jtulach@355
    45
                w.close();
jtulach@355
    46
            } catch (IOException ex) {
jtulach@355
    47
                throw new IllegalStateException(ex);
jtulach@355
    48
            }
jtulach@355
    49
        }
jtulach@355
    50
        return true;
jtulach@355
    51
    }
jtulach@355
    52
jtulach@355
    53
}