summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/CompilerException.java12
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/EntityHandler.java35
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/MyProcessor.java13
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/container/compiler/model/ClassG.java5
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/EntityMirror.java2
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/GeneratorSupport.java8
-rw-r--r--container-compiler-plugin/src/main/java/io/trygvis/persistence/TypeHandler.java7
-rw-r--r--myapp/pom.xml17
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/AddressBook.java61
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/MyMain.java6
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/Person.java (renamed from myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java)4
-rw-r--r--myapp/src/main/java/io/trygvis/container/myapp/package-info.java3
-rw-r--r--persistence-compile-time/pom.xml19
-rw-r--r--pom.xml11
14 files changed, 150 insertions, 53 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) {
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 @@
<artifactId>container-compiler-plugin</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <version>1.3.173</version>
+ </dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
+<!--
+ <annotationProcessors>
+ <processor>io.trygvis.container.compiler.MyProcessor</processor>
+ </annotationProcessors>
+-->
<compilerArguments>
<processor>io.trygvis.container.compiler.MyProcessor</processor>
</compilerArguments>
</configuration>
+ <dependencies>
+ <dependency>
+ <groupId>io.trygvis.container</groupId>
+ <artifactId>container-compiler-plugin</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
</plugin>
</plugins>
</build>
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/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/MyEntity.java b/myapp/src/main/java/io/trygvis/container/myapp/Person.java
index 5eeface..85e471d 100644
--- a/myapp/src/main/java/io/trygvis/container/myapp/MyEntity.java
+++ b/myapp/src/main/java/io/trygvis/container/myapp/Person.java
@@ -1,9 +1,11 @@
package io.trygvis.container.myapp;
import javax.persistence.Entity;
+import javax.persistence.Id;
@Entity
-public class MyEntity {
+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 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>container-playground</artifactId>
- <groupId>io.trygvis.container</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <artifactId>persistence-compile-time</artifactId>
- <dependencies>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- </dependencies>
-</project>
diff --git a/pom.xml b/pom.xml
index b8e3ab6..e9ea30b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,11 +22,20 @@
</configuration>
</plugin>
</plugins>
+ <!--
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.5.1</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ -->
</build>
<modules>
<module>container-compiler-plugin</module>
<module>myapp</module>
<module>container-core</module>
- <module>persistence-compile-time</module>
</modules>
</project>