diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 16:39:50 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2013-08-03 16:39:50 +0200 |
commit | 054cfb131292893d100a43ea2cb20c591c17c810 (patch) | |
tree | 0f726a1cd56a9e72cfeb92f6170606bd9739bf20 /container-compiler-plugin/src/main/java/io/trygvis | |
parent | b5d6f23d8ac3d4bedb139fdaecc2e9dc621f385a (diff) | |
download | container-playground-054cfb131292893d100a43ea2cb20c591c17c810.tar.gz container-playground-054cfb131292893d100a43ea2cb20c591c17c810.tar.bz2 container-playground-054cfb131292893d100a43ea2cb20c591c17c810.tar.xz container-playground-054cfb131292893d100a43ea2cb20c591c17c810.zip |
wip
Diffstat (limited to 'container-compiler-plugin/src/main/java/io/trygvis')
7 files changed, 56 insertions, 26 deletions
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<TypeElement> sqlEntities, Set<PackageElement> packages) throws Exception { for (TypeElement entity : sqlEntities) { - AnnotationMirror sqlEntity = findAnnotation(SqlEntity.class, entity.getAnnotationMirrors()); + AnnotationMirror sqlEntity = findAnnotation(SqlEntity.class, entity); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> 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<? extends AnnotationMirror> 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<? extends TypeElement> 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<String, TypeHandler> 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) { |