From df92538ab3d83da9839f08b28fc8a67317565463 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 3 Aug 2013 20:21:19 +0200 Subject: wip --- .../container/compiler/CompilerException.java | 4 ++ .../trygvis/container/compiler/EntityHandler.java | 66 +++++++++++++--------- .../container/compiler/InternalErrorException.java | 9 +++ .../io/trygvis/container/compiler/MyProcessor.java | 8 ++- .../compiler/NotImplementedException.java | 7 +++ .../trygvis/container/compiler/SqlUnitModel.java | 33 +++++++++++ 6 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 container-compiler-plugin/src/main/java/io/trygvis/container/compiler/InternalErrorException.java create mode 100644 container-compiler-plugin/src/main/java/io/trygvis/container/compiler/NotImplementedException.java create mode 100644 container-compiler-plugin/src/main/java/io/trygvis/container/compiler/SqlUnitModel.java (limited to 'container-compiler-plugin/src/main/java/io/trygvis/container') 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 index a3801ab..e9fcb87 100644 --- 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 @@ -5,6 +5,10 @@ import javax.lang.model.element.Element; public class CompilerException extends RuntimeException { public final Element element; + public CompilerException(String message) { + this(null, message); + } + 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 cf21887..c623af7 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 @@ -4,7 +4,7 @@ import io.trygvis.container.compiler.model.ClassG; import io.trygvis.container.compiler.model.TypeRef; import io.trygvis.persistence.EntityMirror; import io.trygvis.persistence.FieldMirror; -import io.trygvis.persistence.GeneratorSupport; +import io.trygvis.persistence.GeneratorConfiguration; import io.trygvis.persistence.SqlEntity; import io.trygvis.persistence.TypeHandler; @@ -18,6 +18,7 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; import javax.persistence.Id; import javax.tools.JavaFileObject; +import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -30,8 +31,8 @@ import static java.lang.Character.isUpperCase; import static javax.lang.model.util.ElementFilter.fieldsIn; public class EntityHandler extends AbstractHandler { - private GeneratorSupport generatorSupport = new GeneratorSupport(); - private List entities = new ArrayList<>(); + private GeneratorConfiguration generatorConfiguration = new GeneratorConfiguration(); + private SqlUnitModel sqlUnit = new SqlUnitModel(); private PackageElement packageElement; public EntityHandler(ProcessingEnvironment processingEnv) { @@ -48,7 +49,7 @@ public class EntityHandler extends AbstractHandler { TypeHandler typeHandler = (TypeHandler) typeHandlerClass.newInstance(); String type = entity.asType().toString(); - generatorSupport.addTypeHandler(new TypeRef(type, type), typeHandler); + generatorConfiguration.addTypeHandler(new TypeRef(type, type), typeHandler); System.out.println("Loaded TypeHandler for " + type + " through " + typeHandlerClass.getCanonicalName()); break; } @@ -58,7 +59,7 @@ public class EntityHandler extends AbstractHandler { System.out.println("packages = " + packages); if (packages.size() == 0) { - throw new CompilerException(null, "There has to be exactly one @SqlEntitySet annotated package."); + throw new CompilerException("There has to be exactly one @SqlEntitySet annotated package."); } packageElement = packages.iterator().next(); @@ -78,12 +79,12 @@ public class EntityHandler extends AbstractHandler { throw new CompilerException(type, "Could not find annotation " + c.getSimpleName()); } - public void processEntity(TypeElement element) throws Exception { - EntityMirror entityMirror = new EntityMirror(generatorSupport, new TypeRef(types.getDeclaredType(element)), + public void recordEntity(TypeElement element) throws Exception { + EntityMirror entityMirror = new EntityMirror(generatorConfiguration, new TypeRef(types.getDeclaredType(element)), sqlName(element.getSimpleName().toString())); for (VariableElement f : fieldsIn(elements.getAllMembers(element))) { - entityMirror.add(fromElement(generatorSupport, f)); + entityMirror.add(fromElement(generatorConfiguration, f)); } List idFields = new ArrayList<>(); @@ -100,44 +101,47 @@ public class EntityHandler extends AbstractHandler { throw new CompilerException(element, "This implementation only support a single @Id annotated field."); } - String p = elements.getPackageOf(element).getQualifiedName().toString(); + sqlUnit.add(entityMirror); + } + public void generate(EntityMirror entityMirror) throws IOException { ClassG g = new ClassG(entityMirror.daoType); - g.addPublicStaticFinalField(String.class, "createTableSql").value(toJavaString(entityMirror.createTableSql())); + g.addPublicStaticFinalField(String.class, "createTableSql").value(toJavaString(entityMirror.createTableSql(sqlUnit))); g.addPublicStaticFinalField(String.class, "dropTableSql").value(toJavaString(entityMirror.dropTableSql())); g.addPublicStaticFinalField(String.class, "insertIntoSql").value(toJavaString(entityMirror.insertIntoSql())); g.addPublicStaticFinalField(String.class, "deleteFromSql").value(toJavaString(entityMirror.deleteFromSql())); - entityMirror.insertInto(g); + entityMirror.insertInto(sqlUnit, g); + entityMirror.delete(g); + entityMirror.deleteById(g); - JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(entityMirror.daoType.fqName, element); + JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(entityMirror.daoType.fqName, sqlUnit.elementForEntity(entityMirror)); try (PrintWriter w = new PrintWriter(sourceFile.openWriter())) { g.write(w); } - - entities.add(entityMirror); } - public FieldMirror fromElement(GeneratorSupport generatorSupport, VariableElement var) { - TypeRef type = new TypeRef(var.asType().toString()); + public FieldMirror fromElement(GeneratorConfiguration generatorConfiguration, VariableElement var) { + TypeRef type = new TypeRef(var.asType()); // System.out.print("element = "); // elements.printElements(new PrintWriter(System.out), var); String javaName = var.getSimpleName().toString(); String sqlName = sqlName(javaName); boolean notNull = false; - FieldMirror field; + boolean unique = false; boolean id = isId(var); - if (generatorSupport.isPrimitive(type)) { - field = new PrimitiveFieldMirror(type, javaName, sqlName, id, notNull); - } else if (generatorSupport.hasTypeHandler(type)) { - if (id) { - throw new CompilerException(var, "A @Id field has to be a primitive or embedded."); - } + boolean primitive = generatorConfiguration.isPrimitive(type); + if (id && !primitive) { + throw new CompilerException(var, "A @Id field has to be a primitive or embedded."); + } - field = new FieldMirror.ReferenceFieldMirror(type, javaName, sqlName, notNull); + FieldMirror field; + if (primitive) { + field = new PrimitiveFieldMirror(type, javaName, sqlName, id, notNull, unique); + } else if (generatorConfiguration.hasTypeHandler(type)) { + throw new CompilerException(var, "Missing type handler for type: " + type.fqName); } else { - throw new CompilerException(var, "Missing type handler for type: " + type); + field = new FieldMirror.ReferenceFieldMirror(type, javaName, sqlName, notNull, unique); } -// System.out.println("field = " + field); return field; } @@ -145,12 +149,18 @@ public class EntityHandler extends AbstractHandler { return var.getAnnotation(Id.class) != null; } - // TODO: Rename DAO to Session. public void phase3() throws Exception { + for (EntityMirror entity : sqlUnit.getEntities().values()) { + generate(entity); + } + generateSession(); + } + + private void generateSession() throws IOException { String p = packageElement.getQualifiedName().toString(); // TODO: Support a name prefix from @SqlEntitySet - TypeRef type = new TypeRef(p + ".Daos"); + TypeRef type = new TypeRef(p + ".Session"); ClassG g = new ClassG(type); diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/InternalErrorException.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/InternalErrorException.java new file mode 100644 index 0000000..dfb6746 --- /dev/null +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/InternalErrorException.java @@ -0,0 +1,9 @@ +package io.trygvis.container.compiler; + +public class InternalErrorException extends RuntimeException { + + public InternalErrorException(String message) { + super(message); + } + +} 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 713b948..297b9d8 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 @@ -77,9 +77,15 @@ public class MyProcessor implements Processor { System.out.println("claimed = " + claimed); return claimed; } catch (CompilerException e) { + e.printStackTrace(System.err); Messager messager = processingEnv.getMessager(); messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), e.element); return true; + } catch (InternalErrorException e) { + e.printStackTrace(System.err); + Messager messager = processingEnv.getMessager(); + messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage()); + return true; } catch (RuntimeException e) { throw e; } catch (Exception e) { @@ -116,7 +122,7 @@ public class MyProcessor implements Processor { new LogHandler(processingEnv).processLog((TypeElement) element); } if (types.isSameType(entity.asType(), annotationType)) { - entityHandler.processEntity((TypeElement) element); + entityHandler.recordEntity((TypeElement) element); } } } diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/NotImplementedException.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/NotImplementedException.java new file mode 100644 index 0000000..a1941b6 --- /dev/null +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/NotImplementedException.java @@ -0,0 +1,7 @@ +package io.trygvis.container.compiler; + +public class NotImplementedException extends InternalErrorException { + public NotImplementedException() { + super("Not implemented"); + } +} diff --git a/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/SqlUnitModel.java b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/SqlUnitModel.java new file mode 100644 index 0000000..20d5f3b --- /dev/null +++ b/container-compiler-plugin/src/main/java/io/trygvis/container/compiler/SqlUnitModel.java @@ -0,0 +1,33 @@ +package io.trygvis.container.compiler; + +import io.trygvis.container.compiler.model.TypeRef; +import io.trygvis.persistence.EntityMirror; + +import javax.lang.model.element.Element; +import java.util.Map; +import java.util.TreeMap; + +public class SqlUnitModel { + private Map entities = new TreeMap<>(); + private Map elements = new TreeMap<>(); + + public void add(EntityMirror entity) { + entities.put(entity.type, entity); + } + + public EntityMirror get(TypeRef type) { + EntityMirror entity = entities.get(type); + if (entity == null) { + throw new InternalErrorException("Could not find entity for " + type.fqName); + } + return entity; + } + + public Map getEntities() { + return entities; + } + + public Element elementForEntity(EntityMirror entity) { + return elements.get(entity); + } +} -- cgit v1.2.3