From 054cfb131292893d100a43ea2cb20c591c17c810 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Aug 2013 16:39:50 +0200 Subject: wip --- .../container/compiler/CompilerException.java | 12 +++++ .../trygvis/container/compiler/EntityHandler.java | 35 ++++++------- .../io/trygvis/container/compiler/MyProcessor.java | 13 ++++- .../trygvis/container/compiler/model/ClassG.java | 5 +- .../java/io/trygvis/persistence/EntityMirror.java | 2 +- .../io/trygvis/persistence/GeneratorSupport.java | 8 +-- .../java/io/trygvis/persistence/TypeHandler.java | 7 +++ myapp/pom.xml | 17 ++++++ .../io/trygvis/container/myapp/AddressBook.java | 61 ++++++++++++++++++++++ .../java/io/trygvis/container/myapp/MyEntity.java | 10 ---- .../java/io/trygvis/container/myapp/MyMain.java | 6 --- .../java/io/trygvis/container/myapp/Person.java | 12 +++++ .../io/trygvis/container/myapp/package-info.java | 3 ++ persistence-compile-time/pom.xml | 19 ------- pom.xml | 11 +++- 15 files changed, 159 insertions(+), 62 deletions(-) create mode 100644 container-compiler-plugin/src/main/java/io/trygvis/container/compiler/CompilerException.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java delete mode 100644 myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java delete mode 100644 myapp/src/main/java/io/trygvis/container/myapp/MyMain.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/Person.java create mode 100644 myapp/src/main/java/io/trygvis/container/myapp/package-info.java delete mode 100644 persistence-compile-time/pom.xml diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/CompilerException.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/CompilerException.java new file mode 100644 index 0000000..a3801ab --- /dev/null +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/CompilerException.java @@ -0,0 +1,12 @@ +package io.trygvis.container.compiler; + +import javax.lang.model.element.Element; + +public class CompilerException extends RuntimeException { + public final Element element; + + public CompilerException(Element element, String message) { + super(message); + this.element = element; + } +} diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java index fc7ca4f..ad36988 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java @@ -1,10 +1,6 @@ package io.trygvis.container.compiler; import io.trygvis.container.compiler.model.ClassG; -import io.trygvis.container.compiler.model.FieldRef; -import io.trygvis.container.compiler.model.ParameterRef; -import io.trygvis.container.compiler.model.Parameters; -import io.trygvis.container.compiler.model.TypeRef; import io.trygvis.persistence.EntityMirror; import io.trygvis.persistence.FieldMirror; import io.trygvis.persistence.GeneratorSupport; @@ -23,13 +19,11 @@ import javax.lang.model.type.TypeMirror; import javax.persistence.Id; import javax.tools.JavaFileObject; import java.io.PrintWriter; -import java.sql.Connection; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; -import static io.trygvis.container.compiler.Utils.toFieldName; import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror; import static java.lang.Character.isUpperCase; import static javax.lang.model.util.ElementFilter.fieldsIn; @@ -45,7 +39,7 @@ public class EntityHandler extends AbstractHandler { public void phase1(Set sqlEntities, Set packages) throws Exception { for (TypeElement entity : sqlEntities) { - AnnotationMirror sqlEntity = findAnnotation(SqlEntity.class, entity.getAnnotationMirrors()); + AnnotationMirror sqlEntity = findAnnotation(SqlEntity.class, entity); for (Map.Entry v : sqlEntity.getElementValues().entrySet()) { switch (v.getKey().getSimpleName().toString()) { case "value": @@ -63,24 +57,24 @@ public class EntityHandler extends AbstractHandler { System.out.println("packages = " + packages); if (packages.size() == 0) { - throw new RuntimeException("There has to be exactly one @SqlEntitySet annotated package."); + throw new CompilerException(null, "There has to be exactly one @SqlEntitySet annotated package."); } + packageElement = packages.iterator().next(); + if (packages.size() != 1) { - throw new RuntimeException("There can only be one @SqlEntitySet annotated package."); + throw new CompilerException(packageElement, "There can only be one @SqlEntitySet annotated package."); } - - packageElement = packages.iterator().next(); } - private AnnotationMirror findAnnotation(Class c, List annotations) { - TypeElement t = elements.getTypeElement(c.getCanonicalName()); - for (AnnotationMirror a : annotations) { - if (types.isSameType(a.getAnnotationType(), t.asType())) { + private AnnotationMirror findAnnotation(Class c, TypeElement type) { + TypeMirror annotationType = elements.getTypeElement(c.getCanonicalName()).asType(); + for (AnnotationMirror a : type.getAnnotationMirrors()) { + if (types.isSameType(a.getAnnotationType(), annotationType)) { return a; } } - throw new RuntimeException("Could not find annotation " + c.getSimpleName()); + throw new CompilerException(type, "Could not find annotation " + c.getSimpleName()); } public void processEntity(TypeElement element) throws Exception { @@ -99,10 +93,10 @@ public class EntityHandler extends AbstractHandler { } if (idFields.size() == 0) { - throw new RuntimeException("An @Entity is required to have at least one @Id field."); + throw new CompilerException(element, "An @Entity is required to have at least one @Id field."); } if (idFields.size() != 1) { - throw new RuntimeException("This implementation only support a single @Id annotated field."); + throw new CompilerException(element, "This implementation only support a single @Id annotated field."); } String p = elements.getPackageOf(element).getQualifiedName().toString(); @@ -137,12 +131,12 @@ public class EntityHandler extends AbstractHandler { field = new PrimitiveFieldMirror(var, javaName, sqlName, id, notNull); } else if (generatorSupport.hasTypeHandler(type)) { if (id) { - throw new RuntimeException("A @Id field has to be a primitive or embedded."); + throw new CompilerException(var, "A @Id field has to be a primitive or embedded."); } field = new FieldMirror.ReferenceFieldMirror(var, javaName, sqlName, notNull); } else { - throw new RuntimeException("Missing type handler for type: " + type); + throw new CompilerException(var, "Missing type handler for type: " + type); } // System.out.println("field = " + field); return field; @@ -152,6 +146,7 @@ public class EntityHandler extends AbstractHandler { return var.getAnnotation(Id.class) != null; } + // TODO: Rename DAO to Session. public void phase3() throws Exception { String p = packageElement.getQualifiedName().toString(); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java index 43ba49e..713b948 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java @@ -6,6 +6,7 @@ import io.trygvis.persistence.SqlEntitySet; import org.springframework.transaction.annotation.Transactional; import javax.annotation.processing.Completion; +import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.Processor; import javax.annotation.processing.RoundEnvironment; @@ -20,6 +21,7 @@ import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.persistence.Entity; +import javax.tools.Diagnostic; import java.util.HashSet; import java.util.Set; @@ -67,8 +69,17 @@ public class MyProcessor implements Processor { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { + System.out.println("io.trygvis.container.compiler.MyProcessor.process"); + System.out.println("annotations = " + annotations); + System.out.println("roundEnv = " + roundEnv); try { - return work(roundEnv); + boolean claimed = work(roundEnv); + System.out.println("claimed = " + claimed); + return claimed; + } catch (CompilerException e) { + Messager messager = processingEnv.getMessager(); + messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), e.element); + return true; } catch (RuntimeException e) { throw e; } catch (Exception e) { diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java index 6bfd0be..9befa36 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java @@ -167,7 +167,10 @@ public class ClassG { parameters.add("final " + p.klass.name + " " + p.name); } - writer.print(" public " + returnString + " " + method.name + "(" + collectionToDelimitedString(parameters, ", ") + ")"); + writer.print(" " + + Modifier.toString(method.modifiers) + " " + + returnString + " " + + method.name + "(" + collectionToDelimitedString(parameters, ", ") + ")"); if (method.exceptions.isEmpty()) { writer.println(" {"); } else { diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java index 7b45145..41ae1d4 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java @@ -62,7 +62,7 @@ public class EntityMirror { body.add(" stmt.executeUpdate();"); body.add("}"); - g.addMethod(body, TypeRef.VOID, "insertInto", con, object). + g.addStaticMethod(body, TypeRef.VOID, "insertInto", con, object). exception(g.addImport(SQLException.class)); } } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java index d3293c3..379f6f6 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java @@ -2,6 +2,7 @@ package io.trygvis.persistence; import javax.lang.model.element.Element; import javax.lang.model.type.TypeMirror; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -11,9 +12,10 @@ public class GeneratorSupport { private final Map typeHandlers = new HashMap<>(); { - typeHandlers.put("java.lang.Integer", new TypeHandler.IntTypeHandler()); - typeHandlers.put("java.lang.Long", new TypeHandler.LongTypeHandler()); - typeHandlers.put("java.util.Date", new TypeHandler.DateTypeHandler()); + typeHandlers.put(Integer.class.getName(), new TypeHandler.IntTypeHandler()); + typeHandlers.put(Long.class.getName(), new TypeHandler.LongTypeHandler()); + typeHandlers.put(String.class.getName(), new TypeHandler.StringTypeHandler()); + typeHandlers.put(Date.class.getName(), new TypeHandler.DateTypeHandler()); primitiveTypeHandlers.putAll(typeHandlers); } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java index 252f4b4..1d7d424 100644 --- a/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java +++ b/container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java @@ -17,6 +17,13 @@ public interface TypeHandler { } } + public static class StringTypeHandler implements TypeHandler { + @Override + public String resultSetSetter(int i, String o, FieldMirror field) { + return "setString(" + i + ", " + o + "." + field.javaName + ")"; + } + } + public static class DateTypeHandler implements TypeHandler { @Override public String resultSetSetter(int i, String o, FieldMirror field) { diff --git a/myapp/pom.xml b/myapp/pom.xml index 7253422..8851022 100644 --- a/myapp/pom.xml +++ b/myapp/pom.xml @@ -20,16 +20,33 @@ container-compiler-plugin ${project.version} + + com.h2database + h2 + 1.3.173 + maven-compiler-plugin + io.trygvis.container.compiler.MyProcessor + + + io.trygvis.container + container-compiler-plugin + ${project.version} + + diff --git a/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java new file mode 100644 index 0000000..218f25f --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java @@ -0,0 +1,61 @@ +package io.trygvis.container.myapp; + +import java.io.EOFException; +import java.sql.Connection; +import java.sql.DriverManager; + +public class AddressBook { + public static void main(String[] args) throws Exception { + try { + new AddressBook().work(); + } catch (EOFException ignore) { + } + } + + private void work() throws Exception { + boolean done = false; + while (!done) { + String cmd = Character.toString(menu()); + switch (cmd) { + case "l": + run(new ListCommand()); + break; + case "q": + done = true; + break; + default: + System.out.println("Unknown command: " + cmd); + } + } + } + + public static interface Command { + void run(Connection c) throws Exception; + } + + public void run(Command command) throws Exception { + try (Connection c = DriverManager.getConnection("jdbc:h2:mem")) { + c.setAutoCommit(false); + command.run(c); + } + } + + private char menu() throws Exception { + System.out.println("Menu:"); + System.out.println("l List"); + System.out.println("a Add"); + System.out.println("q Quit"); + int read = System.in.read(); + if (read == -1) { + throw new EOFException(); + } + return (char) read; + } + + public static class ListCommand implements Command { + @Override + public void run(Connection c) throws Exception { + PersonDao.insertInto(null, new Person()); + } + } +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java b/myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java deleted file mode 100644 index 5eeface..0000000 --- a/myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.trygvis.container.myapp; - -import javax.persistence.Entity; - -@Entity -public class MyEntity { - public Long id; - - public String name; -} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/MyMain.java b/myapp/src/main/java/io/trygvis/container/myapp/MyMain.java deleted file mode 100644 index f878d43..0000000 --- a/myapp/src/main/java/io/trygvis/container/myapp/MyMain.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.trygvis.container.myapp; - -public class MyMain { - public static void main(String[] args) { - } -} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/Person.java b/myapp/src/main/java/io/trygvis/container/myapp/Person.java new file mode 100644 index 0000000..85e471d --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/Person.java @@ -0,0 +1,12 @@ +package io.trygvis.container.myapp; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Person { + @Id + public Long id; + + public String name; +} diff --git a/myapp/src/main/java/io/trygvis/container/myapp/package-info.java b/myapp/src/main/java/io/trygvis/container/myapp/package-info.java new file mode 100644 index 0000000..c212ca1 --- /dev/null +++ b/myapp/src/main/java/io/trygvis/container/myapp/package-info.java @@ -0,0 +1,3 @@ +@SqlEntitySet package io.trygvis.container.myapp; + +import io.trygvis.persistence.SqlEntitySet; diff --git a/persistence-compile-time/pom.xml b/persistence-compile-time/pom.xml deleted file mode 100644 index 90f5b8a..0000000 --- a/persistence-compile-time/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - 4.0.0 - - container-playground - io.trygvis.container - 1.0-SNAPSHOT - - persistence-compile-time - - - commons-lang - commons-lang - 2.6 - - - diff --git a/pom.xml b/pom.xml index b8e3ab6..e9ea30b 100644 --- a/pom.xml +++ b/pom.xml @@ -22,11 +22,20 @@ + container-compiler-plugin myapp container-core - persistence-compile-time -- cgit v1.2.3