Shorter lines and less scary code of the processor
authorJaroslav Tulach <jtulach@netbeans.org>
Fri, 30 Jul 2010 14:13:25 +0200
changeset 363cbeeeb6ae9c6
parent 362 de139a9cfb36
child 364 088d9d560bda
Shorter lines and less scary code of the processor
samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java
     1.1 --- a/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Thu Jul 29 17:55:04 2010 +0200
     1.2 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java	Fri Jul 30 14:13:25 2010 +0200
     1.3 @@ -12,6 +12,7 @@
     1.4  import java.util.ServiceLoader;
     1.5  import java.util.Set;
     1.6  import javax.annotation.processing.AbstractProcessor;
     1.7 +import javax.annotation.processing.Filer;
     1.8  import javax.annotation.processing.Processor;
     1.9  import javax.annotation.processing.RoundEnvironment;
    1.10  import javax.annotation.processing.SupportedAnnotationTypes;
    1.11 @@ -34,60 +35,30 @@
    1.12  @ServiceProvider(service=Processor.class)
    1.13  public final class LiveDBProcessor extends AbstractProcessor {
    1.14      @Override
    1.15 -    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.16 +    public boolean process(
    1.17 +        Set<? extends TypeElement> annotations, RoundEnvironment roundEnv
    1.18 +    ) {
    1.19 +        final Filer filer = processingEnv.getFiler();
    1.20          for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) {
    1.21              LiveDB db = e.getAnnotation(LiveDB.class);
    1.22              PackageElement pe = (PackageElement)e;
    1.23 -            String clsName = pe.getQualifiedName().toString() + "." + db.classname();
    1.24 +            String clsName = pe.getQualifiedName() + "." + db.classname();
    1.25              try {
    1.26 -                JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe);
    1.27 +                JavaFileObject src = filer.createSourceFile(clsName, pe);
    1.28                  Writer w = src.openWriter();
    1.29 -                Connection c = getConnection(db.url(), db.user(), db.password());
    1.30 +                Connection c = getConnection(
    1.31 +                    db.url(), db.user(), db.password()
    1.32 +                );
    1.33                  CallableStatement s = c.prepareCall(db.query());
    1.34                  ResultSet rs = s.executeQuery();
    1.35                  ResultSetMetaData md = rs.getMetaData();
    1.36 -                w.append("package " + pe.getQualifiedName() + ";\n");
    1.37 -                w.append("import java.util.List;\n");
    1.38 -                w.append("import java.util.ArrayList;\n");
    1.39 -                w.append("import java.sql.*;\n");
    1.40 +                generateClassHeader(w, pe, db);
    1.41                  w.append("class " + db.classname() + " {\n");
    1.42                  for (int i = 1; i <= md.getColumnCount(); i++) {
    1.43 -                    w.append("  public final " + md.getColumnClassName(i) + " " + md.getColumnName(i) + ";\n");
    1.44 +                    generateField(w, md, i);
    1.45                  }
    1.46 -                w.append("  private " + db.classname() + "(\n");
    1.47 -                for (int i = 1; i <= md.getColumnCount(); i++) {
    1.48 -                    w.append("    " + md.getColumnClassName(i) + " " + md.getColumnName(i));
    1.49 -                    if (i < md.getColumnCount()) {
    1.50 -                        w.append(",\n");
    1.51 -                    } else {
    1.52 -                        w.append("\n");
    1.53 -                    }
    1.54 -                }
    1.55 -                w.append("  ) {\n");
    1.56 -                for (int i = 1; i <= md.getColumnCount(); i++) {
    1.57 -                    w.append("    this." + md.getColumnName(i) + " = " + md.getColumnName(i) + ";\n");
    1.58 -                }
    1.59 -                w.append("  }\n");
    1.60 -                w.append("  public static List<" + db.classname() + "> query() throws SQLException {\n");
    1.61 -                w.append("    Connection c = DriverManager.getConnection(\"" + db.url() + "\", \"" + db.user() + "\", \"" + db.password() +"\");\n");
    1.62 -                w.append("    List<" + db.classname() + "> res = new ArrayList<" + db.classname() + ">();\n");
    1.63 -                w.append("    CallableStatement s = c.prepareCall(\"" + db.query() + "\");\n");
    1.64 -                w.append("    ResultSet rs = s.executeQuery();\n");
    1.65 -                w.append("    ResultSetMetaData md = rs.getMetaData();\n");
    1.66 -                w.append("    while (rs.next()) {\n");
    1.67 -                w.append("      res.add(new " + db.classname() + "(\n");
    1.68 -                for (int i = 1; i <= md.getColumnCount(); i++) {
    1.69 -                    w.append("        (" + md.getColumnClassName(i) + ")rs.getObject(" + i + ")");
    1.70 -                    if (i < md.getColumnCount()) {
    1.71 -                        w.append(",\n");
    1.72 -                    } else {
    1.73 -                        w.append("\n");
    1.74 -                    }
    1.75 -                }
    1.76 -                w.append("      ));\n");
    1.77 -                w.append("    };\n");
    1.78 -                w.append("    return res;\n");
    1.79 -                w.append("  }");
    1.80 +                generateConstructor(w, db, md);
    1.81 +                generateQueryMethod(w, db, md);
    1.82                  w.append("}");
    1.83                  w.close();
    1.84              } catch (IOException ex) {
    1.85 @@ -99,6 +70,75 @@
    1.86          return true;
    1.87      }
    1.88  // FINISH: livedb.processor
    1.89 +
    1.90 +    private void generateQueryMethod(Writer w, LiveDB db, ResultSetMetaData md) throws SQLException, IOException {
    1.91 +        w.append("  public static List<" + db.classname() + "> ")
    1.92 +         .append("query() throws SQLException {\n");
    1.93 +        w.append("    Connection c = DriverManager.getConnection(\"")
    1.94 +         .append(db.url()).append("\", \"")
    1.95 +         .append(db.user()).append("\", \"")
    1.96 +         .append(db.password()).append("\");\n");
    1.97 +        w.append("    List<").append(db.classname())
    1.98 +         .append("> res = new ArrayList<")
    1.99 +         .append(db.classname()).append(">();\n");
   1.100 +        w.append("    CallableStatement s = c.prepareCall(\"")
   1.101 +         .append(db.query()).append("\");\n");
   1.102 +        w.append("    ResultSet rs = s.executeQuery();\n");
   1.103 +        w.append("    ResultSetMetaData md = rs.getMetaData();\n");
   1.104 +        w.append("    while (rs.next()) {\n");
   1.105 +        w.append("      res.add(new " + db.classname() + "(\n");
   1.106 +        for (int i = 1; i <= md.getColumnCount(); i++) {
   1.107 +            w.append("        (")
   1.108 +             .append(md.getColumnClassName(i))
   1.109 +             .append(")rs.getObject(" + i).append(")");
   1.110 +            if (i < md.getColumnCount()) {
   1.111 +                w.append(",\n");
   1.112 +            } else {
   1.113 +                w.append("\n");
   1.114 +            }
   1.115 +        }
   1.116 +        w.append("      ));\n");
   1.117 +        w.append("    };\n");
   1.118 +        w.append("    return res;\n");
   1.119 +        w.append("  }");
   1.120 +    }
   1.121 +
   1.122 +    private void generateConstructor(Writer w, LiveDB db, ResultSetMetaData md) throws SQLException, IOException {
   1.123 +        w.append("  private " + db.classname() + "(\n");
   1.124 +        for (int i = 1; i <= md.getColumnCount(); i++) {
   1.125 +            w.append("    ").append(md.getColumnClassName(i))
   1.126 +             .append(" ").append(md.getColumnName(i));
   1.127 +            if (i < md.getColumnCount()) {
   1.128 +                w.append(",\n");
   1.129 +            } else {
   1.130 +                w.append("\n");
   1.131 +            }
   1.132 +        }
   1.133 +        w.append("  ) {\n");
   1.134 +        for (int i = 1; i <= md.getColumnCount(); i++) {
   1.135 +            w.append("    this.")
   1.136 +             .append(md.getColumnName(i))
   1.137 +             .append(" = ")
   1.138 +             .append(md.getColumnName(i))
   1.139 +             .append(";\n");
   1.140 +        }
   1.141 +        w.append("  }\n");
   1.142 +    }
   1.143 +
   1.144 +    private void generateField(Writer w, ResultSetMetaData md, int i) throws IOException, SQLException {
   1.145 +        w.append("  public final ")
   1.146 +         .append(md.getColumnClassName(i))
   1.147 +         .append(" ")
   1.148 +         .append(md.getColumnName(i))
   1.149 +         .append(";\n");
   1.150 +    }
   1.151 +
   1.152 +    private void generateClassHeader(Writer w, PackageElement pe, LiveDB db) throws IOException {
   1.153 +        w.append("package " + pe.getQualifiedName() + ";\n");
   1.154 +        w.append("import java.util.List;\n");
   1.155 +        w.append("import java.util.ArrayList;\n");
   1.156 +        w.append("import java.sql.*;\n");
   1.157 +    }
   1.158      private static Connection getConnection(String url, String user, String password) 
   1.159      throws SQLException {
   1.160          final ClassLoader cl = LiveDBProcessor.class.getClassLoader();