View Javadoc
1   package at.rseiler.spbee.core.generator;
2   
3   import at.rseiler.spbee.core.util.StringCodeWriter;
4   import com.sun.codemodel.JCodeModel;
5   
6   import javax.annotation.processing.ProcessingEnvironment;
7   import javax.tools.JavaFileObject;
8   import java.io.IOException;
9   import java.io.Writer;
10  
11  /**
12   * Base class for the code generators.
13   *
14   * @author Reinhard Seiler {@literal <rseiler.developer@gmail.com>}
15   */
16  public abstract class AbstractGenerator {
17  
18      private final ProcessingEnvironment processingEnv;
19  
20      public AbstractGenerator(ProcessingEnvironment processingEnv) {
21          this.processingEnv = processingEnv;
22      }
23  
24      /**
25       * Generates the Java class based on the parameters.
26       *
27       * @param model              the code of the class
28       * @param qualifiedClassName the name of the class
29       * @throws IOException if the the Java class couldn't be created
30       */
31      public void generateClass(JCodeModel model, String qualifiedClassName) throws IOException {
32          StringCodeWriter codeWriter = new StringCodeWriter();
33          model.build(codeWriter);
34          String javaCode = codeWriter.getJavaCode();
35  
36          JavaFileObject f = processingEnv.getFiler().createSourceFile(qualifiedClassName);
37          Writer w = f.openWriter();
38          w.write(javaCode);
39          w.close();
40      }
41  
42  }